1 3 package org.jgroups.tests; 4 5 6 import org.jgroups.*; 7 8 9 13 public class ChannelTest implements Runnable { 14 private Channel channel=null; 15 private Thread mythread=null; 16 private boolean looping=true; 17 18 19 public void start() throws Exception { 20 channel=new JChannel(); 21 channel.connect("ExampleGroup"); 22 mythread=new Thread (this); 23 mythread.start(); 24 for(int i=0; i < 30; i++) { 25 System.out.println("Casting msg #" + i); 26 channel.send(new Message(null, null, "Msg #" + i)); 27 Thread.sleep(1000); 28 } 29 channel.disconnect(); 30 channel.close(); 31 looping=false; 32 mythread.interrupt(); 33 mythread.join(1000); 34 } 35 36 37 public void run() { 38 Object obj; 39 Message msg; 40 while(looping) { 41 try { 42 obj=channel.receive(0); if(obj instanceof View) 44 System.out.println("--> NEW VIEW: " + obj); 45 else if(obj instanceof Message) { 46 msg=(Message)obj; 47 System.out.println("Received " + msg.getObject()); 48 } 49 } 50 catch(ChannelNotConnectedException conn) { 51 break; 52 } 53 catch(Exception e) { 54 System.err.println(e); 55 } 56 } 57 } 58 59 60 public static void main(String args[]) { 61 ChannelTest test; 62 try { 63 test=new ChannelTest(); 64 test.start(); 65 } 66 catch(Exception e) { 67 System.err.println(e); 68 } 69 } 70 71 } 72 | Popular Tags |