JGroup
JGroups is a toolkit for reliable multicast communication.
(Note that this doesn't necessarily mean IP Multicast, JGroups can also use transports such as TCP).
It can be used to create groups of processes whose members can send messages to each other. The main features include
Flexible Protocol Stack
The most powerful feature of JGroups is its flexible protocol stack, which allows developers to adapt it to exactly match their application requirements and network characteristics.
The benefit of this is that you only pay for what you use. By mixing and matching protocols, various differing application requirements can be satisfied.
JGroups comes with a number of protocols (but anyone can write their own), for example
Application Programming Interface
The API of JGroups is very simple (similar to a UDP socket). The code is always the same, regardless of the protocol stack used.
To be able to send/receive messages, a channel has to be created. The reliability of a channel is specified via XML, which then causes the creation of the underlying protocol stack.
The example below creates a channel and sends/receives 1 message:
JChannel channel=new JChannel("/home/bela/udp.xml");
channel.setReceiver(new ReceiverAdapter() {
public void receive(Message msg) {
System.out.println("received msg from " + msg.getSrc() + ": " + msg.getObject());
}
});
channel.connect("MyCluster");
channel.send(new Message(null, null, "hello world"));
channel.close();
The channel's properties are specified in the constructor, in this case we refer to an XML file with an absolute path. If we use a relative path, then the file is looked up on the classpath.
The XML file contains a list of protocols to be used by the new channel. Protocols included might be transport, discovery, fragmentation, reliable transmission, flow control and state transfer.
To join a cluster, connect() is called. It returns when the member has successfully joined the cluster named "MyCluster", or when it has created a new cluster (if it is the first member).
Then a message is sent using the send() method. A message contains the receiver's address ('null' = all group members), the sender's address ('null', will be filled in by the protocol stack before sending the message) and a byte buffer. In the example, the String "hello world" is set to be the message's contents. It is serialized into the message's byte buffer.
Since the message is sent to all members, the sender will also receive it (unless the socket option 'receive own multicasts' is set to 'true'). This is done via the receive() callback, which was registered with the channel before.
Finally, the member closes the channel and thus leaves the cluster. This results in a notification being sent to all members who are registered for membership change notifications.