Cluster环境中我们有多个server来处理业务,应用代码怎么来通过调用一个JNDI来向jms目标发生消息,
能比较均衡的发送到各个jmsServer的目标上.jmsServer配置和server是一对一的。这时候我们可以配置Distributed Queue 来分发消息。
不过有一种方法是不用Distributed Queue,就是代码直接调用jmsServer目标queue来发消息,然而jmsServer目标queue的jndi是唯一的,那么如果你cluster有8个server那么就得配8个目标queue,代码里也就要写8个目标queue的JNDI。这样就得不尝适。
用了Distributed Queue,配置一个Distributed Queue的JNDI即可。Distributed Queue可以加入多个Members,这些Member都是分别属于各个jmsServer的目标。
我这里配置的是2个server组成的cluster,这里从配置开始记录了测试过程。
还有两个测试发送和接受消息的程序QueueSend.java,QueueReceive.java
1.环境如下:

2.配置JMSStore
这里为了方便我建立的是FileStore,消息是以文件的方式存储。
建立JDBCStore作用是一样的,只是消息存贮在DB里。
3.配置JMS Server


同样方法建立JMSServer-2.
4.配置JMS Modules




5.配置Connection Factory



6.配置queue




这里一定要选择建一个Subdeployment,否则无法部署到jmsserver上。

OK,Queue-1建好了,按照同样的步骤建Queue-2,部署到JMSServer-2.
7.配置Distributed Queue




点击 finish按钮,结束完成。
8.所有配置完成后,可以查看server上的JNDI确认自己配的是否正确。

点击“View JNDI Tree”,会打开新的窗口。

到这里我关于JMS的配置已经完成了,下面我们就可以测试了发消息了。
监控现在消息数,点击JMSServer -> Monitoring -> Active Destinations.


现在的Message Current 都是0.
发送消息的代码配置如下:

注意QUEUE写的是DistributedQueue的JNDI。执行发送消息,然后监控如下:发现消息都发送到了
JMSServer-1的queue中去了。


下面我把DistributedQueue-1成员中的Queue-1删掉,然后再发消息,再继续发消息,发现都发到
Queue2中去了。




这就说明调用公共的DistributedQueue发送消息可以到cluster中任何一个JMSServer上的目标。读消息也是一样的,从DistributedQueue读消息可以读到任何一个JMSServer目标存贮的消息。