gpt4 book ai didi

java - Spring websocket stomp sockjs activemq 持久订阅

转载 作者:行者123 更新时间:2023-12-01 09:43:39 25 4
gpt4 key购买 nike

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

我在连接 header 中设置了 client-id,在订阅 header 中设置了 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();
}
}

最佳答案

这有效,直接在函数中传递 header ,如图所示

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

关于java - Spring websocket stomp sockjs activemq 持久订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38248009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com