You can use a JavaCompute node
to interact with a map in a global cache or an external WebSphere® eXtreme
Scale grid.
About this task
You can use a JavaCompute node
to store data in a global map. You can then create another JavaCompute node that can get
data from the global cache for processing or routing. The JavaCompute node uses the MbGlobalMap
object to access the global cache or external grid. This
class can be used to get a map, and to put or get data in a map. You
cannot create a map explicitly, but if you get a map that does not
exist, the map is created automatically. When you get
a global map from an external grid, the getGlobalMap method makes
a connection to the grid if one does not exist. Interactions
with the cache happen outside the message flow transaction, and are
committed immediately. If an exception is thrown downstream of the
node that interacts with the cache, the cache interactions are not
rolled back.
The following steps describe how one message flow
adds data to a map, and another flow gets the data from that map.
Procedure
- Create a message flow that contains a JavaCompute node.
You
can place the
JavaCompute node
before or after an
MQOutput node.
In the following example, the
JavaCompute node is placed
after an
MQOutput node
so that the
JavaCompute node
can use the message ID of the output message.
- Double-click the JavaCompute node.
The New Java Compute Node Class wizard
opens.
- On the Java Compute Node Class pane,
provide the appropriate information, then click Next.
You can accept the default values by clicking Next.
- On the Java Compute Node Class Template pane,
select the appropriate template for your scenario, then click Finish.
The template Java file
opens in the editor, indicating where you can add your own code.
- To get a map from the embedded cache, or create the map
if it does not exist, add the MbGlobalMap object, as shown in the
following example.
The MbGlobalMap object is also described
in the Java Plugin API documentation.
MbGlobalMap globalMap = MbGlobalMap.getGlobalMap("MyMap");
This
example gets the map called "MyMap". If the map does not exist, it
is created. Alternatively, when you are using the embedded cache,
you can use the default map by not specifying a map name, as shown
in the following example.
When you are connecting to
an external grid, no default map exists. You must specify a valid
map name on the external grid.MbGlobalMap globalMap = MbGlobalMap.getGlobalMap();
The
cache uses
WebSphere eXtreme
Scale dynamic maps.
Any map name is allowed, apart from names that begin with SYSTEM.BROKER
,
which is reserved for use by the integration node. The default
map is named SYSTEM.BROKER.DEFAULTMAP; you can use or clear this map.
To get a map from an external grid, add the MbGlobalMap
object, specifying the name of the map on the external grid and the
name of the policy that is used to connect to the grid:
MbGlobalMap remoteCacheServerMap = MbGlobalMap.getGlobalMap("MyMap.LUT","remoteCacheServer");
In
this example, the map,
MyMap.LUT
, is on an external
grid. The policy that is used to connect to the grid is called "remoteCacheServer".
- Optional: Specify how long, in seconds, the
data remains in the global cache before it is removed automatically.
- To put data in the map, you must create a key and value
pair in the map, as shown in the following example.
For
keys and values, Java primitive
types and strings are supported. Java objects
are supported as values. For the embedded grid only, keys can also
be arrays of primitives or strings. (To use your own Java classes with the global cache, put the
JAR files that contain the Java classes
in one of the shared-classes directories.)
- Save the message flow.
- To access the stored data from another message flow, create
a flow that contains a JavaCompute node.
For example, the following message flow contains a
JavaCompute node that retrieves
the appropriate information from the map, then sends a response message
through an
MQReply node.
- Repeat steps 2, 3, and 4 to create a Java file for your new JavaCompute node.
- Use the MbGlobalMap object to get the map that you created
in step 5, then get the data that you added to the map in step 6.
If you know that the data that you are retrieving is a string, for
example, specify string when you get the data, as shown in the following
example.
MbGlobalMap globalMap = MbGlobalMap.getGlobalMap("MyMap");
...
String val = (String)globalMap.get(key);
The
following example shows how to get the data that you added to a map
on an external grid:
MbGlobalMap remoteCacheServerMap = MbGlobalMap.getGlobalMap("MyMap.LUT","remoteCacheServer");
...
String val = (String)remoteCacheServerMap.get(key);
To prevent the cache from becoming too big, when you have
finished with a key and value pair, use the remove()
method
to delete that data from the map.
You can use content assist to list available options when you are constructing
Java code. To access content assist, place the cursor at the
point of insertion, then either click or press Ctrl+Space. For example, if you type "myMap", then press
Ctrl+Space, you can choose from a list of options, including the following actions.
- remove, which removes a single object from a global cache
- containsKey, which indicates whether a key exists in a map
- update, which updates an existing object
All available options are documented in the
Javadoc for the Java
Plugin API (see
IBM Integration
API).
- Save your message flow.
- Deploy your message flows.