我正在构建一个应用程序,它将使用 STOMP over websockets 发送消息。我想在没有客户端发出请求的情况下发送消息。根据文档,我可以通过使用 convertAndSend 来做到这一点。
但是,当我尝试这样做时,我得到一个空指针异常。请看下面的代码:
public class ParseJSON {
@Autowired
private SimpMessagingTemplate template;
public void getDetails(String json) {
try {
Tweet status = sendDetails(TwitterObjectFactory.createStatus(json));
sentToWebApp(status);
} catch (TwitterException e) {
e.printStackTrace();
}
}
@Scheduled(fixedDelay = 50)
private void sentToWebApp(Tweet status) {
System.out.println(status);
this.template.convertAndSend("/tweet/update", status);
}
}
堆栈跟踪:
java.lang.NullPointerException: null
at org.myproject.worker.ParseJSON.sentToWebApp(ParseJSON.java:62)
at org.myproject.worker.ParseJSON.getDetails(ParseJSON.java:51)
at org.myproject.worker.TwitterClient.run(TwitterClient.java:50)
at org.myproject.Controllers.TweetController.greeting(TweetController.java:37)
任何人都可以对我的情况进行任何说明,这样我就可以通过 websocket 发送消息而不会遇到异常。
提前致谢。