【问题标题】:Spring websocket stomp sock js activemq durable subcriptionSpring websocket stomp sockjs activemq 持久订阅
【发布时间】:2016-11-09 22:07:34
【问题描述】:

根据 activemq 的文档,我们需要设置 http://activemq.apache.org/stomp client-id 标头以具有持久订阅。

我在连接标头中设置了客户端 ID,在订阅标头中设置了 activemq.subscriptionName,如下所示,但是我没有看到所需的行为。我们还需要在 Web 套接字配置和消息端设置什么吗?

这是订阅代码

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 配置

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

【问题讨论】:

  • 能否请您发布相关的spring-websocket 配置?

标签: java activemq stomp spring-websocket sockjs


【解决方案1】:

这行得通,直接在函数中传递标头,如图所示

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

【讨论】:

    猜你喜欢
    • 2016-07-04
    • 1970-01-01
    • 2018-10-31
    • 2016-01-14
    • 2017-06-09
    • 1970-01-01
    • 2017-06-05
    • 2022-12-15
    • 2018-11-05
    相关资源
    最近更新 更多