gpt4 book ai didi

java - StompSessionHandler 不在 afterConnected block 中调用 handleFrame?

转载 作者:行者123 更新时间:2023-11-30 08:39:36 25 4
gpt4 key购买 nike

我有一个相当简单的设置来测试 Spring 中的 Stomp 支持。

我计划使用 JS 将消息发送到队列并在 Spring 应用程序中接收和处理它们。但是,由于某种原因,它似乎不起作用。

JS 客户端:

var ws = new SockJS('/hello');
client = Stomp.over(ws);
...
client.subscribe('jms.queue.test', function(message) {}
...
client.send("jms.queue.test", {}, "message");

Spring 配置(目前大部分没用,因为我不使用 /app 目标):

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableStompBrokerRelay("jms.topic", "jms.queue");
config.setApplicationDestinationPrefixes("/app");
config.setPathMatcher(new AntPathMatcher("."));
}

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

Spring Boot 应用:

@Component
public class BusinessLogic implements CommandLineRunner {

@Autowired
hulloController controller;

@Override
public void run(String... args) throws Exception {
stuff(args);
}

public void stuff(String[] args) throws InterruptedException {
Thread.sleep(20000);//sleep while spring boot fully initializes
controller.waitForGreet();
}
}

Controller (不是真正的@Controller,我不打算使用MVC):

@Component
public class hulloController {

private SimpMessagingTemplate template;

@Autowired
StompSessionHandler handler;

@Autowired
public hulloController(SimpMessagingTemplate template) {
this.template = template;
}

public void waitForGreet() {
System.out.println("Entered Listener");

WebSocketClient transport = new StandardWebSocketClient();
WebSocketStompClient stompClient = new WebSocketStompClient(transport);
stompClient.setMessageConverter(new StringMessageConverter());
stompClient.connect("ws://127.0.0.1:61613/stomp", handler);
}
}

最后,处理程序:

@Component
public class SessionHandler implements StompSessionHandler {
@Override
public void afterConnected(StompSession stompSession, StompHeaders stompHeaders) {
System.out.println("Connected!");
StompFrameHandler handler = new StompFrameHandler() {
@Override
public Type getPayloadType(StompHeaders stompHeaders) {
return null;
}

@Override
public void handleFrame(StompHeaders stompHeaders, Object o) {
System.out.println("received " + o.toString());
}
};

//StompSession.Subscription s = stompSession.subscribe("jms.queue.test", handler);

stompSession.send("jms.queue.test", "hello!");
}
...
}

如果我评论 client.subscribe 部分,client.send 工作正常,消息被接收并在 JS 中呈现,所以队列名称和连接 URL 没问题。我也尝试使用 SockJSClient 作为传输,但它没有帮助。

当我从 JS 发送消息时,有大约 1 或 2 分钟的时间,其中一半没有显示(如果订阅正常工作就会出现这种情况),然后 JS 开始接收 100% 的消息。

我今天在 github 上看到了很多几乎相同的代码,但我只是看不出这里可能有什么问题。

最佳答案

 //StompSession.Subscription s = stompSession.subscribe("jms.queue.test", handler);

stompSession.send("jms.queue.test", "hello!");

基于 WebSocket 的 STOMP 是一种异步协议(protocol)。

您在那里调用 subscribe 并到达 send。当您开始向该目的地发送内容时,无法保证订阅已经发生。

考虑使用StompSession.Subscription及其addReceiptTask()在确认订阅后发送消息到目的地。

并且您应该启用 StompSession.setAutoReceipt(true) 以使其与您的 STOMP Broker 一起工作。

关于java - StompSessionHandler 不在 afterConnected block 中调用 handleFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36066195/

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