JGroups
JGroups is a library for reliable one-to-one or one-to-many communication written in the Java language.
Developer(s) | Bela Ban |
---|---|
Stable release | 4.1.3.Final
/ August 16, 2019 |
Written in | Java |
Operating system | Cross-platform |
Size | 2.1 MB |
Type | reliable multicast system |
License | Apache License 2.0 |
Website | http://www.jgroups.org/ |
It can be used to create groups of processes whose members send messages to each other. JGroups enables developers to create reliable multipoint (multicast) applications where reliability is a deployment issue. JGroups also relieves the application developer from implementing this logic themselves. This saves significant development time and allows for the application to be deployed in different environments without having to change code.
Features
- Group creation and deletion. Group members can be spread across LANs or WANs
- Joining and leaving of groups
- Membership detection and notification about joined/left/crashed members
- Detection and removal of crashed members
- Sending and receiving of member-to-group messages (point-to-multipoint)
- Sending and receiving of member-to-member messages (point-to-point)
Code sample
This code below shows how a simple chat application could be written using JGroups:
public class Chat extends ReceiverAdapter {
JChannel channel;
public void viewAccepted(View new_view) {
System.out.println("** view: " + new_view);
}
public void receive(Message msg) {
System.out.printf("from %s: %s\n", msg.getSource(), msg.getObject());
}
private void start(String props, String name) throws Exception {
channel = new JChannel(props).setName(name)
.setReceiver(this).connect("ChatCluster");
eventLoop();
channel.close();
}
private void eventLoop() {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (true) {
try {
System.out.print("> "); System.out.flush();
String line = in.readLine().toLowerCase();
Message msg = new Message(null, line);
channel.send(msg);
}
catch(Exception e) {
}
}
}
public static void main(String[] args) throws Exception {
String props = "udp.xml";
String name = null;
for (int i=0; i < args.length; i++) {
if (args[i].equals("-props")) {
props = args[++i];
continue;
}
if (args[i].equals("-name")) {
name = args[++i];
continue;
}
System.out.println("Chat [-props XML config] [-name name]");
return;
}
new Chat().start(props, name);
}
}
In start(), a JChannel is created from an XML configuration (e.g. udp.xml). The channel is the endpoint for joining a cluster.
Next, the Receiver is set, which means that 2 callbacks are going to be invoked:
- viewAccepted(View v) whenever a new member joins, or an existing member leaves the cluster
- receive(Message msg) when a message from some other cluster member is received
Then, the channel joins cluster "ChatCluster". From now, messages can be sent and received, plus a new view (including this member) will be installed in all cluster members (including the newly joined member).
Anything typed in the main loop results in the creation of a Message that's sent to all cluster members, including the sender.
Instances of the chat application can be run in the same process, on the same box, on different hosts in the local network, on hosts in different networks, or in the cloud. The code remains the same; the only thing that needs to be changed is the configuration.
For example, in a local network, IP multicasting might be used. When IP multicasting is disabled, TCP can be used as transport. When run in the cloud, TCP plus a cloud discovery protocol would be used and so on...
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
- Transport protocols: UDP (IP Multicast), TCP
- Fragmentation of large messages
- Discovery protocols to discover the initial membership for a joining node
- Reliable unicast and multicast message transmission. Lost messages are retransmitted
- Failure detection: crashed members are excluded from the membership
- Ordering protocols: Fifo, Total Order (sequencer or token based)
- Membership and notification of joined or crashed members
- Network partition (split brain) detection and merging
- Flow control
- Encryption and authentication (including SASL support)
- Compression
Building blocks
Building blocks are classes layered over JGroups channels, which provide higher-level abstractions such as
- RPCs to individual or all cluster nodes
- Distributed caches
- Distributed locks
- Distributed atomic counters
- Distributed task execution