1 package org.jgroups.tests; 2 3 import org.jgroups.*; 4 import org.jgroups.util.RspList; 5 import org.jgroups.blocks.MessageDispatcher; 6 import org.jgroups.blocks.GroupRequest; 7 import org.jgroups.blocks.RequestHandler; 8 9 import java.io.IOException ; 10 import java.util.Vector ; 11 12 23 public class MessageDispatcherShunTest implements MembershipListener, RequestHandler { 24 JChannel channel; 25 MessageDispatcher 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 MessageDispatcherShunTest().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 MessageDispatcher(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 sendMessage(); 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 private void shun() { 86 System.out.println("shunning this member"); 87 channel.up(new Event(Event.EXIT)); 88 } 89 90 private void sendMessage() { 91 RspList rsp_list; 92 Message msg=new Message(null, null, "Hello world"); 93 View v=channel.getView(); 94 Vector members=new Vector (v.getMembers()); 95 System.out.println("sending to " + members); 96 rsp_list=disp.castMessage(members, msg, GroupRequest.GET_ALL, 0); 97 System.out.println("responses:\n" + rsp_list); 98 } 99 100 private static void help() { 101 System.out.println("MessageDispatcherShunTest [-help] [-props <props>]"); 102 } 103 104 public Object handle(Message msg) { 105 return "same to you"; 106 } 107 108 public void viewAccepted(View new_view) { 109 System.out.println("-- view: " + new_view); 110 } 111 112 public void suspect(Address suspected_mbr) { 113 } 114 115 public void block() { 116 } 117 } 118 | Popular Tags |