【问题标题】:Connect stomp and ActiveMQ using Websocket使用 Websocket 连接 stomp 和 ActiveMQ
【发布时间】:2015-11-03 22:36:33
【问题描述】:

我正在使用 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);

我找了一些关于使用StompJmsConnectionFactory 类的WS 连接的例子,但只有with tcp 连接。

有人已经实现了这样的东西吗?

谢谢

【问题讨论】:

  • 我不认为 StompJMS 支持 websocket 连接。为什么还要为 Java 客户端使用 WebSockets?

标签: java tcp websocket activemq stomp


【解决方案1】:

我使用 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. 在创建MessageConsumer 后,我在最后调用了connection.start();

【讨论】:

    猜你喜欢
    • 2015-04-06
    • 2012-11-03
    • 2015-07-08
    • 1970-01-01
    • 2022-12-15
    • 2016-07-04
    • 2014-08-21
    • 1970-01-01
    • 2015-02-10
    相关资源
    最近更新 更多