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 ; 10 import java.util.Vector ; 11 12 23 public class RpcDispatcherShunTest implements MembershipListener, RequestHandler { 24 JChannel channel; 25 RpcDispatcher disp; 26 27 28 public static void main(String [] args) { 29 String props=null; 30 for(int i=0; i < args.length; i++) { 31 String 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 e) { 43 e.printStackTrace(); 44 } 45 } 46 47 private void start(String props) throws IOException , ChannelException { 48 channel=new JChannel(props); 49 channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE); 50 disp=new RpcDispatcher(channel, null, this, this, 51 false, true); channel.connect("MessageDispatcherTestGroup"); 54 mainLoop(); 55 } 56 57 private void mainLoop() throws IOException { 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 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 members=new Vector (v.getMembers()); 99 System.out.println("sending to " + members); 100 rsp_list=disp.callRemoteMethods(members, "helloWorld", null, (String [])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 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 |