【问题标题】:Spring websocket send message from async methodSpring websocket从异步方法发送消息
【发布时间】:2016-03-02 14:41:04
【问题描述】:

我正在使用带有 sockjs 和 stomp 的 spring websocket 4.2.4,我正在尝试从服务器向异步任务中的所有订阅者发送消息,但没有成功

我的班级是:

public class MyClass{
    private timer;
    public MyClass(){
        this.timer = new Timer();
    }

    @Async
    public void myMethod(){
        timer.schedule(new MyReminder(), 1000);
    }

    @Async
    private class MyReminder extends TimerTask{

        @Autowired
        SimpMessagingTemplate messageingTemplate;

        @Override
        public void run(){
            messageingTemplate.convertAndSend("/app/subscribers","message");
        }
    }
}

但订阅者没有收到消息

有什么帮助吗?我做错了什么:(

* 编辑 *

我的消息代理:

public void configureMessageBroker(MessageBrokerRegistry config){ 
    config.enableSimpleBroker("/topic","/myApp"); 
    config.setApplicationDestinationPrefixes("/myApp");
}

当我订阅时:

subscribe("myApp/someRoute") 

谢谢!!

** 编辑 2:**

感谢您帮助我解决问题:)

【问题讨论】:

    标签: spring spring-mvc stomp spring-websocket sockjs


    【解决方案1】:

    您可以这样做,但要通过代理目的地。

    请分享您的AbstractWebSocketMessageBrokerConfigurer

    通常我们会这样做:

    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.setApplicationDestinationPrefixes("/app/")
                .enableSimpleBroker("/queue/", "/topic/");
    }
    

    Broker 不会处理所有这些/app/ 目的地。您只能在 @SubscribeMapping 的情况下订阅它们 - 来自客户端倡议的请求-回复场景。

    您的任务完全符合发布-订阅的故事 - 就 STOMP 而言,topic

    因此,您的订阅者(客户)应该订阅代理上的某个主题,然后您可以简单地向它发送消息。

    【讨论】:

    • public void configureMessageBroker(MessageBrokerRegistry config){ config.enableSimpleBroker("/topic","/myApp"); config.setApplicationDestinationPrefixes("/myApp");};;;;;;;;;当我订阅时: subscribe("myApp/someRoute")
    • 请把这样的评论移到您的问题中作为编辑。 cmets中的代码很难跟进。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 2018-07-14
    • 1970-01-01
    • 2015-12-10
    • 2019-06-16
    • 2016-04-28
    相关资源
    最近更新 更多