import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.ReceiverAdapter;
import org.jgroups.View;
public class GroupCommunicationExample extends ReceiverAdapter {
private JChannel channel;
public GroupCommunicationExample(String clusterName) throws Exception {
channel = new JChannel();
channel.setReceiver(this);
channel.connect(clusterName);
}
public void sendMessage(String message) throws Exception {
Message msg = new Message(null, null, message);
channel.send(msg);
}
@Override
public void receive(Message message) {
System.out.println("Received message: " + message.getObject());
}
@Override
public void viewAccepted(View newView) {
System.out.println("New cluster view: " + newView);
}
public static void main(String[] args) {
try {
GroupCommunicationExample example = new GroupCommunicationExample("MyCluster");
example.sendMessage("Hello world");
} catch (Exception e) {
e.printStackTrace();
}
}
}
<config xmlns="urn:org:jgroups"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:org:jgroups http://www.jgroups.org/schema/jgroups.xsd">
<TCP bind_addr="127.0.0.1" bind_port="7800"
enable_diagnostics="false" thread_pool.min_threads="2" thread_pool.max_threads="4"/>
<TCPPING timeout="3000" initial_hosts="${jgroups.tcpping.initial_hosts:localhost[7800],localhost[7801]}" port_range="10"/>
<MERGE2 min_interval="10000" max_interval="30000"/>
<FD_SOCK/>
<FD_ALL timeout="3000" interval="1000" timeout_check_interval="2000"/>
</config>