【问题标题】:Push Message from ActiveMQ to Spring Controller从 ActiveMQ 向 Spring 控制器推送消息
【发布时间】:2015-08-03 22:42:09
【问题描述】:

我正在使用 Spring MVC、ActiveMQ 和 WebSocket(通过 sock.jsstomp.js)构建实时数据传递应用程序。

我们知道,当生产者(另一个桌面应用程序)向 ActiveMQ 推送消息时,onMessage() 方法会捕获它。

public class MessageReceiver implements MessageListener {
    public void onMessage(Message message) {
        System.out.println(message);

        // How to push the message to a Controller in Spring?
    }
}

大多数教程只是将消息打印到控制台。

我有另一个控制器叫WebSocketController

@Controller
public class WebSocketController {
    @SubscribeMapping("/getRealTimeResult.action/{submissionId}")
    public Message getRealTimeResultAction(
            @DestinationVariable long submissionId) {
        return new Message("Message content from onMessage() method");
    }
}

我想将onMessage()方法收到的消息推送到getRealTimeResultAction()方法。你能告诉我怎么做吗?

我知道 ActiveMQ 可以通过端口61613 使用stomp 与浏览器通信。

我不想这样做,因为我认为 MQ 应该对用户透明。另外我需要在WebSocketController做一些授权。

【问题讨论】:

    标签: java spring-mvc websocket activemq stomp


    【解决方案1】:

    一般来说,带有@SubscribeMapping 和@MessageMapping 方法的@Controller 可以处理来自通过WebSocket 连接的STOMP 客户端的订阅和消息。

    根据您的描述,不清楚您要做什么。消息是从浏览器客户端通过 STOMP 推送到 ActiveMQ,还是由其他后端 JMS 客户端生成?此外,MessageReceiver 接收实际消息,而 @Controller 方法具有用于处理来自 STOMP 客户端的订阅的 @SubscribeMapping 方法。目前尚不清楚您要做什么。请提供更多信息,以便我提供更好的答案。

    【讨论】:

    • 首先,Web 客户端向 ActiveMQ 推送一条消息(具有唯一 ID)。然后后端 JMS 客户端(不在 Spring MVC 中)接收到消息并将更多消息推送到 ActiveMQ(异步)。所以会调用Spring MVC中的onMessage()函数。现在我想将消息推送到 Spring Controller。我该怎么办?
    • @Controller 和 MessageReceiver 在同一个 JVM 中吗?如果是这样,您可以使用一些通知机制(例如 Spring ApplicationContext 事件)。或者根据您想要做什么,他们可能会共享某种数据结构。
    • 是的,它们在同一个 Web 项目中。 Spring ApplicationContext 可能不是一个好的选择。有什么优雅的实现吗?
    • Spring 4.2 增加了对@EventListener spring.io/blog/2015/05/26/spring-framework-4-2-goes-rc1 的支持。
    猜你喜欢
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2011-12-29
    • 2018-11-24
    相关资源
    最近更新 更多