Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
J

JGroups

> 编程语言
Open source

The JGroups project

1.1K stars0 likes1 views
WebsiteGitHub

About

The JGroups project

= JGroups :author: Bela Ban :toc2: :toclevels: 3 :icons: :homepage: http://www.jgroups.org :source-highlighter: pygments

== Overview link:$$http:///jgroups.org$$[JGroups] is a clustering library, allowing members to exchange messages.

It provides the following functionality:

  • Joining a given cluster (becoming a member)
  • (Once joined), sending messages to other members
  • Getting a list of the members in the same cluster
  • Registering callbacks that are invoked when ** a message is received ** a member joins ** a member leaves
  • Leaving a cluster

== Sample code

The code below shows how to send messages to all cluster members. Open two or more shells and run the demo with different arguments (names of the members), e.g. node1, node2 etc.

[source,java]

public class Demo { protected JChannel ch;

protected void start(String name) throws Exception {
    ch=new JChannel("udp.xml").name(name)
      .setReceiver(new MyReceiver(name))
      .connect("demo-cluster");
    int counter=1;
    for(;;) {
        ch.send(null, "msg-" + counter++);
        Util.sleep(3000);
    }
}

protected static class MyReceiver implements Receiver {
    protected final String name;

    protected MyReceiver(String name) {
        this.name=name;
    }

    public void receive(Message msg) {
        System.out.printf("-- [%s] msg from %s: %s\n", name, msg.src(), msg.getObject());
    }

    public void viewAccepted(View v) {
        System.out.printf("-- [%s] new view: %s\n", name, v);
    }
}

public static void main(String[] args) throws Exception {
    new Demo().start(args[0]);
}

}

JChannel is the handle to interact with JGroups. In start(), a new JChannel is created with configuration udp.xml (see >), which needs to be found on the classpath. Alternatively, a fully qualified pathname can be given, e.g. /Users/bela/tcp.xml.

The receiver is set to an instance of MyReceiver, which implements two callbacks: viewAccepted(), invoked when a member joins or leaves, and receive(), invoked when a message is received.

Finally, cluster "demo-cluster" is joined via JChannel.connect(). When this call returns, a member can start sending and receiving messages.

The main loop sends a message to all cluster members (including itself) at a given interval.

The output of running 3 members A, B and C might look like this:

[listing] ....

GMS: address=C, cluster=demo-cluster, physical address=192.168.1.106:59093

-- [C] new view: [A|6] (3) [A, B, C] -- [C] message from C: msg-1 -- [C] message from A: msg-15 -- [C] message from B: msg-5 -- [C] message from C: msg-2 -- [C] message from A: msg-16 ....

[[Configuration]] == Configuration A JChannel is created from an XML configuration (but can also be created programmatically, see the manual for details). The configuration contains a list of protocols. JChannel.send(Message msg) sends a message down the stack, passing each protocol, and the transport protocol (at the bottom) sends the message.

At the receiver, the transport protocol reads the message and passes it up the stack to the JChannel, which delivers it to the application (the receive(Message msg) callback).

A configuration might look as follows:

[source,xml]


It essentially contains the list of protocols. In the example above, UDP is the transport protocol and FRAG2 is the top protocol. Protocols have attributes, which govern the working of a protocol, e.g. xmit_interval in UNICAST3, which is the retransmission interval (in milliseconds) of messages, until they're acknowledged.

The list of protocols defines the quality of service of a given stack, ie. reliable retransmisson, FIFO or total ordering and so on.

JGroups ships with multiple sample configurations, e.g. tcp.xml, which uses TCP instead of UDP as transport protocol. Consult the manual (link below) for more details.

== Running the tests

The unit tests are executed with Maven:

[source,bash]

mvn test

This runs all TestNG suites (functional, encrypt, byteman, jdbc, stack-independent, time-sensitive, udp, udp-new, tcp, tcp-new, tcp-nio, tcp-nio-new).

To run a single suite:

[source,bash]

mvn surefire:test@functional

Replace functional with any of the suite names listed above.

== Links

  • Web site: link:$$http://jgroups.org$$[http://www.jgroups.org]
  • Manual: link:$$http://www.jgroups.org/ug.html$$[http://www.jgroups.org/ug.html]
  • JIRA: link:$$https://issues.redhat.com/projects/JGRP$$[https://issues.redhat.com/projects/JGRP]
  • Forum: link:$$https://groups.google.com/g/jgroups-dev$$[https://groups.google.com/g/jgroups-dev]

Bela Ban, Dec 2022

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Java

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言