简体   繁体   中英

Spring websocket send message from async method

I am using spring websocket 4.2.4 with sockjs and stomp and i am trying to send a message from the server to all the subscribers in an async task with no luck

my class is:

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");
        }
    }
}

but the subscribers dont get the message

any help? what did i do wrong :(

* EDIT *

my message broker:

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

and when i subscribe:

subscribe("myApp/someRoute") 

thanks!!

** Edit 2: **

thanks for the help i fix the problem :)

You can do that but over Broker destination.

Share, please, your AbstractWebSocketMessageBrokerConfigurer .

Typically we do this:

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

Where all those /app/ destinations won't be treated by Broker. You can subscribe to them only in case of @SubscribeMapping - the request-reply scenario from the client initiative.

Your task is fully fits to the publish-subscribe story - topic in terms of STOMP.

There fore your subscribers (clients) should be subscribed to some topic on the broker and after that you can simply send message to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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