Michael Nelson
Michael Nelson

Reputation: 535

Spring 4 WebSockets subscribe to a topic

Is it possible to map a method in a spring managed bean to a topic subscription using spring messaging?

I've looked at the examples here: http://assets.spring.io/wp/WebSocketBlogPost.html including the example stock application but it looks like all topic subscription is on the client side. Is it possible to also subscribe to topics on the server side?

Upvotes: 0

Views: 394

Answers (1)

Evgeni Dimitrov
Evgeni Dimitrov

Reputation: 22516

You can use JMS to subscribe to the topic.

public class ExampleListener implements MessageListener {

    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            try {
                System.out.println(((TextMessage) message).getText());
            }
            catch (JMSException ex) {
                throw new RuntimeException(ex);
            }
        }
        else {
            throw new IllegalArgumentException("Message must be of type TextMessage");
        }
    }
}





   <!-- this is the Message Driven POJO (MDP) -->
    <bean id="messageListener" class="jmsexample.ExampleListener" />

    <!-- and this is the message listener container -->
    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="destination" ref="destination"/>
        <property name="messageListener" ref="messageListener" />
    </bean>

More here: http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/jms.html

Upvotes: 0

Related Questions