Nikhil Das Nomula
Nikhil Das Nomula

Reputation: 1953

Spring websocket stomp sock js activemq durable subcription

As per the documentation of activemq we need to set the http://activemq.apache.org/stomp client-id header to have durable subscriptions.

I set the client-id in connect headers and activemq.subscriptionName in subscription headers as shown below, however I am not seeing the desired behavior. Do we need to set anything on the web socket configuration and message side too?

Here is the subscription code

var headers = {
      // additional header
      'client-id': 'my-client-id'
};

var subscription_headers = {
      // additional header
      'activemq.subscriptionName': 'my-client-id'
};

var connect = function () {
   var socket = new SockJS( webSocketUrl );
   stompClient = Stomp.over( socket );

   stompClient.connect( headers, function ( frame ) {

      console.log( 'Connected: ' + frame );

      stompClient.subscribe( topic, function ( message ) {
        .....
        .....
      }, subscription_headers);
   }, function(frame) {
        console.log("Web socket disconnected");
   });
 }

Websocket configuration

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer {

@Autowired
@Value("${spring.websocket.activemq.relay.host}")
private String relayHost;

@Autowired
@Value("${spring.websocket.activemq.relay.port}")
private int relayPort;

@Autowired
@Value("${spring.activemq.user}")
private String activeMqLogin;

@Autowired
@Value("${spring.activemq.password}")
private String activeMqPassword;

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableStompBrokerRelay("/queue/", "/topic/")
       .setRelayHost(relayHost)
       .setRelayPort(relayPort)
       .setSystemLogin(activeMqLogin)
        .setSystemPasscode(activeMqPassword);
        registry.setApplicationDestinationPrefixes("/testbrkr");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/exWs").withSockJS();
    }
}

Upvotes: 0

Views: 1179

Answers (1)

Nikhil Das Nomula
Nikhil Das Nomula

Reputation: 1953

This worked, passing the headers directly in the function as shown

var connect = function () {
   var socket = new SockJS( webSocketUrl );
   stompClient = Stomp.over( socket );

   stompClient.connect( {"client-id": "my-client-id"},, function ( frame ) {

      console.log( 'Connected: ' + frame );

      stompClient.subscribe( topic, function ( message ) {
        .....
        .....
      }, {"activemq.subscriptionName": "my-client-id"});
   }, function(frame) {
        console.log("Web socket disconnected");
   });
 }

Upvotes: 1

Related Questions