2015-11-03 242 views
3

我正在使用Stomp和ActiveMQ監聽來自lan的消息並將其發佈到某些應用程序。使用Websocket連接stomp和ActiveMQ

對於測試,我實現了使用tcp協議連接,我需要使用websocket協議。

我的ActiveMQ已經配置爲使用的WebSocket,請參見下面的代碼:

<!-- 
    The transport connectors expose ActiveMQ over a given protocol to 
    clients and other brokers. For more information, see: 

    http://activemq.apache.org/configuring-transports.html 
--> 
<transportConnectors> 
    <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB --> 
    <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/> 
    <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/> 
    <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/> 
    <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/> 
    <transportConnector name="ws" uri="ws://0.0.0.0:61623?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/> 
</transportConnectors> 

但是,如果使用的是WS連接不是爲我工作:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH:mm:ss.SSS"); 

String user = env("ACTIVEMQ_USER", "admin"); 
String password = env("ACTIVEMQ_PASSWORD", "password"); 
String host = env("ACTIVEMQ_HOST", "localhost"); 
int port = Integer.parseInt(env("ACTIVEMQ_PORT", "61623")); 
String destination = arg(args, 0, "/topic/event"); 
String protocol = "ws://"; 


StompJmsConnectionFactory factory = new StompJmsConnectionFactory(); 
factory.setBrokerURI(protocol + host + ":" + port); 

Connection connection = factory.createConnection(user, password); 
connection.start(); 
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
Destination dest = new StompJmsDestination(destination); 

MessageConsumer consumer = session.createConsumer(dest); 

我看了有關的一些示例WS連接使用StompJmsConnectionFactory類,但只能使用tcp連接。

有人已經實現了這樣的東西?

感謝

+0

後,我不認爲StompJMS支持WebSocket連接。爲什麼還要爲Web客戶端使用Java客戶端? –

回答

1

我已經使用ActiveMQ的與踐踏和WebSockets的從瀏覽器獲取數據。爲我工作的配置,除了頗爲相似:

  1. 在我的代碼我用String protocol = "tcp://";。它是與WebSockets(對瀏覽器?)進行通信的消息代理。您的Java應用程序通過tcp與消息代理進行通信。

  2. 我用了阿波羅消息中間件引擎與此配置

    <connector id="tcp" bind="tcp://0.0.0.0:61613" connection_limit="64"> 
    <detect protocols="openwire stomp" /> 
    </connector> 
    <connector id="ws" bind="ws://0.0.0.0:61623" connection_limit="16"> 
    <detect protocols="stomp" /> 
    </connector> 
    
  3. 我在最後叫connection.start();MessageConsumer已經建立

+0

謝謝Manos! –