我有下面这段代码:
@Controller
@EnableScheduling
public class QuoteController {
@Scheduled(fixedDelay=5000)
@SendTo(value="/topic/quote")
public String sendPrice() {
return "message from scheduler";
}
}并且它不会将消息发送到通道。但是下面的代码可以工作:
@Controller
@EnableScheduling
public class QuoteController {
@Autowired
public SimpMessageSendingOperations messagingTemplate;
@Scheduled(fixedDelay=5000)
public String sendPrice() {
messagingTemplate.convertAndSend("/topic/quote", "message from scheduler");
}
}发布于 2019-02-19 20:17:54
我们应该只通过websocket调用的函数来使用@SendTo注释,它是用@MessageMapping注释的平均函数。
如果您想以其他方式将消息发送到队列,则应该使用messagingTemplate.convertAndSend。
@SendTo示例
@MessageMapping("/hello") // from websocket
@SendTo("/topic/bla")
public String foo1(String message) {
return message;
}.convertAndSend示例
@Autowired
private SimpMessagingTemplate template;
@GetMapping("/{msg}") //from GET request
public void foo2(@PathVariable String msg) {
template.convertAndSend("/topic/bla", msg);
}https://stackoverflow.com/questions/53179604
复制相似问题