java/j2ee tips and blueprints
java/j2ee tips and blueprints
It is a very sad thing that nowadays
there is so little useless information.
Oscar Wilde
JGroups-Spring In Action
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
- ChannelBeanFactory for creating JChannel Instances
- 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…











