Search code examples
javascriptjavawebsocketactivemq-classicstomp

How to use the correct protocol for receiving active mq web socket stomp from java based program


I have a JavaScript based active mq listener, who is using stomp and web sockets.I was able to send test messages to active mq and receive them.

What I really want is it needs to be sent from a Java based code.

  • Is it Ok to have the JavaScript listening on web sockets/stomp and the java code be using tcp ?
  • If it is OK, should all of the ports be the same?

I am having issues receiving data in JavaScript. However I am seeing the topic being enquued in the active mq.thanks

function subscribeEndpoint(endpoint){
    var client = Stomp.client("ws://localhost:61614/stomp", "v11.stomp");
    var headers = { id:'JUST.FCX', ack: 'client'};
    client.connect("admin", "admin", function () {
        client.subscribe(endpoint,
             function (message) {
                 alert(message);
             }, headers);
    });
}

Java:

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616/stomp");

// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();

// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

// Create the destination (Topic)
Destination destination = session.createTopic("vrwrThreat");

// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

// Create a messages
TextMessage message = session.createTextMessage(text);

producer.send(message);

// Clean up
session.close();
connection.close();

Solution

  • Clients can communicate with each other across protocols and transport mechanisms without issue. A STOMP client on WebSocket or any other port (TCP, SSL, etc) can send to a Topic and an AMQP, MQTT or OpenWire client can receive the message so long as the protocol supports the content of the message, same goes for the reverse case of AMQP, MQTT or OpenWire sending and STOMP receiving.

    The case you've mentioned is pretty standard. The code you've posted appears to be using the OpenWire client to send to a Topic "vrwrThreat". The URI used in the connection factory is slightly off in that you added the '/stomp' which is meaningless. Since you are sending to a Topic you need to ensure that the client that is to receive the message is active at the time of transmission otherwise the message will be dropped. Also you need to ensure that both are operating on the same Topic which is unclear from your code snippets.