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.

public class DocumentSequencer {
	   private static final NamedCache seq_cache = CacheFactory.getCache("repl-" + 
	   public static void main(String [] args)
		   for (int i=1;i<11;i++)
	   public static long getNextSequenceFromDocSequencer()
		   String key = "Document";
		   long nextSeq;
		   Object obj = seq_cache.get(key);
		   if (obj==null)
			   seq_cache.put(key, new Long(nextSeq));
			   nextSeq = (Long)obj+1;
			   seq_cache.put(key, new Long(nextSeq));
		   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="">
    <xpf:category id="%OSB_FUNCTIONS%">
            <xpf:comment>Gets next value from clustered counter</xpf:comment>
            <xpf:method>long getNextSequenceFromDocSequencer()</xpf:method>

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.

About atheek

I am a Tech 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.


  2. atheek says:

    Have a look at the coherence samples here: , 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: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s