next up previous contents
Next: Promise Up: Dispatcher Previous: Dispatcher

Example

The example code below does essentially the same as the examples in sections 2.2.1 and 2.2.6.

public class DispatcherTestChannel {
    
    public Date GetDate() {return new Date();}

    public static void main(String args[]) {
        DispatcherTestChannel obj1;
        Dispatcher            disp=new Dispatcher();

        try {
            obj1=new DispatcherTestChannel();
            disp.Join("GroupA", obj1);

            while(true) {
                System.out.println("Sending method");
                d=disp.SendGetFirst("GroupA", "GetDate", null, 3000);
                if(d != null)
                    System.out.println("Received response: " + d);
                Thread.currentThread().sleep(2000);
            }
        }
        catch(Exception e) {System.err.println(e);}
    }
}

The code demonstrates that an object acting in the server role (offering method GetDate) can at the same time also act in the client role by invoking GetDate on all member dispatchers of the group and displaying the first returned result.

By creating a dispatcher, we are able to invoke remote methods on all members of a group (in this case "GroupA") and receiving return values. By joining a group, we are additionally able to act as a server for method GetDate.

It is clearly seen here that the value of a dispatcher lies in the simplicity with which methods can be invoked; providers (servers) of methods do not have to receive messages, find out the correct method to invoke, generate a result and use a transport to send the result back to the caller. Instead, all of this is automatically performed by the Dispatcher class.



Bela Ban
1998-08-06