gpt4 book ai didi

java - convertAndSend 与 Send To 注释

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:22:07 25 4
gpt4 key购买 nike

我有以下代码:

@Controller
@EnableScheduling
public class QuoteController {

@Scheduled(fixedDelay=5000)
@SendTo(value="/topic/quote")
public String sendPrice() {
return "message from scheduler";
}
}

它不会将消息发送到 channel 。但是下面的代码有效:

@Controller
@EnableScheduling
public class QuoteController {
@Autowired
public SimpMessageSendingOperations messagingTemplate;

@Scheduled(fixedDelay=5000)
public String sendPrice() {
messagingTemplate.convertAndSend("/topic/quote", "message from scheduler");
}
}

最佳答案

我们应该只在 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);
}

关于java - convertAndSend 与 Send To 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53179604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com