1 2 3 package org.jgroups.tests; 4 5 6 import org.jgroups.Channel; 7 import org.jgroups.JChannel; 8 import org.jgroups.blocks.GroupRequest; 9 import org.jgroups.blocks.RpcDispatcher; 10 import org.jgroups.util.RspList; 11 import org.jgroups.util.Util; 12 13 14 25 public class RpcDispatcherSimpleTest { 26 Channel channel; 27 RpcDispatcher disp; 28 RspList rsp_list; 29 String props=null; 30 31 32 public int print(int number) throws Exception { 33 System.out.println("print(" + number + ')'); 34 return number * 2; 35 } 36 37 38 public void start(int num, long interval) throws Exception { 39 channel=new JChannel(props); 40 channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE); 41 disp=new RpcDispatcher(channel, null, null, this); 42 channel.connect("RpcDispatcherTestGroup"); 43 44 for(int i=0; i < num; i++) { 45 Util.sleep(interval); 46 rsp_list=disp.callRemoteMethods(null, "print", new Object []{new Integer (i)}, 47 new Class []{int.class}, GroupRequest.GET_ALL, 0); 48 System.out.println("Responses: " + rsp_list); 49 } 50 System.out.println("Closing channel"); 51 channel.close(); 52 System.out.println("Closing channel: -- done"); 53 54 System.out.println("Stopping dispatcher"); 55 disp.stop(); 56 System.out.println("Stopping dispatcher: -- done"); 57 } 58 59 60 public static void main(String [] args) { 61 int num=10; 62 long interval=1000; 63 for(int i=0; i < args.length; i++) { 64 if(args[i].equals("-num")) { 65 num=Integer.parseInt(args[++i]); 66 continue; 67 } 68 if(args[i].equals("-interval")) { 69 interval=Long.parseLong(args[++i]); 70 continue; 71 } 72 help(); 73 return; 74 } 75 76 try { 77 new RpcDispatcherSimpleTest().start(num, interval); 78 } 79 catch(Exception e) { 80 System.err.println(e); 81 } 82 } 83 84 private static void help() { 85 System.out.println("RpcDispatcherTest [-help] [-num <number of msgs>] [-interval <sleep in ms between calls>]"); 86 } 87 } 88 | Popular Tags |