使用Websocket连接stomp和ActiveMQ



我使用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连接的示例,但只有tcp连接。

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

感谢

我使用了带有Stomp和WebSockets的ActiveMQ从浏览器获取数据。为我工作的配置非常相似,除了:

  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();

最新更新