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, configure it as described in Configuring the embedded global cache.
  • If you are connecting to an external grid, create a WXSServer policy to define how the connection is made. See Creating policies with the IBM App Connect Enterprise Toolkit.
  • 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 servers that participate in the global cache. For more information about the shared-classes directories, see Java shared classloader.

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

  1. 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.
    The graphic shows an MQInput node that is connected to an MQOutput node, which is connected to a JavaCompute node.
  2. Double-click the JavaCompute node.
    The New Java Compute Node Class wizard opens.
  3. On the Java Compute Node Class pane, provide the appropriate information, then click Next.
    You can accept the default values by clicking Next.
  4. 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.

  5. 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".
  6. 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.

  7. 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.)
    globalMap.put(key, val);
  8. Save the message flow.
  9. 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.
    The graphic shows an MQInput node that is connected to a JavaCompute node, which is connected to an MQReply node.
  10. Repeat steps 2, 3, and 4 to create a Java file for your new JavaCompute node.
  11. 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 Edit > Content Assist 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).
  12. Save your message flow.
  13. Deploy your message flows.