2017-09-23 117 views
0

我是ActiveMQ的新手。我想研究一下它是如何工作通過檢查Apache的這個鏈接提供的示例代碼: -ActiveMQ中BrokerService的用例以及如何正確使用它

http://activemq.apache.org/how-should-i-implement-request-response-with-jms.html

public class Server implements MessageListener { 
     private static int ackMode; 
     private static String messageQueueName; 
     private static String messageBrokerUrl; 

     private Session session; 
     private boolean transacted = false; 
     private MessageProducer replyProducer; 
     private MessageProtocol messageProtocol; 

     static { 
      messageBrokerUrl = "tcp://localhost:61616"; 
      messageQueueName = "client.messages"; 
      ackMode = Session.AUTO_ACKNOWLEDGE; 
     } 

     public Server() { 
      try { 
       //This message broker is embedded 
       BrokerService broker = new BrokerService(); 
       broker.setPersistent(false); 
       broker.setUseJmx(false); 
       broker.addConnector(messageBrokerUrl); 
       broker.start(); 
      } catch (Exception e) { 
       System.out.println("Exception: "+e.getMessage()); 
       //Handle the exception appropriately 
      } 

      //Delegating the handling of messages to another class, instantiate it before setting up JMS so it 
      //is ready to handle messages 
      this.messageProtocol = new MessageProtocol(); 
      this.setupMessageQueueConsumer(); 
     } 

     private void setupMessageQueueConsumer() { 
      ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(messageBrokerUrl); 
      Connection connection; 
      try { 
       connection = connectionFactory.createConnection(); 
       connection.start(); 
       this.session = connection.createSession(this.transacted, ackMode); 
       Destination adminQueue = this.session.createQueue(messageQueueName); 

       //Setup a message producer to respond to messages from clients, we will get the destination 
       //to send to from the JMSReplyTo header field from a Message 
       this.replyProducer = this.session.createProducer(null); 
       this.replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 

       //Set up a consumer to consume messages off of the admin queue 
       MessageConsumer consumer = this.session.createConsumer(adminQueue); 
       consumer.setMessageListener(this); 
      } catch (JMSException e) { 
       System.out.println("Exception: "+e.getMessage()); 
      } 
     } 

     public void onMessage(Message message) { 
      try { 
       TextMessage response = this.session.createTextMessage(); 
       if (message instanceof TextMessage) { 
        TextMessage txtMsg = (TextMessage) message; 
        String messageText = txtMsg.getText(); 
        response.setText(this.messageProtocol.handleProtocolMessage(messageText)); 
       } 

       //Set the correlation ID from the received message to be the correlation id of the response message 
       //this lets the client identify which message this is a response to if it has more than 
       //one outstanding message to the server 
       response.setJMSCorrelationID(message.getJMSCorrelationID()); 

       //Send the response to the Destination specified by the JMSReplyTo field of the received message, 
       //this is presumably a temporary queue created by the client 
       this.replyProducer.send(message.getJMSReplyTo(), response); 
      } catch (JMSException e) { 
       System.out.println("Exception: "+e.getMessage()); 
      } 
     } 

     public static void main(String[] args) { 
      new Server(); 
     } 
    } 

我對messageBrokerUrl =「TCP困惑://本地主機:61616 「;您知道默認情況下,ActiveMQ服務在端口61616上運行。爲什麼這個例子選擇相同的端口。如果我嘗試運行代碼thows eception爲: 異常:無法綁定到服務器套接字:tcp:// localhost:61616由於:java.net.BindException:地址已在使用中:JVM_Bind

也許如果我更改端口號,我可以執行代碼。

請讓我知道爲什麼它是這樣的例子以及如何使用BrokerService。

回答

0

本示例中的BrokerService正在嘗試創建一個用於此示例的內存ActiveMQ代理。鑑於你看到的錯誤,我猜你已經有一臺運行在綁定到端口61616的計算機上的ActiveMQ代理,因爲這是默認端口,因此兩者相沖突。您可以停止外部代理並運行示例,也可以修改示例以不運行嵌入式代理,並僅依賴外部代理實例。

嵌入式代理非常適合單元測試或創建不需要用戶安裝和運行代理的示例。

+0

Embedded BrokerService vs已安裝的ActiveMQ代理?功能是否相同或不同?還可以提及這兩種方式的優點和缺點 – masiboo

+0

這就是堆棧溢出的主題,我已經回答了您的基本問題,其他信息應該在ActiveMQ社區中提出要求。 –

相關問題