JGroups
| Company type | Software library |
|---|---|
| Industry | Computer software |
| Products | Reliable multicast communication library for Java |
JGroups is a Java-based library designed for dependable communication between one sender and one or multiple recipients, facilitating the formation of process groups that share messages. It enables reliable multicast communication, thereby abstracting the complexity of reliability from developers and allowing seamless deployment across various platforms without altering the source code, ultimately accelerating development cycles.
Features
- Creation and removal of groups over LANs or WANs
- Ability to join and exit groups
- Monitoring group membership with notifications for members who join, depart, or fail
- Identification and elimination of crashed members
- Messaging capabilities supporting both multicast (member-to-group) and unicast (member-to-member) communication
Code sample
The following snippet illustrates a basic command-line IRC client implementation utilizing JGroups:
<syntaxhighlight lang=Java> public class Chat extends ReceiverAdapter {
private JChannel channel;
public Chat(String props, String name) {
channel = new JChannel(props)
.setName(name)
.setReceiver(this)
.connect("ChatCluster");
}
public void viewAccepted(View view) {
System.out.printf("** view: %s\n", view);
}
public void receive(Message msg) {
System.out.printf("from %s: %s\n", msg.getSource(), msg.getObject());
}
private void send(String line) {
try {
channel.send(new Message(null, line));
} catch (Exception e) {}
}
public void run() throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.print("> ");
System.out.flush();
send(in.readLine().toLowerCase());
}
}
public void end() throws Exception {
channel.close();
}
public static void start(Chat client) throws Exception {
try {
client.run();
} catch (Exception e) {
} finally {
client.end();
}
}
public static void main(String[] args) throws Exception {
String props = "udp.xml";
String name;
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;
}
start(new Chat(props, name)); }
} </syntaxhighlight>
A JChannel instance is instantiated using an XML configuration file (e.g., ), which acts as the interface for joining a cluster. Assigning a receiver enables handling callbacks related to membership updates () and incoming messages (). By connecting to the "ChatCluster," the channel facilitates message transmission and keeps cluster membership views current.
This chat program operates identically whether running on a single device, within local networks, across different network segments, or in cloud infrastructures—requiring no code modifications, only configuration adjustments. For instance, local networks leverage IP multicast; environments without multicast support utilize TCP transport; cloud setups combine TCP with cloud-specific discovery protocols.
Adaptable protocol stack
A major advantage of JGroups lies in its adaptable protocol stack, which allows developers to customize it precisely according to the specific requirements of their applications and network environments. This modular design promotes cost-effectiveness by incorporating only the protocols that are essential. Protocols provided encompass:
- Transport mechanisms: UDP (IP multicast), TCP
- Fragmentation support for large messages
- Discovery protocols to establish initial node membership
- Reliable unicast and multicast communication with lost message retransmission
- Failure detection to remove unresponsive members
- Message ordering options including FIFO and Total Order (using sequencer or token methods)
- Management of membership and event notifications
- Detection and resolution of network partitions (split brain)
- Flow control mechanisms
- Security features such as encryption and authentication (including SASL)
- Data compression
Core components
On top of JGroups channels, core components provide more advanced abstractions, including:
- Remote procedure calls to single or all cluster nodes
- Distributed caching systems
- Distributed locking mechanisms
- Atomic counters distributed across nodes
- Execution of tasks in a distributed manner