【发布时间】:2018-04-15 04:45:27
【问题描述】:
我在一个 Spring 组件中有一个方法,它从 Spring 集成通道接收消息。收到消息后,会将其发送到 WebSocket 端点。这行不通。消息未广播。
this.messagingTemplate.convertAndSend("/topic/update", dto);
但是,当我将相同的代码放入 Web 控制器并在其上放置一个 RequestMapping 并调用该端点时,它就可以工作了。消息被广播。
当 Spring 集成执行程序调用它时,可能导致它无法工作的原因是什么?
当它工作时:.14:01:19.939 [http-nio-8080-exec-4] DEBUG o.s.m.s.b.SimpleBrokerMessageHandler - 处理消息目的地=/主题/更新会话=空负载={XXX} .14:01:19.939 [http-nio-8080-exec-4] 调试 o.s.m.s.b.SimpleBrokerMessageHandler - 广播到 1 个会话。
当它不起作用时,第二条消息不存在。 (线程是 taskExecutor-1 而不是 http-nio..)
控制器代码:
@RequestMapping("/testreq")
public void updateDelta() {
SummaryDTO dto = new SummaryDTO();
dto.setValue(-5000.0);
dto.setName("G");
this.messagingTemplate.convertAndSend("/topic/update", dto);
}
//this method is called by Spring Integration
//created by serviceActivator = new
//ServiceActivatingHandler(webcontroller,"update");
public void updateDelta(SummaryDTO dto) {
this.messagingTemplate.convertAndSend("/topic/update", dto);
}
消息发送:
synchronized(this){
...
this.updatedcontrollerchannel.send(MessageBuilder.withPayload(summarydto).build( )); }
频道创建:
updatedchannel = new DirectChannel();
updatedchannel.setBeanName("updatedcontroller");
serviceActivator = new ServiceActivatingHandler(detailService,"update");
handlerlist.add(serviceActivator);
updatedchannel.subscribe(serviceActivator);
beanFactory.registerSingleton("updatedcontroller", channel);
更新 我在我的环境中添加了 spring 消息传递源代码并实现了以下功能: 运行时中有 2 个 SimpleBrokerMessageHandler 类的实例。对于工作副本订阅注册表有一个条目,对于非工作副本,它有 0 个订阅。这是否为问题的根本原因提供了线索?只定义了一个 MessageSendingOperations 变量,它在控制器上。
【问题讨论】:
标签: spring websocket spring-integration