简体   繁体   English

获取Spring Broker Relay内部使用的stomp客户端

[英]Get the stomp client used internally by Spring Broker Relay

I'm trying to setup a broker relay in Spring with RabbitMQ being the broker.我正在尝试在 Spring 中设置一个代理中继,以 RabbitMQ 作为代理。 Things work as intended when all events originate from my browser, however, sometimes I have events generated on the server side dynamically.当所有事件都来自我的浏览器时,事情会按预期工作,但是,有时我会在服务器端动态生成事件。 I want to send these too to RabbitMQ to take advantage of things like durable topics or TTL for messages.我也想将这些发送到 RabbitMQ 以利用诸如持久主题或消息的 TTL 之类的东西。 As far as my understanding goes, using SimpleMessagingTemplate.convertAndSend() and convertAndSendToUser both end up sending the event to the browser instead of broker.就我的理解而言,使用SimpleMessagingTemplate.convertAndSend()convertAndSendToUser最终都会将事件发送到浏览器而不是代理。

As of now, I'm trying to create a new stomp client to rabbitmq and send events through that.截至目前,我正在尝试为 rabbitmq 创建一个新的 stomp 客户端并通过它发送事件。 But I can't help feel it to be a bit hacky.但我不禁觉得它有点hacky。 Is there a way to get a hold on the stomp client used by Spring and forward my messages easily?有没有办法控制 Spring 使用的 stomp 客户端并轻松转发我的消息? Or am I missing something here?还是我在这里遗漏了什么?

Any help is appreciated, thanks!任何帮助表示赞赏,谢谢!

Took a while but turns out you don't need to get a hold of the internal stomp client (it's actually an internal TcpClient from Reactor Netty though) or anything like that.花了一段时间,但事实证明您不需要掌握内部 stomp 客户端(尽管它实际上是 Reactor Netty 的内部 TcpClient)或类似的东西。 Following are the steps you need to do when you want a little bit of customization:以下是您需要进行一些自定义时需要执行的步骤:

  • Spring uses @EnableWebSocketMessageBroker to configure the broker or you can extend DelegatingWebSocketMessageBrokerConfiguration . Spring 使用@EnableWebSocketMessageBroker来配置代理,或者您可以扩展DelegatingWebSocketMessageBrokerConfiguration I ended up extending it, it makes little difference though.我最终扩展了它,但它没有什么区别。
  • In configureMessageBroker(MessageBrokerRegistry registry) , use the registry and enable stomp relay and the important part: for the registry in the same method, add ChannelInterceptors .configureMessageBroker(MessageBrokerRegistry registry) ,使用 registry 并启用 stomp relay 和重要的部分:在相同的方法中为 registry 添加ChannelInterceptors You can get the stomp command and process it as required.您可以获取 stomp 命令并根据需要对其进行处理。 The idea is identical to Spring Intercetpors.这个想法与 Spring Intercetpors 相同。 Add the headers needed inside that.在里面添加所需的标题。

    final StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(message); final StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(message); StompCommand command = headerAccessor.getCommand(); StompCommand 命令 = headerAccessor.getCommand();

Then finally, recreate the message for sending.最后,重新创建要发送的消息。

MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders());

Lastly, you can test if things are actually going to RabbitMQ management console to observe if messages are actually being sent.最后,您可以测试事情是否真的进入 RabbitMQ 管理控制台,以观察消息是否真的被发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM