相关文章推荐
聪明伶俐的刺猬  ·  解决“The ...·  1 年前    · 
知识渊博的豆芽  ·  WKWebView刷新URL - ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams public class QuoteController { @Autowired public SimpMessageSendingOperations messagingTemplate; @Scheduled(fixedDelay=5000) public String sendPrice() { messagingTemplate.convertAndSend("/topic/quote", "message from scheduler"); @SendTo works when the request is coming from the browser and interpreted by spring framework. And convertAndSend is the api you can use it in any environment. Because you are using it in Thread, this is not an endpoint in spring and value returned by the sendPrice() method will not be processed by spring. Vikram Singh Nov 9, 2018 at 4:55 I think @VikramSingh is sort of right, though I disagree with the reason: @SendTo works when the method being annotated is involved in messaging interactions - Spring invokes it in response to a message it received, and takes the @SendTo annotation as an indication that a reply message is to be generated. Here, sendPrice() is invoked by Spring, but not in a messaging context, so Spring has no reason to think that a reply is expected - so the @SendTo annotation is ignored... moilejter Nov 9, 2018 at 5:02 Yes I think when The method is annotated with @SendTo spring treat it as an messaging endpoint and when it receives a message from client it internally call convertAndSend method to send message to client. But In case of @Scheduled annotation it will be invoked sometime latter without receiving message from client. Because there is no client is involved it doesn't make sense to send message to client which might be not available at the time of the invocation. Vikram Singh Nov 9, 2018 at 5:10

We should use @SendTo annotation only by functions that called by websocket, it's mean functions annocated with @MessageMapping .

If you want to send message to queue in other way you should use messagingTemplate.convertAndSend .

Example for @SendTo :

@MessageMapping("/hello") // from websocket
@SendTo("/topic/bla")
public String foo1(String message) {
    return message;

Example for .convertAndSend:

@Autowired
private SimpMessagingTemplate template;
@GetMapping("/{msg}") //from GET request
public void foo2(@PathVariable String msg) {
    template.convertAndSend("/topic/bla", msg);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.