Implementing in-memory,clustered, stateful counter in OSB using Coherence

This post shows how to leverage Coherence to implement an in memory ,clustered , stateful counter in OSB. The counter is clustered means all managed servers in the cluster share the same counter and the sequence is mantained across all managed server instances.

Code below uses basic Coherence Java API to maintain a coherence cache for the counter . We will convert that code to a custom xpath function so that it can be easily accessed within OSB – i.e. from message pipeline as well as within Xquery transformations.

import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;
public class DocumentSequencer {
	   private static final NamedCache seq_cache = CacheFactory.getCache("repl-" + 
	            "DocumentSequencer");
	   public static void main(String [] args)
	   {
		   for (int i=1;i<11;i++)
			   System.out.println(getNextSequenceFromDocSequencer());
	   }
	   
	   public static long getNextSequenceFromDocSequencer()
	   {
		   String key = "Document";
		   long nextSeq;
		   seq_cache.lock("monitor",-1);
		   Object obj = seq_cache.get(key);
		   if (obj==null)
		   {
			   nextSeq=1;
			   seq_cache.put(key, new Long(nextSeq));
			   
		   }
		   else 
		   {
			   nextSeq = (Long)obj+1;
			   seq_cache.put(key, new Long(nextSeq));
		   }
		   seq_cache.unlock("monitor");
		   return nextSeq;
	   }


}

The key method here is public static long getNextSequenceFromDocSequencer(). It returns the next value from the sequencer. The code uses basic coherence api functions. The lock() and unlock() call is used to synchronize the update of counter by the threads in the different jvm instances in the cluster.

To test this in OEPE , place coherence.jar from $COHERENCE_HOME/lib folder in build path of the project. The cache named “repl-DocumentSequencer” which we use here matches the wild card cache configuration “repl-*” in coherence-cache-config.xml in coherence.jar. This is a replicated cache meaning the data we put into the cache will be replicated across all instances in the cluster. Thus the data will be available until all instances in the cluster are shutdown at the same time.

Convert the above compiled class to DocumentSequencer.jar and place it on on $OSB_HOME/config/xpath-functions folder.
XML descriptor for this function looks as below:

<?xml version="1.0" encoding="UTF-8"?>
<xpf:xpathFunctions xmlns:xpf="http://www.bea.com/wli/sb/xpath/config">
    <xpf:category id="%OSB_FUNCTIONS%">
        <xpf:function>
            <xpf:name>getNextSequenceFromDocSequencer</xpf:name>
            <xpf:comment>Gets next value from clustered counter</xpf:comment>
            <xpf:namespaceURI>http://www.bea.com/xquery/xquery-functions</xpf:namespaceURI>
            <xpf:className>DocumentSequencer</xpf:className>
            <xpf:method>long getNextSequenceFromDocSequencer()</xpf:method>
            <xpf:isDeterministic>false</xpf:isDeterministic>
            <xpf:scope>Pipeline</xpf:scope>
            <xpf:scope>SplitJoin</xpf:scope>
        </xpf:function>  		
    </xpf:category>
</xpf:xpathFunctions>

Place this as DocumentSequencer.xml in the same folder and restart the servers.

For testing this a simple HTTP Messaging Proxy with GET method is used in which a replace action returns the next counter value.

Invoke each managed server url for the service in a browser. In my configuration osb managed server 1 runs on 8002 and osb managed server 2 runs on port 8003.

As can be seen the counter sequence is maintained across the cluster . Now restart one server and test again. The next value from the sequence will be returned. This shows the state is maintained acrosss restarts of individual cluster nodes.

Advertisements

About atheek

I am a Weblogic consultant working in Middleware/Integration area.
This entry was posted in Coherence, OSB and tagged , , . Bookmark the permalink.

2 Responses to Implementing in-memory,clustered, stateful counter in OSB using Coherence

  1. Syam says:

    Hi Atheek,
    I am new to OSB, as per my project requirement I have to store the data in coherence. After that fetch the data from coherence, same data writes in log files.
    Please help me in this regard.

    Thanks,
    Syam

  2. atheek says:

    Have a look at the coherence samples here: http://coherence.oracle.com/display/COH35UG/Basic+Coherence+Features+Examples , especially the one for basic data access ( getting, puting etc) which can achieve what you need..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s