Spring Boot 同一个StompSessionManager示例可以用于不同的目的地吗?

pxiryf3j  于 2023-11-17  发布在  Spring
关注(0)|答案(1)|浏览(153)

我有一个Spring集成流,StompInboundChannelAdapter监听来自一个目的地的传入消息,另一个集成流,StompMessageHandler发送消息到不同的目的地。我可以为两者使用相同的StompSessionManager示例吗?或者每个流都应该有自己的示例?STOMP服务器是相同的。
我尝试了以下配置的单例示例,它似乎可以工作,但我不知道这是正确的方法还是我遗漏了一些东西:

  1. @Configuration
  2. public class StompSessionManagerConfiguration {
  3. @Value("${host}")
  4. private String host;
  5. @Value("${port}")
  6. private Integer port;
  7. @Value("${login}")
  8. private String login;
  9. @Value("${passcode}")
  10. private String passcode;
  11. @Bean
  12. public StompSessionManager stompSessionManager() {
  13. ReactorNettyTcpStompClient stompClient = new ReactorNettyTcpStompClient(host, port);
  14. ReactorNettyTcpStompSessionManager stompSessionManager =
  15. new ReactorNettyTcpStompSessionManager(stompClient);
  16. stompSessionManager.setConnectHeaders(connectHeaders());
  17. return stompSessionManager;
  18. }
  19. public StompHeaders connectHeaders() {
  20. StompHeaders connectHeaders = new StompHeaders();
  21. connectHeaders.setLogin(login);
  22. connectHeaders.setPasscode(passcode);
  23. return connectHeaders;
  24. }
  25. }
  1. @Configuration
  2. public class IncomingMessageFlowConfiguration {
  3. @Autowired
  4. private StompSessionManager stompSessionManager;
  5. @Bean
  6. public IntegrationFlow incomingMessageFlow() {
  7. return IntegrationFlow.from(stompInboundChannelAdapter())
  8. .channel("incomingMessageChannel").get();
  9. }
  10. public StompInboundChannelAdapter stompInboundChannelAdapter() {
  11. StompInboundChannelAdapter adapter =
  12. new StompInboundChannelAdapter(stompSessionManager, "incomingDestination");
  13. adapter.setPayloadType(byte[].class);
  14. return adapter;
  15. }
  16. }
  1. @Configuration
  2. public class OutgoingMessageFlowConfiguration {
  3. @Autowired
  4. private StompSessionManager stompSessionManager;
  5. @Bean
  6. public IntegrationFlow outgoingMessageFlow() {
  7. return IntegrationFlow.from("outgoingMessageChannel")
  8. .handle(stompMessageHandler()).get();
  9. }
  10. public StompMessageHandler stompMessageHandler() {
  11. StompMessageHandler stompMessageHandler = new StompMessageHandler(stompSessionManager);
  12. stompMessageHandler.setDestination("outgoingDestination");
  13. return stompMessageHandler;
  14. }
  15. }
pgky5nke

pgky5nke1#

是的。您可以在不同的端点之间共享StompSessionManager。它的目标是管理连接到STOMP代理的单个客户端会话。
我们甚至在共享StompSessionManager的项目中有一个集成测试:

  1. @Bean
  2. public StompSessionManager stompSessionManager() {
  3. AbstractStompSessionManager stompSessionManager = new ReactorNettyTcpStompSessionManager(stompClient);
  4. stompSessionManager.setAutoReceipt(true);
  5. stompSessionManager.setRecoveryInterval(500);
  6. return stompSessionManager;
  7. }
  8. @Bean
  9. public PollableChannel stompInputChannel() {
  10. return new QueueChannel();
  11. }
  12. @Bean
  13. public StompInboundChannelAdapter stompInboundChannelAdapter() {
  14. StompInboundChannelAdapter adapter =
  15. new StompInboundChannelAdapter(stompSessionManager(), "/topic/myTopic");
  16. adapter.setOutputChannel(stompInputChannel());
  17. return adapter;
  18. }
  19. @Bean
  20. @ServiceActivator(inputChannel = "stompOutputChannel")
  21. public MessageHandler stompMessageHandler() {
  22. StompMessageHandler handler = new StompMessageHandler(stompSessionManager());
  23. handler.setDestination("/topic/myTopic");
  24. handler.setConnectTimeout(1000);
  25. return handler;
  26. }

字符串
查看更多信息在源代码:https://github.com/spring-projects/spring-integration/blob/main/spring-integration-stomp/src/test/java/org/springframework/integration/stomp/client/StompServerIntegrationTests.java

展开查看全部

相关问题