001 /** 002 * 003 * Copyright 2004 Protique Ltd 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 **/ 018 019 package org.activemq.transport.multicast; 020 import java.net.URI; 021 import java.net.URISyntaxException; 022 023 import javax.jms.JMSException; 024 025 import org.activemq.io.impl.DefaultWireFormat; 026 import org.activemq.message.ActiveMQTextMessage; 027 import org.activemq.message.Packet; 028 import org.activemq.message.PacketListener; 029 import org.activemq.util.IdGenerator; 030 031 import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean; 032 033 /** 034 * An agent used to discover other instances of a service 035 * 036 * @version $Revision: 1.1.1.1 $ 037 */ 038 public class MulticastTester implements PacketListener { 039 040 private static final IdGenerator idGenerator = new IdGenerator(); 041 public static final String DEFAULT_DISCOVERY_URI = "multicast://224.1.2.3:6066"; 042 043 private SynchronizedBoolean started = new SynchronizedBoolean(false); 044 private MulticastTransportChannel channel; 045 046 private URI uri; 047 private String localId = idGenerator.generateId(); 048 049 public static void main(String[] args) throws URISyntaxException, JMSException, InterruptedException { 050 051 MulticastTester tester = new MulticastTester(); 052 tester.setUri(new URI(DEFAULT_DISCOVERY_URI)); 053 054 if( args.length > 0 ) { 055 tester.setUri(new URI(args[0])); 056 } 057 if( args.length > 1 ) { 058 tester.setLocalId(args[1]); 059 } 060 061 tester.start(); 062 063 ActiveMQTextMessage message = new ActiveMQTextMessage(); 064 int counter = 0; 065 while( true ) { 066 message.setText("Message "+counter+" from "+tester.getLocalId()); 067 tester.send(message); 068 counter++; 069 Thread.sleep(1000); 070 } 071 072 } 073 074 private void send(ActiveMQTextMessage message) throws JMSException { 075 channel.asyncSend(message); 076 } 077 078 public void start() throws JMSException { 079 if (started.commit(false, true)) { 080 System.out.println("Opening: "+uri); 081 channel = new MulticastTransportChannel(new DefaultWireFormat(), uri); 082 channel.setClientID(localId); 083 channel.setPacketListener(this); 084 channel.start(); 085 } 086 } 087 088 public void stop() throws JMSException { 089 if (started.commit(true, false)) { 090 channel.stop(); 091 } 092 } 093 094 public void consume(Packet packet) { 095 if( packet instanceof ActiveMQTextMessage ) { 096 try { 097 System.out.println("Received Text Packet: "+((ActiveMQTextMessage)packet).getText()); 098 } catch (JMSException e) { 099 e.printStackTrace(); 100 } 101 } else { 102 System.out.println("Received Unknown Packet: "+packet); 103 } 104 } 105 /** 106 * @return Returns the localId. 107 */ 108 public String getLocalId() { 109 return localId; 110 } 111 /** 112 * @param localId The localId to set. 113 */ 114 public void setLocalId(String localId) { 115 this.localId = localId; 116 } 117 /** 118 * @return Returns the uri. 119 */ 120 public URI getUri() { 121 return uri; 122 } 123 /** 124 * @param uri The uri to set. 125 */ 126 public void setUri(URI uri) { 127 this.uri = uri; 128 } 129 130 }