Tuesday, 4 October 2016

Global Cache PUT & GET

Global Cache
IBM Integration Bus provides an out of the box caching mechanism based on WebSphere eXtreme Scale.WebSphere eXtreme Scale provides a scalable, in-memory data grid. The data grid dynamically caches, partitions, replicates, and manages data across multiple servers.

This cache can be used to store reference data that are regularly accessed or to hold a routing table.

The cache is not enabled by default and it is really easy to enable it: the default configuration is activated by setting a configuration parameters through the IBM Explorer administration tool.
To activate the cache across different Integration Node instances, a XML configuration file (templates are provided) has to be defined.

Specialized skills in extremes scale is not necessary in order to use the cache. There is however two important cache components that may be good to know

Catalog servers
component that is embedded in an integration server and that controls placement of data and monitors the health of containers. You must have at least one catalog server in your global cache.
Container servers
component that is embedded in the integration server that holds a subset of the cache data. Between them, all container servers in the global cache host all of the cache data at least once. If more than one container exists, the default cache policy ensures that all data is replicated at least once. In this way, the global cache can cope with the loss of container servers without losing data.
Map
A data structure that maps keys to values. One map is the default map, but the global cache can have several maps.
Each execution group can host a WebSphere XS catalog server, container server, or both. Additionally, each execution group can make a client connection to the cache for use by message flows. The global cache works out of the box, with default settings, and no configuration -- you just switch it on! You do not need to install WebSphere XS alongside the broker, or any other additional components or products.

Scenario:-
Put And Get Key,Values in Global Cache


Write the below code in JCN
MbMessage outMessage = new MbMessage(inMessage);
                                    outAssembly = new MbMessageAssembly(inAssembly, outMessage);
// ----------------------------------------------------------
            // Add user code below
MbGlobalMap map = MbGlobalMap.getGlobalMap();
           
MbElement inParser = inAssembly.getMessage().getRootElement().getLastChild();
String key = "";
if(inParser.getFirstElementByPath("Input/key") != null)
{
key = inParser.getFirstElementByPath("Input/key").getValue().toString();
}
                                                     
MbElement outParser = outAssembly.getMessage().getRootElement().getLastChild ();
outParser.getLastChild().detach();
MbElement result = outParser.createElementAsLastChild(MbElement.TYPE_NAME, "Result", null);
                                   
if(!map.containsKey(key))
{
String value = "";
if(inParser.getFirstElementByPath("Input/value") != null)
      {
value = inParser.getFirstElementByPath("Input/value").getValue().toString();
      }
      map.put(key, value);
result.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "status", "Key = "+key + "inserted Successful in Global Cache");
      }
      else
      {
      Object value = map.get(key);             
                                          result.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Key", key);
result.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Value", value);
                                    }
Input Messages:-
//<Input><key>Bhanu</key><value>2032</value></Input>
//<Input><key>Key1</key></Input>
Set below Properties in MQ:-

Now the obtained OutPut Structure will look like:-
Global Cache with Grids Code:-

MbGlobalMap map = MbGlobalMap.getGlobalMap("grid1");
MbGlobalMap mapWithGrid2 = MbGlobalMap.getGlobalMap("grid2");
MbElement inParser = inAssembly.getMessage().getRootElement().getLastChild();
String key = "", value = "";
if(inParser.getFirstElementByPath("Input/key") != null)
{
key = inParser.getFirstElementByPath("Input/key").getValue().toString();
}
if(inParser.getFirstElementByPath("Input/value") != null)
{
value = inParser.getFirstElementByPath("Input/value").getValue().toString();
}
map.put(key, value);
mapWithGrid2.put(key, value);
Object dataFromGrid1 = map.get(key);
Object dataFromGrid2 = mapWithGrid2.get(key);

No comments:

Post a Comment