JMS com ActiveMQ

0 respostas
tonyam

estou testando um exemplo usando ActiveMQ para transporte de mensagem. Instalei o ActiveMQ 4.1.0 em uma maquina com IP: 192.168.200.43. Na minha aplicação ocorre esse erro:

java.io.IOException: Failed to bind to server socket: 
tcp://192.168.200.43:61616 due to: java.net.BindException: Cannot 
assign requested address: JVM_Bind

o código:

public class Receiver {

	private static String host  = "192.168.200.43";
	private static String port  = "61616";
	private static String queue_name = "SERVER_TEST";
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		
		thread(new StartBroker() , true);
		while (true){
			thread(new StartConsumer(), false);
			Thread.sleep(5000);
		}

	}
	
	public static void thread(Runnable runnable, boolean daemon){
		Thread brokerThread = new Thread(runnable);
		brokerThread.setDaemon(daemon);
		brokerThread.start();
	}
	
	public static class StartBroker implements Runnable {
		public StartBroker(){
			
		}
		
		public void run(){
			try {
				
				BrokerService broker = new BrokerService();
				broker.addConnector("tcp://"+host+":"+port+"");
				broker.start();
			} catch (Exception e) {
				System.out.println("Caugth "+ e);
				e.printStackTrace();
			}
		}
	}
	
	public static class StartConsumer implements Runnable, ExceptionListener{

		public StartConsumer(){
			
		}
		
		public void run() {
			try {
				ActiveMQConnectionFactory conneciontFactory = new ActiveMQConnectionFactory("tcp://"+host+":"+port+"");
				Connection connection = conneciontFactory.createConnection();
				
				connection.start();
				
				connection.setExceptionListener(this);
				
				Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
				
				Destination destination = session.createQueue(queue_name);
				
				MessageConsumer consumer = session.createConsumer(destination);
				
				Message message = consumer.receive(1000);
				
				onMessage(message);
				
				consumer.close();
				session.close();
				connection.close();
				
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}

		
		public void onMessage(Message jmsMessage){
			try {
				if (jmsMessage instanceof TextMessage ){
					
					TextMessage textMessage = (TextMessage) jmsMessage;
					String text = textMessage.toString();
					System.out.println("Received: "+text);
					
					if (text.contains("address")){
						System.out.println(">>> Sending");
						BkgProcess bp = new BkgProcess(text.substring(8));
						bp.startBkgProcess();
					}
					
					
				}
			}catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
		public void onException(JMSException arg0) {
			System.out.println("JMS Excepion ocurred. Shuting down client.");
			
		}
		
	}
Criado 13 de fevereiro de 2007
Respostas 0
Participantes 1