我的服务器上运行着一个 Spring MVC 网络应用程序。最近我想添加一个功能来通知任何登录用户他们的角色已经改变,这样他们就可以注销并重新登录。
我考虑过使用 JMX,所以做了一个小测试,它工作正常,只是它没有广播消息。因此,例如,如果我有 2 个人登录,则只有 1 人收到消息。
所以我的问题是,是否可以使用 JMX 向网络应用程序的所有实例( Activity session )广播消息?
编辑 - 使用 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,它就会起作用。我在这里缺少什么,这行得通吗?
最佳答案
我认为简短的回答是否定的。 JMX是使用 RMI 的单连接 API通过 TCP/IP 套接字。您可以做的最好的事情是串行单播到您的所有实例或使用线程并通过多个 JMX 客户端连接并行发送到您的所有 Web 应用程序 session 。
关于java - 使用 JMX/JMS 向 Web 应用程序的所有 session 广播消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9325315/