Spring STOMP 从应用程序的任何地方发送消息 [英] Spring STOMP sending messages from anywhere in the application

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

问题描述

我正在使用 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?

更紧迫的问题是我无法从任何地方调用greet"方法.我尝试了几种不同的方法.

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 似乎从未初始化过.调试的时候发现adder方法调用greet"方法时,sender为null,收到空指针异常.

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 一起使用.发件人的模板被初始化,但它从未被赋予值或 id.然后当 this.template.convertAndSend() 在 greet 中被调用时,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.

我已经混合并匹配了控制器的两个正确实现和我从应用程序中的单独类调用greet方法的两个实现,但没有任何效果.

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.

有没有人可以对我的问题有所了解?任何帮助、提示或建议将不胜感激!

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

提前致谢.

推荐答案

正如评论中正确写的那样,有多种方法可以自动装配依赖项,它们大多是等效的,您必须根据需要选择您喜欢的方法.

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.

根据另一个问题:在 Spring 中,控制器是侦听用户请求的对象(单例),因此映射 @RequestMapping 将 HTTP 请求处理到指定的 url.

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.

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

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