Java Spring Boot: SimpMessagingTemplate send messages are not received by Stomp Endpoints

血红的双手。 提交于 2021-01-27 02:41:35

问题


I can't get the Spring Message Sample to work in Unit tests using the SimpMessagingTemplate to send messages to the Endpoints.

I followed the instructions here: https://spring.io/guides/gs/messaging-stomp-websocket/

So far my Controller looks like:

@Data @NoArgsConstructor @AllArgsConstructor
public static class Message {
    private Long id;
    private String value;
    private long time;
}

@MessageMapping("/message")
@SendTo("/topic/response")
public Message slowEndpont(Message message) throws Exception {
    Thread.sleep(3000); // simulated delay
    System.err.println("Message Received: " + message);
    return new Message(message.id, "Hello Client", System.currentTimeMillis());
}

My Unit Test now tries to send a message:

@Autowired
SimpMessagingTemplate messageTemplate;

@Test
public void sendMessage() throws Exception {
    System.err.println("** Sending messages...");
    messageTemplate.convertAndSend("/app/message", 
            new MessageController.Message(1L, "Hello Server", System.currentTimeMillis()));

    messageTemplate.convertAndSend("/topic/message", 
            new MessageController.Message(1L, "Hello Server", System.currentTimeMillis()));

    messageTemplate.convertAndSend("/queue/message", 
            new MessageController.Message(1L, "Hello Server", System.currentTimeMillis()));

    messageTemplate.convertAndSend("/message", 
            new MessageController.Message(1L, "Hello Server", System.currentTimeMillis()));
    System.err.println("** Messages send!");
    Thread.sleep(1500);
}

Full Code sample is here: https://github.com/puel/training/tree/master/messaging

So far so good. Messages are all send. But never received. I traced it down and the registry of the MessageTemplate is empty. But why?

This problem seems to be close too: Send Message to all clients via SimpMessagingTemplate in ServletContextListener

But using MessageSendingOperations doesn't help either.

Thanks,

Paul

来源:https://stackoverflow.com/questions/32275898/java-spring-boot-simpmessagingtemplate-send-messages-are-not-received-by-stomp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!