KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: RpcDispatcherBlocking.java,v 1.6 2004/07/30 04:44:52 jiwils Exp $
2

3 package org.jgroups.tests;
4
5 import org.jgroups.*;
6 import org.jgroups.blocks.GroupRequest;
7 import org.jgroups.blocks.RpcDispatcher;
8 import org.jgroups.util.RspList;
9 import org.jgroups.util.Util;
10
11
12
13
14 /**
15  * Tests synchronous group RPCs. 2 main test cases:
16  * <ol>
17  * <li>Member crashes during invocation of sync group RPC: start 3 instances (A,
18  * B,C), set the timeout to be 30 seconds. Then invoke a sync group RPC by A
19  * (press 's' in A's window). A,B and C should receive the RPC. Now kill C.
20  * After some time, A's method call should return and show A's and B's reply to
21  * be valid, while showing C's response marked as suspected.
22  * <li>Member joins group during synchronous group RPC: start A and B with
23  * timeout=30000. Invoke a sync group RPC on A. Start C. A and B should
24  * <em>not</em> receive the view change <em>before</em> the group RPC has
25  * returned with A's and B's results. Therefore A and B should <em>not</em> wait
26  * for C's response, which would never be received because C never got the RPC
27  * in the first place. This would block A forever.
28  * </ol>
29  *
30  * @author bela Dec 19, 2002
31  */

32 public class RpcDispatcherBlocking implements MembershipListener {
33     RpcDispatcher disp;
34     Channel channel;
35     long timeout=30000;
36     String JavaDoc props=null;
37     int i=0;
38
39
40     public RpcDispatcherBlocking(String JavaDoc props, long timeout) {
41         this.props=props; this.timeout=timeout;
42     }
43
44
45     public void print(int i) throws Exception JavaDoc {
46         System.out.println("<-- " + i + " [sleeping for " + timeout + " msecs");
47         Util.sleep(timeout);
48     }
49
50
51     public void viewAccepted(View new_view) {
52         System.out.println("new view: " + new_view);
53     }
54
55
56     /** Called when a member is suspected */
57     public void suspect(Address suspected_mbr) {
58         System.out.println(suspected_mbr + " is suspected");
59     }
60
61
62     /** Block sending and receiving of messages until viewAccepted() is called */
63     public void block() {
64         
65     }
66
67
68     public void start() throws Exception JavaDoc {
69         int c;
70         RspList rsps;
71
72         channel=new JChannel(); // default props
73
disp=new RpcDispatcher(channel, null, this, this);
74     channel.connect("rpc-test");
75         
76         while(true) {
77             System.out.println("[x]: exit [s]: send sync group RPC");
78             System.out.flush();
79             c=System.in.read();
80             switch(c) {
81             case 'x':
82                 channel.close();
83                 disp.stop();
84                 return;
85             case 's':
86                 rsps=sendGroupRpc();
87                 System.out.println("responses:\n" + rsps);
88                 break;
89             }
90             
91             System.in.skip(System.in.available());
92         }
93     }
94
95
96     RspList sendGroupRpc() throws Exception JavaDoc {
97         return disp.callRemoteMethods(null, "print", new Object JavaDoc[]{new Integer JavaDoc(i++)}, new Class JavaDoc[] {int.class},
98                 GroupRequest.GET_ALL, 0);
99     }
100
101
102     public static void main(String JavaDoc[] args) {
103         long timeout=30000;
104         String JavaDoc props=null;
105
106         for(int i=0; i < args.length; i++) {
107             if("-props".equals(args[i])) {
108                 props=args[++i];
109                 continue;
110             }
111             if("-timeout".equals(args[i])) {
112                 timeout=Long.parseLong(args[++i]);
113                 continue;
114             }
115             help();
116             return;
117         }
118         
119
120
121         try {
122             new RpcDispatcherBlocking(props, timeout).start();
123         }
124         catch(Exception JavaDoc ex) {
125             System.err.println(ex);
126         }
127     }
128
129
130     static void help() {
131         System.out.println("RpcDispatcherBlocking [-help] [-props <properties>] [-timeout <timeout>]");
132     }
133 }
134
Popular Tags