How to create and use my own distributed locks?
drathnow Jan 7, 2013 5:51 PMWe are planning to run a cluster of JBoss AS7 servers that accept data from, and send data to, remote devices. These devices are industrial monitoring devices that collect data from oil and gas wells. Handling incoming data is simple since we can easily distribute the connection requests to the server instances in the cluster. However, the outgoing part is bit more complicated because only one server can send data to a remote device at any given time. To maintain high availability, outgoing data is delivered to all servers via a JMS topic and from there, the servers need to decide which one will send the data. If any one of the servers goes down, we want the other servers in the cluster to take over without any human intervention.
My thought was to use JGroups distributed locking feature to create a named Lock, something like "device@<ip-address>", and then call "tryLock" to see if the lock can be aquired. If so, then the server that gets the lock takes responsibility for sending the data. If not, then it assumes one of the other server nodes will deal with it.
I've been searching high and low to find examples of how to use the JGroups Distributed Lock manager from inside JBoss but so far I haven't found anything. The only example I've been able to find has come from the JGroups documentation, which looks something like this:
JChannel jchannel = JChannel("/home/bela/locking.xml");
LockService lockService = new LockService(jchannel);
jchannel.connect("lock-cluster");
Lock lock=lock_service.getLock("device@.1.2.3.4");
if (lock.tryLock())
try {
// Send outgoing data
} finally {
lock.unlock();
}
However, this isn't going to work with JBoss since JGroup's configuration is all done in domain.xml.
So I have the following questions:
1. Will the approach I'm proposing work?
2. How can I get JChannel from application running inside the JBoss container? Is there a way to inject a JChannel or do I have to look it up?
3. In the example above, I'm not sure what the jchannel.connect("lock-cluster") is doing. Is it needed from inside JBoss? What is the "lock-cluster" name?