KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > RpcDispatcherShunTest


1 package org.jgroups.tests;
2
3 import org.jgroups.*;
4 import org.jgroups.blocks.GroupRequest;
5 import org.jgroups.blocks.RequestHandler;
6 import org.jgroups.blocks.RpcDispatcher;
7 import org.jgroups.util.RspList;
8
9 import java.io.IOException JavaDoc;
10 import java.util.Vector JavaDoc;
11
12 /**
13  * Tests message sending and shunning.Use:
14  * <ol>
15  * <li>Start multiple instances
16  * <li>Send a few messages
17  * <li>Shun a member
18  * <li>Send a message from another member (must work !)
19  * </ol>
20  * @author Bela Ban
21  * @version $Id: RpcDispatcherShunTest.java,v 1.1 2005/01/10 11:25:41 belaban Exp $
22  */

23 public class RpcDispatcherShunTest implements MembershipListener, RequestHandler {
24     JChannel channel;
25     RpcDispatcher disp;
26
27
28     public static void main(String JavaDoc[] args) {
29         String JavaDoc props=null;
30         for(int i=0; i < args.length; i++) {
31             String JavaDoc arg=args[i];
32             if(arg.equals("-props")) {
33                 props=args[++i];
34                 continue;
35             }
36             help();
37             return;
38         }
39         try {
40             new RpcDispatcherShunTest().start(props);
41         }
42         catch(Exception JavaDoc e) {
43             e.printStackTrace();
44         }
45     }
46
47     private void start(String JavaDoc props) throws IOException JavaDoc, ChannelException {
48         channel=new JChannel(props);
49         channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
50         disp=new RpcDispatcher(channel, null, this, this,
51                 false, // deadlock detection is disabled
52
true); // concurrent processing is enabled
53
channel.connect("MessageDispatcherTestGroup");
54         mainLoop();
55     }
56
57     private void mainLoop() throws IOException JavaDoc {
58         while(true) {
59             int c;
60             System.in.skip(System.in.available());
61             System.out.println("\n[1] Send [2] Shun [3] Print view [q] Quit");
62             c=System.in.read();
63             switch(c) {
64             case -1:
65                 break;
66             case '1':
67                 invokeGroupMethod();
68                 break;
69             case '2':
70                 shun();
71                 break;
72             case '3':
73                 View v=channel.getView();
74                 System.out.println("View: " + v);
75                 break;
76             case 'q':
77                 channel.close();
78                 return;
79             default:
80                 break;
81             }
82         }
83     }
84
85     public Object JavaDoc helloWorld() {
86         System.out.println("method helloWorld() was called");
87         return "same to you";
88     }
89
90     private void shun() {
91         System.out.println("shunning this member");
92         channel.up(new Event(Event.EXIT));
93     }
94
95     private void invokeGroupMethod() {
96         RspList rsp_list;
97         View v=channel.getView();
98         Vector JavaDoc members=new Vector JavaDoc(v.getMembers());
99         System.out.println("sending to " + members);
100         rsp_list=disp.callRemoteMethods(members, "helloWorld", null, (String JavaDoc[])null, GroupRequest.GET_ALL, 0);
101         System.out.println("responses:\n" + rsp_list);
102     }
103
104     private static void help() {
105         System.out.println("MessageDispatcherShunTest [-help] [-props <props>]");
106     }
107
108     public Object JavaDoc handle(Message msg) {
109         return "same to you";
110     }
111
112     public void viewAccepted(View new_view) {
113         System.out.println("-- view: " + new_view);
114     }
115
116     public void suspect(Address suspected_mbr) {
117     }
118
119     public void block() {
120     }
121 }
122
Popular Tags