KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Vector JavaDoc;
10
11 /**
12  * Tests cluster method invocations on disconnected and connected services
13  * @author Bela Ban
14  * @version $Id: RpcDispatcherMultiplexerTest.java,v 1.5 2006/09/01 07:44:16 belaban Exp $
15  */

16 public class RpcDispatcherMultiplexerTest implements MembershipListener, RequestHandler, ChannelListener {
17     Channel channel;
18     JChannelFactory factory=null;
19     RpcDispatcher disp;
20     String JavaDoc props=null;
21     boolean spurious_channel_created=false;
22
23
24     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
25         String JavaDoc props=null;
26         for(int i=0; i < args.length; i++) {
27             String JavaDoc arg=args[i];
28             if(arg.equals("-props")) {
29                 props=args[++i];
30                 continue;
31             }
32             help();
33             return;
34         }
35         new RpcDispatcherMultiplexerTest().start(props);
36     }
37
38     private void start(String JavaDoc props) throws Exception JavaDoc {
39         if(factory == null)
40             factory=new JChannelFactory();
41         if(props == null)
42             this.props="stacks.xml";
43         else
44             this.props=props;
45         factory.setMultiplexerConfig(this.props);
46         channel=factory.createMultiplexerChannel("udp", "MyId");
47
48         if(!spurious_channel_created) {
49             // create one additional channel so that we don't close the JGroups channel when disconnecting from MuxChannel !
50
Channel tmp=factory.createMultiplexerChannel("udp", "tempChannel");
51             tmp.connect("bla");
52             spurious_channel_created=true;
53         }
54
55         channel.addChannelListener(this);
56         channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
57         disp=new RpcDispatcher(channel, null, this, this,
58                                false, // deadlock detection is disabled
59
true); // concurrent processing is enabled
60
channel.connect("MessageDispatcherTestGroup");
61         mainLoop();
62     }
63
64
65
66     private void stop() {
67         disp.stop();
68         channel.close();
69     }
70
71     private void mainLoop() throws Exception JavaDoc {
72         while(true) {
73             int c;
74             System.in.skip(System.in.available());
75             System.out.println("\n[1] Send [2] Start service [3] Stop service [4] Print view [q] Quit");
76             c=System.in.read();
77             switch(c) {
78             case -1:
79                 break;
80             case '1':
81                 invokeGroupMethod();
82                 break;
83             case '2':
84                 start(this.props);
85                 break;
86             case '3':
87                 stop();
88                 break;
89             case '4':
90                 View v=channel.getView();
91                 System.out.println("View: " + v);
92                 break;
93             case 'q':
94                 channel.close();
95                 return;
96             default:
97                 break;
98             }
99         }
100     }
101
102     public Object JavaDoc helloWorld() {
103         System.out.println("method helloWorld() was called");
104         return "same to you";
105     }
106
107
108     private void invokeGroupMethod() {
109         RspList rsp_list;
110         View v=channel.getView();
111         if(v == null)
112             return;
113         Vector JavaDoc members=new Vector JavaDoc(v.getMembers());
114         System.out.println("sending to " + members);
115         rsp_list=disp.callRemoteMethods(members, "helloWorld", null, (String JavaDoc[])null, GroupRequest.GET_ALL, 0);
116         System.out.println("responses:\n" + rsp_list);
117     }
118
119     private static void help() {
120         System.out.println("MessageDispatcherShunTest [-help] [-props <props>]");
121     }
122
123     public Object JavaDoc handle(Message msg) {
124         return "same to you";
125     }
126
127     public void viewAccepted(View new_view) {
128         System.out.println("-- view: " + new_view);
129     }
130
131     public void suspect(Address suspected_mbr) {
132     }
133
134     public void block() {
135     }
136
137
138     public void channelConnected(Channel channel) {
139         // System.out.println("-- channel connected");
140
}
141
142     public void channelDisconnected(Channel channel) {
143         // System.out.println("-- channel disconnected");
144
}
145
146     public void channelClosed(Channel channel) {
147         // System.out.println("-- channel closed");
148
}
149
150     public void channelShunned() {
151         // System.out.println("-- channel shunned");
152
}
153
154     public void channelReconnected(Address addr) {
155         // System.out.println("-- channel reconnected");
156
}
157 }
158
Popular Tags