春天STOMP从任何应用程序中发送消息 [英] Spring STOMP sending messages from anywhere in the application

查看:616
本文介绍了春天STOMP从任何应用程序中发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立使用践踏促成了WebSockets的消息的应用程序。我尝试发送从服务器到客户端消息,而无需在应用程序中的任何位置的请求。我在网上找到两个独立的选择,在应用程序从任何地方发送消息。

I am building an application using Stomp to broker messages over websockets. I am trying to send messages from the server to the client without a request from anywhere in the application. I found two separate choices online for sending messages from anywhere in the application.

首先是在 WebSocket的文档中发现。部分20.4.5:

The first is found in the Websocket documentation. Section 20.4.5:

@Controller
public class GreetingController {

    private SimpMessagingTemplate template;

    @Autowired
    public GreetingController(SimpMessagingTemplate template) {
        this.template = template;
    }

    @RequestMapping(value="/greetings", method=POST)
    public void greet(String greeting) {
        String text = "[" + getTimestamp() + "]:" + greeting;
        this.template.convertAndSend("/topic/greetings", text);
    }

}

二是通过的一份书面指南春博客

@Controller
public class GreetingController {

  @Autowired
  private SimpMessagingTemplate template;


  @RequestMapping(value="/greeting", method=POST)
  public void greet(String greeting) {
    String text = "[" + getTimeStamp() + "]:" + greeting;
    this.template.convertAndSend("/topic/greeting", text);
  }

}

两者都非常相似。第一个覆盖默认的构造函数,并且不自动装配模板的初始化。第二个不创建一个新的构造,但自动装配模板的初始化。我的第一个问题是,这两个动作是否相同?

Both are very similar. The first overrides the default constructor and does not autowire the template initialization. The second does not create a new constructor, but does autowire the template initialization. My first question is whether these two actions are equivalent?

越是pressing问题是,我无法调用从任何地方打招呼的方法。我曾尝试这个做了几个不同的方式。

The more pressing matter is that I am having trouble calling the "greet" method from anywhere. I have tried doing this a couple of different ways.

这是第一个:

    public class modifier {

    @Autowired
    private HelloController sender;


    public void adder(String words) throws Exception {
        sender.greet(words);

    }
}

在上述情况下,它好像新HelloController中从不初始化。调试时,我发现,当加法器方法调用迎接的方法,发件人为空,我收到一个空指针异常。

In the above case, it seems as if the new HelloController in never initialized. When debugging, I find that when the adder method calls the "greet" method, the sender is null and I receive a null pointer exception.

下面是我用来建立另一条路线:

Here is the other route I have used to set up:

public class modifier {

    public void adder(String words) throws Exception {
        HelloController sender = new HelloController();
        sender.greet(words);
}

}

在该第二上面的情况下,调试完毕后,也有一个零指示字例外,但是它不与发送者。它是与sender.template。发送者的模板被初始化,但它从未定的值,或者标识。然后,当this.template.convertAndSend()被调用内部打招呼,一个空指针异常上调this.template。

In this second above case, after debugging, there is also a null pointer exception, however it is not with sender. It is with sender.template. The template of sender is initialized, however it is never given a value, or id. Then when this.template.convertAndSend() is called inside greet, a null pointer exception is raised on this.template.

我已经混合和匹配的控制器都在两个gound实现和我从应用程序和工作没有任何一个单独的类调用映入眼帘方法的两种实现方法。

I have mixed and matched both the two gound implementations of the controller and my two implementations of calling the greet method from a separate class in the application and none work.

是否有任何人可以阐明我的问题有些轻?任何帮助,提示或建议将不胜AP preciated!

Is there anyone that could shed some light on my issue? Any help, tips, or advice would be greatly appreciated!

先谢谢了。

推荐答案

由于正确地写在注释中,有多种方式自动装配依赖,他们大多是等价的,你要选择你preFER取决于那些您的需求。

As correctly written in the comments, there are multiple ways to autowire dependencies, they are mostly equivalent, you have to choose the ones you prefer depending on your needs.

作为每其他问题:在弹簧控制器是收听用户请求的对象(单体),所以映射@RequestMapping处理HTTP请求到指定网址

As per the other question: in Spring the controller is the object (a singleton) that listen to user requests, so the mapping @RequestMapping handles HTTP requests to the designated url.

如果你需要使用SimpMessagingTemplate对象从服务器将邮件推送到客户端,您必须知道客户端必须使用STOMP协议(过的WebSockets),而且必须订阅合适的话题。

If you need to push messages from the server to the client using the SimpMessagingTemplate object you must know that the client must using the STOMP protocol (over websockets), and it must be subscribed to the right topic.

在使用STOMP服务器不能随意将邮件推送到客户端,但它只能消息发布到主题,和客户端必须订阅到右边的话题接受它们。事实上

In fact using STOMP the server can't push arbitrarily messages to the client, but it can only publish messages to topics, and the client must be subscribed to the right topics to receive them.

这篇关于春天STOMP从任何应用程序中发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆