1 3 package org.jgroups.tests; 4 5 6 import org.jgroups.Channel; 7 import org.jgroups.JChannel; 8 import org.jgroups.Message; 9 import org.jgroups.blocks.GroupRequest; 10 import org.jgroups.blocks.MessageDispatcher; 11 import org.jgroups.blocks.RequestHandler; 12 import org.jgroups.util.RspList; 13 import org.jgroups.util.Util; 14 15 16 20 public class MessageDispatcherTest implements RequestHandler { 21 Channel channel; 22 MessageDispatcher disp; 23 RspList rsp_list; 24 String props=null; 25 26 27 public void start() throws Exception { 28 channel=new JChannel(props); 29 disp=new MessageDispatcher(channel, null, null, this, 32 false, true); channel.connect("MessageDispatcherTestGroup"); 35 36 45 MyThread t1=new MyThread("one"), t2=new MyThread("two"); 46 t1.start(); 47 t2.start(); 48 t1.join(); 49 t2.join(); 50 51 System.out.println("** Disconnecting channel"); 52 channel.disconnect(); 53 System.out.println("** Disconnecting channel -- done"); 54 55 System.out.println("** Closing channel"); 56 channel.close(); 57 System.out.println("** Closing channel -- done"); 58 59 System.out.println("** disp.stop()"); 60 disp.stop(); 61 System.out.println("** disp.stop() -- done"); 62 63 } 67 68 69 class MyThread extends Thread { 70 public MyThread(String name) { 71 setName(name); 72 } 73 74 public void run() { 75 for(int i=0; i < 10; i++) { 76 System.out.println('[' + getName() + "] casting message #" +i); 77 rsp_list=disp.castMessage(null, 78 new Message(null, null, '[' + getName() + "] number #" + i), 79 GroupRequest.GET_ALL, 0); 80 System.out.println('[' + getName() + "] responses:\n" + rsp_list); 81 } 82 } 83 } 84 85 86 public Object handle(Message msg) { 87 System.out.println("handle(): " + msg.getObject()); 88 Util.sleepRandom(2000); 89 return "Success !"; 90 } 91 92 93 public static void main(String [] args) { 94 95 try { 96 new MessageDispatcherTest().start(); 97 } 98 catch(Exception e) { 99 System.err.println(e); 100 } 101 } 102 } 103 | Popular Tags |