我有一个使用 Spring Boot 和 Stomp over Websocket 的示例。当我将代理注册从 SimpleBrokerRegistration 更改为 StompBrokerRelayRegistration 时,它没有按预期工作。
这是我的 Websocket 配置:
@Configuration
@EnableWebSocketMessageBroker
@ConfigurationProperties(prefix = "spring.artemis")
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
//...
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
// If STOMP broker not configured, create an simple fallback
if (!StringUtil.isEmpty(host) || port > 0) {
config.enableStompBrokerRelay("/topic", "/queue")
.setRelayHost(host)
.setRelayPort(port);
} else {
config.enableSimpleBroker("/topic", "/queue");
}
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/hello")
.withSockJS();
}
//...
}
和 ArtemisConfig:
@Configuration
@ConfigurationProperties(prefix = "spring.artemis")
public class JmsConfig implements ArtemisConfigurationCustomizer {
private static final String DEFAULT_TRANSPORT_PROTOCOLS = "STOMP";
private String host;
private int port;
private String protocols;
// ...
@Override
public void customize(org.apache.activemq.artemis.core.config.Configuration configuration) {
host = StringUtil.hasText(host)?host:TransportConstants.DEFAULT_HOST;
port = port > 0? port:TransportConstants.DEFAULT_PORT;
protocols = StringUtil.hasText(protocols)?protocols:DEFAULT_TRANSPORT_PROTOCOLS;
Set<TransportConfiguration> acceptors = configuration.getAcceptorConfigurations();
Map<String, Object> params = new HashMap<>();
params.put(TransportConstants.HOST_PROP_NAME, host);
params.put(TransportConstants.PORT_PROP_NAME, port);
params.put(TransportConstants.PROTOCOLS_PROP_NAME, protocols);
TransportConfiguration tc = new TransportConfiguration(NettyAcceptorFactory.class.getName(), params);
acceptors.add(tc);
}
//...
}
然后,我使用这样的 javascript 进行连接:
var socket = new SockJS('/hello');
stompClient = Stomp.over(socket);
stompClient.connect('guest', 'guest', function(frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/greetings', function(greeting){
showGreeting(greeting.body);
});
});
它说找不到队列/topic/greetings
当我像这样使用 SimpMessagingTemplate 时:
messagingTemplate.convertAndSend("/topic/greetings", "WARN: " + warningString());
它抛出一个错误:
StompBrokerRelayMessageHandler : Received ERROR {message=[AMQ339001: Destination does not exist: /topic/greetings]} session=...
我不知道为什么它不能作为 SimpleBroker 工作。