Accessing the global cache by using a JavaCompute node
You can use a JavaCompute node to interact with a map in a global cache or an external WebSphere® eXtreme Scale grid.
Before you begin
You can also access the global cache by using a Mapping node; see Accessing the global cache by using a Mapping node.
- Read the concept information in Data caching overview.
- Ensure that the global cache or external grid is available. The cache is disabled by default. To use the cache, select an integration node level cache policy (see Configuring the embedded global cache).
- If you are connecting to an external grid, create a configurable service to define how the connection is made. For more information, see mqsicreateconfigurableservice command.
- 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. These Java classes must be available to all integration nodes and integration servers that participate in the global cache. For more information about the shared-classes directories, see Java shared classloader.
About this task
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.
- 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.
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("MyMap");
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.
MbGlobalMap globalMap = MbGlobalMap.getGlobalMap();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 configurable service that is used to connect to the grid:
In this example, the map, "MyMap.LUT", is on an external grid on an XC10 appliance. The configurable service that is used to connect to the grid is called "xc10Connection".
MbGlobalMap xc10Map = MbGlobalMap.getGlobalMap("MyMap.LUT","xc10Connection");
- Optional: Specify how long, in seconds, the
data remains in the global cache before it is removed automatically.
This time is known as the time to live and is counted from when that map entry is last updated. You specify this time when you get an MbGlobalMap object. The value applies to all cache entries that are created by using that MbGlobalMap object. For detailed instructions, see Specifying how long data remains in the global cache by using the JavaCompute node.
- 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
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 xc10Map = MbGlobalMap.getGlobalMap("MyMap.LUT","xc10Connection"); ... String val = (String)xc10Map.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 clickor 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
- Save your message flow.
- Deploy your message flows.