JAVA j2se支持jms_HornetQ之JMS2.0 (实例讲解)

允许多个订阅者在同一个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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值