Wednesday, March 10, 2010

JGroups-Spring In Action

Tagged with: ,
Thursday, February 21, 2008, 17:00
This news item was posted in HowTo & Tutorial category and has 0 Comments so far.

If your are new with jgroups ,take a look JGroups Basic and other jGroups posts
Some Sample Code you may use for adding networking utilities to your Application

Two FactoryBeans for JGroups

  1. ChannelBeanFactory for creating JChannel Instances
  2. ReplicatedHashMapFactory for ReplicatedHashMapFactory Instances

add jgroups to your dependency pom.xml

<dependency>
<groupId>jgroups</groupId>
<artifactId>jgroups</artifactId>
<version>2.5.2</version>
</dependency>

ChannelBeanFactory.java
Is a Spring BeanFactory to create and configure JGroups Channel by spring configuration

public class ChannelBeanFactory extends AbstractFactoryBean implements DisposableBean {
private String jgroupsconfig;
private String cluster_name;
private JChannelFactory factory;

public Class getObjectType() {
return Channel.class;
}

protected Object createInstance() throws Exception {
if (factory == null)

this.factory = new JChannelFactory(jgroupsconfig);

Channel jChannel = factory.createChannel();
jChannel.connect(cluster_name);
return jChannel;
}

And another nice FactoryBean for distributable Map implementation of JGroups

ReplicatedHashMapFactory.java


public class ReplicatedHashMapFactory extends AbstractFactoryBean {
String channelName = "Altuure Cluster";
private String clusterOptions = null;
private boolean distributable=false;
private int timeout = 5000;

protected Object createInstance() throws Exception {
if(!distributable)
return new HashMap();
JChannel jChannel;
ReplicatedHashMap hashtable;
if (clusterOptions == null) {
jChannel = new JChannel();
jChannel.connect(channelName);

hashtable = new ReplicatedHashMap(jChannel,false, timeout);
hashtable.setBlockingUpdates(true);
} else {
jChannel = new JChannel(Thread.currentThread().getContextClassLoader().getResource(clusterOptions));
jChannel.connect(channelName);

hashtable = new ReplicatedHashMap(jChannel,false, timeout);
hashtable.setBlockingUpdates(true);

}
hashtable.start(timeout);
return hashtable;
}

protected void destroyInstance(Object instance) throws Exception {
if (instance instanceof ReplicatedHashMap) {
ReplicatedHashMap o = (ReplicatedHashMap) instance;
o.getChannel().close();
o.stop();

}
}

and sample configuration and Test Case

<bean id="com.altuure.spring.jgroups.ReplicatedHashMapFactory">
class="com.altuure.spring.jgroups.ReplicatedHashMapFactory">

<property name="channelName" value="App1"></property>
<property name="clusterOptions" value="udp.xml"></property>
<property name="timeout" value="5000"></property>
<property name="distributable" value="true"></property>

</bean>

ReplicatedHashMapTest.java

public class ReplicatedHashMapTest extends TestCase {
private static final String XML = "com/altuure/spring/jgroups/ReplicatedHashMapTest.xml";
public static final int APPCOUNT=6;
private ClassPathXmlApplicationContext[] applicationContexts= null;

protected void setUp() throws Exception {
super.setUp();
applicationContexts= new ClassPathXmlApplicationContext[APPCOUNT];
for (int i = 0; i < applicationContexts.length; i++) {
applicationContexts[i]=new ClassPathXmlApplicationContext(XML);

}
}
public void testAll(){
Map map=
(Map) applicationContexts[0].getBean("com.altuure.spring.jgroups.ReplicatedHashMapFactory");
map.put("test1-Key","test1-Value");//put
map.put("test2-Key","test2-Value"); //put & update
map.put("test2-Key","test2-Value2");
map.put("test3-Key","test3-Value"); //put & remove
map.remove("test3-Key");

for (int i = 0; i < applicationContexts.length; i++) {
ClassPathXmlApplicationContext applicationContext = applicationContexts[i];
Map mapInstance=(Map) applicationContexts[i].getBean("com.altuure.spring.jgroups.ReplicatedHashMapFactory");
assertEquals("test1-Value",map.get("test1-Key"));
assertEquals("test2-Value2",map.get("test2-Key"));
assertNull(map.get("test3-Key"));

}

}

protected void tearDown() throws Exception {
super.tearDown();
for (int i = 0; i < applicationContexts.length; i++) {
applicationContexts[i].close();

}
}
}

source files :jgroups.zip

and your comments…

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Wists
  • LinkedIn
  • Slashdot
  • Google Bookmarks
  • Reddit
  • StumbleUpon
  • email
  • Twitter

Related posts:

  1. DistributableEventPublisher for Spring via JGroups
  2. light-weight distributed messaging via JGroups
  3. JGroups Basics
  4. Clustering Acegi via JGroups (DistributedHashtable)
  5. Spring is totally losing its invisibility

You can leave a response, or trackback from your own site.

Leave a Reply