允许多个订阅者在同一个Topic Subscription,想象现在消息的吞吐量很大, 在老的版本中我们一个消息只能绑定到一个订阅者。但是消息两很大希望能做到负载均衡类似于Apache。JMS2.0提供这个功能,支持在多个虚拟机中共享一个消息。新建四个订阅者在四个(两组)不同的VM中.
第一组:
Client1-S1:
Context cnx;
try {
cnx = new InitialContext();
final ConnectionFactory cf = (ConnectionFactory) cnx
.lookup("/ConnectionFactory");
final Topic topic = (Topic) cnx.lookup("/topic/test2");
// Invalid concurrent session usage. Sessions are not supposed to be
// used by more than one thread concurrently.
new Thread() {
public void run() {
JMSContext jmsContext = cf.createContext();
JMSConsumer jmsConsumer = jmsContext.createSharedConsumer(
topic, "S1");
while (true) {
Message msg = jmsConsumer.receive();
System.out.println("Client1-S1:"+Thread.currentThread().getId() + ":"
+ msg);
}
};
}.start();
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSRuntimeException jmse) {
jmse.printStackTrace();
}
Client2-S1:
Context cnx;
try {
cnx = new InitialContext();
final ConnectionFactory cf = (ConnectionFactory) cnx
.lookup("/ConnectionFactory");
final Topic topic = (Topic) cnx.lookup("/topic/test2");
// Invalid concurrent session usage. Sessions are not supposed to be
// used by more than one thread concurrently.
new Thread() {
public void run() {
JMSContext jmsContext = cf.createContext("guest", "guest");
JMSConsumer jmsConsumer = jmsContext.createSharedConsumer(
topic, "S1");
while (true) {
Message msg = jmsConsumer.receive();
System.out.println("Client2-S1:"+Thread.currentThread().getId() + ":"
+ msg);
}
};
}.start();
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSRuntimeException jmse) {
jmse.printStackTrace();
}
第二组:
Client3-S2:
Context cnx;
try {
cnx = new InitialContext();
final ConnectionFactory cf = (ConnectionFactory) cnx
.lookup("/ConnectionFactory");
final Topic topic = (Topic) cnx.lookup("/topic/test2");
// Invalid concurrent session usage. Sessions are not supposed to be
// used by more than one thread concurrently.
new Thread() {
public void run() {
JMSContext jmsContext = cf.createContext("guest", "guest");
JMSConsumer jmsConsumer = jmsContext.createSharedConsumer(
topic, "S2");
while (true) {
Message msg = jmsConsumer.receive();
System.out.println("Client3-S2:"+Thread.currentThread().getId() + ":"
+ msg);
}
};
}.start();
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSRuntimeException jmse) {
jmse.printStackTrace();
}
Client4-S2:
Context cnx;
try {
cnx = new InitialContext();
final ConnectionFactory cf = (ConnectionFactory) cnx
.lookup("/ConnectionFactory");
final Topic topic = (Topic) cnx.lookup("/topic/test2");
// Invalid concurrent session usage. Sessions are not supposed to be
// used by more than one thread concurrently.
new Thread() {
public void run() {
JMSContext jmsContext = cf.createContext();
JMSConsumer jmsConsumer = jmsContext.createSharedConsumer(
topic, "S2");
while (true) {
Message msg = jmsConsumer.receive();
System.out.println("Client4-S2:"+Thread.currentThread().getId() + ":"
+ msg);
}
};
}.start();
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSRuntimeException jmse) {
jmse.printStackTrace();
}
分两次发送消息到同一个topic观察输出结果在四个VM控制台.
第一次发送消息:
Client4-S2:12:HornetQMessage[ID:5f856d00-bff1-11e3-86fb-af8e1cdeda67]:PERSISTENT
Client1-S1:12:HornetQMessage[ID:4b696a47-bff1-11e3-8b74-d32c9a2e8eec]:PERSISTENT
第二次发送消息:
Client3-S2:12:HornetQMessage[ID:4b696a47-bff1-11e3-8b74-d32c9a2e8eec]:PERSISTENT
Client2-S1:12:HornetQMessage[ID:5f856d00-bff1-11e3-86fb-af8e1cdeda67]:PERSISTENT