JGroups Basics
Sunday, December 9, 2007 16:24JGroups is a basic networking api for java.It handles all networking issues for u with very complex configuration and simple interfaces.Here are some basic introduction for JGroups
Interfaces
Channel: is your connector to the network peers it detects peers and make cross connections if necessary
View: is your currently your Network Peer List Container
eg: ViewDemo
public class ViewDemo extends ReceiverAdapter {
private Channel channel;
public void viewAccepted(View new_view) {
System.out.println(”** New view: ” + new_view.getMembers());
}
/**
* Called when a member is suspected
*/
public void suspect(Address suspected_mbr) {
System.out.println(”Suspected(” + suspected_mbr + ‘)’);
}
public void start(String props) throws Exception {
channel=new JChannel(props);
channel.setReceiver(this);
channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
channel.connect(”ViewDemo”);
while(true) {
Thread.sleep(10000);
}
}public static void main(String args[]) throws Exception {
ViewDemo t=new ViewDemo();
String props=”udp.xml”;
t.start(props);
}
}
Configurations:
there are a list of out of box configuration in jgroups.jar/jgroups-all.jar you should check those before you use the. some of them use udp,encryption and and direct tcp connection
eg:MessageDispatcherTest
import org.jgroups.Channel;
import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.blocks.GroupRequest;
import org.jgroups.blocks.MessageDispatcher;
import org.jgroups.blocks.RequestHandler;
import org.jgroups.util.RspList;public class MessageDispatcherTest implements RequestHandler {
public final long RANDOM=Math.round(Math.random()*1000);
Channel channel;
MessageDispatcher disp;
RspList rsp_list;
String props; // to be set by application programmerpublic void start() throws Exception {
channel = new JChannel(props);
disp = new MessageDispatcher(channel, null, null, this);
channel.connect(”MessageDispatcherTestGroup”);
Thread.sleep(3000);
for (int i = 0; i < 30; i++) {
Thread.sleep(300);System.out.println(RANDOM+”:Casting message #” + i);
rsp_list = disp.castMessage(null,
new Message(null, null, new String(RANDOM+”Number #” + i)),
GroupRequest.GET_ALL, 0);
System.out.println(RANDOM+”:Responses:\n” + rsp_list);
}
channel.close();
disp.stop();
}public Object handle(Message msg) {
System.out.println(RANDOM+”:handle(): ” );
return new String(”Success !”+RANDOM);
}public static void main(String[] args) {
try {Runnable runnable = new Runnable() {
public void run() {
try {
new MessageDispatcherTest().start();
} catch (Exception e) {
e.printStackTrace();
}
}
};
new Thread(runnable).start();
new Thread(runnable).start();
}
catch (Exception e) {
System.err.println(e);
}
}
}
out:
….
95:handle():
893:handle():
95:handle():
893:Responses:
[sender=192.168.2.2:4608, retval=Success !893, received=true, suspected=false]
[sender=192.168.2.2:4607, retval=Success !95, received=true, suspected=false]95:Responses:
[sender=192.168.2.2:4608, retval=Success !893, received=true, suspected=false]
[sender=192.168.2.2:4607, retval=Success !95, received=true, suspected=false]
….
And here is your first broadcast implemenation
Additional Link:











