使用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,它会起作用。我在这里缺少了什么,这行吗?

我认为简短的答案是否定的。JMX是一个单连接API,它通过TCP/IP套接字使用RMI。您所能做的最好的事情是串行单播到您的所有实例,或者使用线程,并通过多个JMX客户端连接并行发送到所有web应用程序会话。

最新更新