Ace
Ace

Reputation: 450

Java StompSession send Message - Spring Boot

I'm trying to set up a websocket connection between two Spring Boot applications; I'm following the sample code here: https://github.com/eugenp/tutorials/blob/master/spring-boot-client/src/main/java/org/baeldung/websocket/client/MyStompSessionHandler.java

From which, this section works fine for me:

 @Override
    public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
   logger.info("New session established : " + session.getSessionId());
   session.subscribe("/topic/messages", this);
   logger.info("Subscribed to /topic/messages");
   session.send("/app/chat", getSampleMessage());
   logger.info("Message sent to websocket server");
}

However, I don't know how to reuse session.send() outside of afterConnected function. In other words, I want one function like below:

void sendMessage(String message){
    session.send("/app/chat", message);
}

Upvotes: 2

Views: 4460

Answers (2)

I have had the same problem, I would like to share my solution in this post. I have implemented a config class that exposes the StompSession with bean that can be injected into another component as a service.

@Configuration
@RequiredArgsConstructor
public class WebSocketClientConfig {

    private static final Logger logger = LoggerFactory.getLogger(WebSocketClientConfig.class);

    /**
     * Provide Stomp Session Handler
     *
     * @return
     */
    @Bean
    protected StompSessionHandler provideStompSessionHandler() {
        return new WebSocketStompSessionHandler();
    }

    /**
     * Provide WebSocket Client
     *
     * @return
     */
    @Bean
    protected WebSocketClient provideWebSocketClient() {
        return new StandardWebSocketClient();
    }

    /**
     * Provide StompSession session
     *
     * @param sessionHandler
     * @param webSocketClient
     * @return
     * @throws java.lang.InterruptedException
     * @throws java.util.concurrent.ExecutionException
     */
    @Bean
    protected StompSession provideStompSession(
            final StompSessionHandler sessionHandler,
            final WebSocketClient webSocketClient
    ) throws InterruptedException, ExecutionException {
        final WebSocketStompClient stompClient = new WebSocketStompClient(webSocketClient);
        stompClient.setMessageConverter(new MappingJackson2MessageConverter());
        return stompClient.connect("ws://localhost:8080/messages", sessionHandler).get();
    }
}

Now I can use the StomSession outside of the afterConnection:

@Service
@RequiredArgsConstructor
public class PublishMessageServiceImpl implements IPublishMessageService {

    /**
     * Stomp Session
     */
    private final StompSession stompSession;

    /**
     *
     * @param message
     */
    @Override
    public void publish(final PublishMessageDTO message) {
        Assert.notNull(message, "Message can not be null");
        stompSession.send("/topic/message", message);
    }

}

I hope it can be of use to someone, regards

Upvotes: 4

Heyphord
Heyphord

Reputation: 11

In order to use Session.send outside of afterConnected function. You need to store the Session variable that is returned when you call. StompClient.connect()

Like

StompSession session = stompClient.connect(URL, sessionHandler).get();

And use it Like

session.send("/app/chat", message);

I hope this answers you question

Upvotes: 1

Related Questions