【发布时间】:2018-05-25 16:37:44
【问题描述】:
对于Spring Websocket
我有以下并且工作正常:
private final SimpMessagingTemplate simpMessagingTemplate;
...
@Scheduled(cron="some expression")
public void sendNotification() {
logger.info("sendNotification ...");
simpMessagingTemplate.convertAndSend(
"/topic/something",
new Notification(...));
logger.info("... sendNotification");
}
代码运行良好。所有订阅该Topic 的客户(我正在与ActiveMQ 合作)都能够看到发送的消息。
自定义类
@Component
class MessageChannelInterceptorAdapter extends ChannelInterceptorAdapter {
@Override 的所有方法都是为了拦截而创建的。
这个MessageChannelInterceptorAdapter 类注册关于基础设施如下:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Autowired
@Qualifier("messageChannelInterceptorAdapter")
private ChannelInterceptorAdapter messageChannelInterceptorAdapter;
...
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.interceptors(messageChannelInterceptorAdapter);
WebSocketMessageBrokerConfigurer.super.configureClientInboundChannel(registration);
}
@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
registration.interceptors(messageChannelInterceptorAdapter);
WebSocketMessageBrokerConfigurer.super.configureClientOutboundChannel(registration);
}
...
即使应用程序运行良好。通过SimpMessagingTemplate发送的消息永远不会被MessageChannelInterceptorAdapter截获。
注意:MessageChannelInterceptorAdapter 类可以很好地拦截Stomp 事件,但同样不适用于SimpMessagingTemplate
因此:如何拦截SimpMessagingTemplate 通过convertAndSend 或convertAndSendToUser 方法发送的任何消息?
缺少什么?缺少有关基础架构的配置或创建其他扩展特定 Spring 类的类。
【问题讨论】:
-
有任何进展吗?
-
@Jeef 是的,关于
SimpMessagingTemplate,拦截器仅在存在客户端侦听Destination时仅按预期工作。因为在我的情况下它是Topic,并且没有消费者,所以消息被发送但丢失了,拦截器永远不会工作。