问题
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