使用 JMX / JMS 向 Web 应用程序的所有会话广播消息?
我的服务器上运行着一个 Spring MVC Web 应用程序。最近,我想添加一项功能来通知所有登录的用户他们的角色已更改,以便他们可以注销并再次登录。
我考虑过使用 JMX,所以做了一个小测试,效果很好,只是它不广播消息。例如,如果我有 2 个人登录,则只有 1 个人正在接收消息。
所以我的问题是,是否可以使用 JMX 将消息广播到 Web 应用程序的所有实例(活动会话)?
编辑 - 使用 JMS
所以我现在正在尝试使用 JMS,这是我的配置文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="connectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"/>
</bean>
<bean id="msgDestination"
class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="userToLogout.topic"/>
</bean>
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="pubSubDomain" value="true"/>
<property name="receiveTimeout" value="10000"/>
</bean>
<bean id="userNotifier"
class="com.cap.messaging.UserNotifier">
<property name="destination" ref="msgDestination"/>
<property name="jmsTemplate" ref="jmsTemplate"/>
</bean>
<bean id="userNotificationListener"
class="com.cap.messaging.UserNotificationListener">
</bean>
<bean
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destinationName" value="userToLogout.topic" />
<property name="messageListener" ref="userNotificationListener" />
</bean>
这现在不起作用。但是,如果我将 ActiveMQTopic 更改为 ActiveMQQueue,它就会起作用。我在这里缺少什么,这会起作用吗?
I have a Spring MVC web app running on my server. Recently I wanted to add a functionality to notify any logged in users that their role has changed, so they can logout and in again.
I thought about using JMX, so did a small test and it worked fine, except that its not broadcasting the message. So for example, if i have 2 logged in persons, only 1 is receiving the message.
So my question is, is it possible to broadcast a message with JMX to all instances of a web app (active sessions)?
Edit - using JMS
So I am trying with JMS right now, here is my configuration file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="connectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"/>
</bean>
<bean id="msgDestination"
class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="userToLogout.topic"/>
</bean>
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="pubSubDomain" value="true"/>
<property name="receiveTimeout" value="10000"/>
</bean>
<bean id="userNotifier"
class="com.cap.messaging.UserNotifier">
<property name="destination" ref="msgDestination"/>
<property name="jmsTemplate" ref="jmsTemplate"/>
</bean>
<bean id="userNotificationListener"
class="com.cap.messaging.UserNotificationListener">
</bean>
<bean
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destinationName" value="userToLogout.topic" />
<property name="messageListener" ref="userNotificationListener" />
</bean>
This isnt working right now. However it works if I change the ActiveMQTopic to ActiveMQQueue. what am I missing here, and would this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为简短的答案是否定的。 JMX 是一个使用 < a href="http://java.sun.com/developer/onlineTraining/rmi/RMI.html" rel="nofollow">RMI 通过 TCP/IP 套接字。您能做的最好的事情就是串行单播到所有实例,或者使用线程并通过多个 JMX 客户端连接并行发送到所有 Web 应用程序会话。
I think the short answer is no. JMX is a single connection API which uses RMI over TCP/IP sockets. Best you can do is to serially unicast to all of your instances or use threads and send to all of your web app sessions in parallel over a number of JMX client connections.