【问题标题】:Spring 4 WebSockets subscribe to a topicSpring 4 WebSockets 订阅主题
【发布时间】:2014-05-16 15:33:07
【问题描述】:

是否可以使用 Spring 消息传递将 Spring 托管 bean 中的方法映射到主题订阅?

我看过这里的例子:http://assets.spring.io/wp/WebSocketBlogPost.html 包括示例股票应用程序,但看起来所有主题订阅都在客户端。是否也可以在服务器端订阅主题?

【问题讨论】:

    标签: spring websocket spring-websocket


    【解决方案1】:

    您可以使用 JMS 订阅主题。

    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>
    

    更多:http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/jms.html

    【讨论】:

      猜你喜欢
      • 2014-09-07
      • 2017-08-12
      • 1970-01-01
      • 2020-12-13
      • 2018-10-04
      • 1970-01-01
      • 1970-01-01
      • 2020-09-20
      • 2018-06-17
      相关资源
      最近更新 更多