Did you know that if you have a JBoss cluster, HA singletons service beans on each can share a common memory state? State is a memory space shared by all HA singletons service beans in a cluster. It is possible save an object to the state using HA singleton service bean on one node, and to retrieve that object on another node.
The implementation is very easy. Lets assume you have two nodes in a cluster, on each node you have HA singleton service bean running. Lets call them “service A” and “service B“. Your service beans should extend from HASingletonSupport class. HASingletonSupport in its turn extends from HAServiceMBeanSupport. The latter gives you access to two methods:
public void setDistributedState(String key, Serializable value) throws Exception
and
public Serializable getDistributedState(String key)
These are convenience which allow you to save and retrieve objects from shared state. It can be whatever you want – primitive data types or even POJOs.
Lets say somewhere inside you service A, you have the following method:
public void setObjectToSharedState() {
String key = "test-1";
String value = "Testing memory state";
try {
this.setDistributedState(key, value);
}
catch (Exception e) {
e.printStackTrace();
}
}
Now, what has happened is that you, by using your service A saved into the shared memory a String value, under a key name “test-1“. This key name you can use in your service B to retrieve its corresponding value from the shared memory, in this case a String. Lets say the following method is sitting somewhere in your service B bean, on another node in the cluster:
public void getObjectFromSharedState() {
String key = "test-1";
String value = (String) this.getDistributedState(key);
System.out.println("The value is: " + value);
}
By specifying a key name, you can retrieve back from the shared memory what ever is saved under that key. You just have to know the type to cast it back to.
Also keep in mind: if you specify wrong key name, the retrieved object will be null.
Shared memory state is another option, if you want to share something between your nodes, and for some reason you don’t want to use JBoss cache.
loading...
Related posts:
- JBoss Clustering – How Many Nodes in the Cluster?
If you want to know how many nodes there are in the current cluster partition, all you have to do is to ask... - JBoss Clustering Architecture – Distributed Replicant Manager
My understanding of Distributed Replicant Manager (DRM) is that it allows you to attach some serialized data (stub) to a cluster node and... - JBoss Clustering – HASingleton Service
Have you ever dealt with clustered singleton service? How to determine which cluster node is the master? Well, if I am the current... - Deployment of MBean Separately to Its Interface
Few days ago i came across a little nasty thing during mbean deployment. What I did was separation of my mbean class and... - Using Template to Deploy a JBoss Queue
Currently I am involved in a project, where I have to use Velocity template engine to deploy queues and message-driven beans to JBoss....
