JGroups - A Toolkit for Reliable Messaging


JGroups

Documentation

Download

Misc

Building Blocks

Channels have been designed to be simple. However, JGroups also provides high-level abstractions, so called building blocks, which can be used instead of a bare bone channel. An example is RpcDispatcher which allows applications to make remote group method calls:

public class RpcDispatcherTest {
    Channel       channel;
    RpcDispatcher disp;
    RspList       rsp_list;
    String        props="/home/bela/udp.xml"

    public int print(int number) {
        System.out.println("print(" + number + ")");
        return number * 2;
    }

    public void start() throws Exception {
        channel=new JChannel(props);
        disp=new RpcDispatcher(channel, null, null, this);
        channel.connect("RpcDispatcherTestGroup");
        for(int i=0; i < 100; i++) {
           Util.sleep(1000);
           rsp_list=disp.callRemoteMethods(null, "print", new Integer(i),
                                                  GroupRequest.GET_ALL,
                                                  0);
           System.out.println("Responses: " + rsp_list);
        }
        channel.close();
    }
}

As before, the example creates a channel off of an XML configuration. It defines a method print() which will be called by the RpcDispatcher. Then an instance of RpcDispatcher is created on top of the channel and the channel is connected (this joins the new member to the group). Now messages can be sent and received. But instead of sending/receiving messages using the channel, the application invokes a remote method call using RpcDispatcher's callRemoteMethods().

The first argument 'null' means send to all cluster nodes, "print" is the name of the method to be invoked, 'new Integer(i)' is the argument to the print() method, GET_ALL means wait until the responses from all group members have been received and '0' specifies the timeout (in this case, it means wait forever). RpcDispatcher sends a multicast message (containing the method call) to all members (e.g. 4 members, including itself) and waits for 4 replies. If one or more of the members crash in the meantime, the call nevertheless returns and has those replies marked as 'suspected' in the response list. The response list contains an entry for each expected reply, which has the address of the replier, the value (if any, in our case it is an integer), and a flag (received, not received (in case of timeouts) or suspected). If this member is the only group member, then the method call will call its own print() method.



Copyright © 2002-2035, Red Hat
Hosted by Get JavaGroups at SourceForge.net. Fast, secure and Free Open Source software downloads Red Hat Proud to useThe best Java IDE