简体   繁体   English

Spring Boot如何知道MessageMapping是指定Jms队列还是Websocket主题?

[英]How does Spring Boot know whether MessageMapping specifies a Jms Queue or a Websocket topic?

I have a Controller bean with a method such as 我有一个控制器bean,其方法如

@MessageMapping("${my.topic}")
public void receiveCommand(Message content) {
    log.info("Received command: " + content);
}

And I also inject a SimpMessagingTemplate and a JmsMessagingTemplate into this controller. 而且我也注入SimpMessagingTemplateJmsMessagingTemplate到这个控制器。 I believe the problem with my receiveCommand method is that Spring is expecting this to be a JMS message and not a WebSocket message. 我相信我的receiveCommand方法的问题是Spring期望这是JMS消息而不是WebSocket消息。 However this seems impossible to determine, how can I make sure that this method corresponds to a WebSocket topic and not a JMS queue? 但是,这似乎无法确定,如何确定此方法对应于WebSocket主题而不是JMS队列?

My application is annotated with @EnableJms and @EnableWebSocket and works well for receiving JMS messages and sending WebSocket messages via STOMP, but doesn't seem to be able to receive WebSocket messages. 我的应用程序带有@EnableJms@EnableWebSocket注释,并且可以很好地用于接收JMS消息和通过STOMP发送WebSocket消息,但是似乎无法接收WebSocket消息。

The message imports are as follows on my Controller. 消息导入如下在我的控制器上。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;

I also have another method that uses a JmsListener that works, so I am wondering if this is somehow picked up by Spring on the classpath and overrides the MessageMapping to be a JMS queue name. 我还有另一种使用有效的JmsListener的方法,因此我想知道Spring是否在类路径上以某种方式选择了此方法,并将MessageMapping重写为JMS队列名称。

@JmsListener(destination = "${my.destination}")
public void receiveMessage(String content) {
   System.out.println("Jms message: " + content); 
}

I do not see any stack traces or errors when running a unit test that looks like the following. 运行如下所示的单元测试时,没有看到任何堆栈跟踪或错误。

@Autowired
private SimpMessagingTemplate websocket;

@Value("${my.topic}")
private String topic;

@Test
public void shouldReceiveCommandMessages() throws IOException {
    URL url = Resources.getResource("command.json");
    String json = Resources.toString(url, Charsets.UTF_8);
    this.websocket.convertAndSend(topic, json);
}

Which is within a larger SpringBootTest that works for testing the Jms sending and receiving. 它在较大的SpringBootTest ,可用于测试Jms发送和接收。

The SIMP template is for sending data out to connected clients; SIMP模板用于将数据发送到连接的客户端。 to send data into the controller you should use the STOMP Client . 要将数据发送到控制器,您应该使用STOMP Client

4.4.6. 4.4.6。 Send Messages 发送信息

What if you want to send messages to connected clients from any part of the application? 如果要从应用程序的任何部分向连接的客户端发送消息怎么办? Any application component can send messages to the "brokerChannel". 任何应用程序组件都可以将消息发送到“ brokerChannel”。 The easiest way to do that is to have a SimpMessagingTemplate injected, and use it to send messages. 最简单的方法是注入SimpMessagingTemplate,然后使用它发送消息。 Typically it should be easy to have it injected by type, for example: 通常,按类型插入它应该很容易,例如:

4.4.15. 4.4.15。 STOMP Client STOMP客户端

Spring provides a STOMP over WebSocket client and a STOMP over TCP client. Spring提供了一个基于WebSocket的STOMP客户端和一个基于TCP的STOMP客户端。

@Controller
class Listener {

    @MessageMapping("/string")
    public void handle(String in) {
        System.out.println("STOMP Received: " + in);
    }

    @JmsListener(destination = "so50614472")
    public void listen(String in) {
        System.out.println("JMS Received: " + in);
    }

}

and

STOMP Received: foo
JMS Received: bar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM