Paulo Teixeira
Paulo Teixeira

Reputation: 458

Connect stomp and ActiveMQ using Websocket

I'm using Stomp and ActiveMQ to listener messages from lan and publish it to some application.

For testing, I implemented using tcp protocol connection, I need to use Websocket protocol.

My activeMQ already configure to use WebSocket, see the code below:

<!--
    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>

But if I use the ws connection not work for me:

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

I looked for some example about the WS connection using the StompJmsConnectionFactory class, but only have with tcp connection.

Someone already implemented something like this?

Thanks

Upvotes: 3

Views: 3151

Answers (1)

Manos Nikolaidis
Manos Nikolaidis

Reputation: 22224

I have used ActiveMQ with Stomp and WebSockets to get data from a browser. The configuration that worked for me is quite similar except :

  1. In my code I used String protocol = "tcp://";. It's the message broker that communicates with WebSockets (to a browser ?). Your java application communicates with the message broker through tcp.

  2. I used the Apollo message broker engine with this configuration

    <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. I called connection.start(); at the end after the MessageConsumer had been created

Upvotes: 2

Related Questions