2015-11-03 83 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! –