gpt4 book ai didi

Spring 网络套接字 : How intercept a message sent by SimpMessagingTemplate through any either convertAndSend or convertAndSendToUser methods?

转载 作者:行者123 更新时间:2023-12-05 07:33:35 25 4
gpt4 key购买 nike

对于 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 通过 convertAndSendconvertAndSendToUser 方法发送的任何消息?

缺少什么?缺少有关基础设施的配置或创建其他扩展特定 Spring 类的类。

最佳答案

据我所知,没有一种简单的方法可以做到这一点。我遇到过直接调用模板的情况(就像你一样)。当我迁移到使用外部 STOMP 代理时,我突然发现我的一些调用不再有效:

当您使用 Rabbit 时,

/topic/sddf/traffic 不再是有效目的地

我考虑编写自己的类 SimpMessagingTemplate 版本,这变得太困难了 - 所以我不得不手动更改所有调用。

我的示例使用的是 Kotlin 而不是 Java

我的解决方案是选择这门课:

@Component
@Configurable
class MessagingTemplateWrapper{

var template: SimpMessagingTemplate = ApplicationContextHolder.getContext().getBean(SimpMessagingTemplate::class.java)
private val log = LoggerFactory.getLogger(BridgeDataListener::class.java)

fun convertAndSend(dest: String, data: Any) {

val rabbitDestination = dest.substring(0,11) + dest.substring(11).replace("/",".")
template.convertAndSend(rabbitDestination, data)
}
}

在我使用模板的任何地方我都这样做了:

//    var template: SimpMessagingTemplate = ApplicationContextHolder.getContext().getBean(SimpMessagingTemplate::class.java)
var template: MessagingTemplateWrapper = ApplicationContextHolder.getContext().getBean(MessagingTemplateWrapper::class.java)

所以我只有一个包装器来做(也许)你想做的事。

我不喜欢这个解决方案 - 但它确实有效。

关于 Spring 网络套接字 : How intercept a message sent by SimpMessagingTemplate through any either convertAndSend or convertAndSendToUser methods?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50533575/

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