簡體   English   中英

使用Websocket連接stomp和ActiveMQ

[英]Connect stomp and ActiveMQ using Websocket

我正在使用Stomp和ActiveMQ來監聽來自lan的消息並將其發布到某個應用程序。

為了測試,我實現了使用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);

我查找了一些關於使用StompJmsConnectionFactory類的WS連接的StompJmsConnectionFactory ,但只有tcp連接。

有人已經實現了這樣的事情嗎?

謝謝

我已經使用ActiveMQ與Stomp和WebSockets從瀏覽器獲取數據。 對我有用的配置非常相似,除了:

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

  2. 我在這個配置中使用了Apollo消息代理引擎

     <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創建之后的最后

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM