JMS入门,好东西

1 基本信息

摘要:本篇为JMS的学习笔记, 教你一步一步使用JMS,并提供一个能运行的完整的案例,可以使人达到快速入门的目的。

作者:陈光耀

2 正文

  JMS(Java Message Service),是Java消息服务,通过JMS,可以在Java对象之间发送消息。JMS消息支持点对点之间的消息发送,也支持主题/订阅方式的消息发送。

  /** 注: 本笔记中的代码针在jboss4.0.5下运行通过 */

  消息服务由客户和消息代理组成。每位客户都连接到消息服务,客户可以创建消息、发送消息、接收消息、阅读消息。消息服务可以将接收到的消息转发给其他的客户。

  消息服务的关键特点:客户只需要最少的信息就可以其他客户通信(需知道其他客户的提供的服务、服务所需信息和客户地址)。

  消息服务使用转发-存储结构以提交异步信息。

  JMS包含5个元素:

•  提供者: 负责管理消息服务的消息代理
•  客户:  java编写的,利用提供者进行通信的应用程序和组件
•  消息:  在客户之间传输的对象
•  管理的对象: 传输中使用的jms对象,分两种,目标工厂(Destination Factory)对象和连接对象,用于连接消息服务,处理发送者和接收者之间的传输。
•  本机客户: 是在引入jms之前构建的应用程序,它们是采用另外一种消息系统的本机客户API

  在引入jms之前,客户是用 点对点 和 订阅/发布结构

  jms的5个元素:

•  管理对象: 连接工厂(Connection Factory)对象和会话对象;连接工厂对象用于创建会话对象;会话对象用于创建发送者和接收者
   ConnectionFactory(QueueConnectionFactory,TopicConnectionFactoy,XAQueueConnectionFactory,XATopicConnectionFactoy)
   Connection(QueueConnection,TopicConnection,XAQueueConnection,XATopicConnection)
•  会话:  Session
•  消息生成者: MessageProducer(QueueSender, TopicPublisher)
•  消息使用者: MessageConsumer(QueueReciever,TopicSubscriber)
•  消息:  Message

  jms使用步骤:

1 访问连接工厂:

  1. InitialContext ctx=new InitialContext();
  2. TopicConnectionFactory tcf=(TopicConnectionFactory) ctx.lookup("TopicConnectionFactory");
  3. QueueConnectionFactory qcf=(QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");

2 访问目标工厂:创建Queue对象/Topic对象

  1. Topic mt=(Topic) ctx.lookup("topic/testTopic");
  2. Queue mq=(Queue) ctx.lookup("queue/A");

3 创建连接,分两种:队列连接和主题连接

  1. TopicConnection tc=tcf.createTopicConnection();
  2. QueueConnection qc=qcf.createQueueConnection();

4. 开始接收:也可以放在后面进行

  1. tc.start(); //以后需要调用 tc.close(); 关闭连接
  2. qc.start(); //以后需要调用 qc.close(); 关闭连接

5. 创建会话:会话用于创建消息生产者、消息消费者、消息。

  1. QueueSession qSession=qc.createQueueSession(true,0);
  2. TopicSession tSession=tc.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);

参数1:是否使用事务
参数2:消息确认模式

  当消息发送者向消息提供者(即消息代理)发送消息时,消息发送者等待消息代理的确认,没有回应则抛出异常,消息发送程序负责处理这个错误。

  注:消息代理确认只是确认收到了消息,而不是确认消息提交给了消息接收者。

  消息确认模式:JMS使用确认协议以保证消息的发送,使用了3种确认模式

AUTO_ACKNOWLEDGE : 指定消息提供者在每次收到消息时自动发送确认。消息只向目标发送一次,但传输过程中可能因为错误而丢失消息。
CLIENT_ACKNOWLEDGE : 由消息接收者确认收到消息,通过调用消息的acknowledge()方法(会通知消息提供者收到了消息)
DUPS_OK_ACKNOWLEDGE : 指定消息提供者在消息接收者没有确认发送时重新发送消息(这种确认模式不在乎接收者收到重复的消息)。如

   消息提供者在试图向非持久化的消息接收者发送消息失败时,消息会丢失。在向持久化消息接收者发送消息时,会等待消息接收者确认,未收到确认,则重新发送 消息(消息提供者需要设置JMSRedilivered=true,消息接收者需要调用Message对象的getJMSRedelivered()方法 确认JMSRedilivered标记是否为true)

CLIENT_ACKNOWLEDGE 确认收到消息代码:

  1. public void onMessage(Message msg){
  2. try{
  3. //Process incoming messages
  4. msg.acknowledge();
  5. }catch(Exception e){
  6. //handle error
  7. }
  8. }

  消息事务: 包含一组消息,要么全部发送,要不全部都不发送给消息提供者。

  消息提供者缓存消息,如果消息发送者有一个消息发送失败,则调用session.rollback()方法,则消息提供者会放弃前面发送成功的消息;如果全部发送成功,调用session.commit()方法,将消息全部发送给消息接收者。

6 创建消息生产者:

  1. QueueSender qSender=qSession.createSender(mq);
  2. TopicPublisher tPublisher=tSession.createPublisher(mt);
  3. qSender.send(msg);
  4. tPublisher.publish(msg);

7 创建消息使用者:在接收任何消息之前,客户必须注册到JMS提供者表明希望接收消息。注册后,JMS提供者就负责向客户发送消息。

  1. QueueReceiver qr=qSession.createReceiver(mq);
  2. qc.start();
  3. Message msg1=qr.receive();
  4.  
  5. TopicSubscriber tSubscriber=tSession.createSubscriber(mt);
  6. tc.start();
  7. Message msg2=tSubscriber.receive(1500); //每隔1500毫秒从主题接收一次

8 创建消息监听器(实现MessageListener接口onMessage()方法):消息使用者通过消息监听器异步接收消息。

  1. QueueListener qListener=new QueueListener(); //QueueListener为自定义类,实现MessageListener接口
  2. qr.setMessageListener(qListener);
  3.  
  4. TopicListener tListener=new TopicListener(); //TopicListener为自定义类,实现MessageListener接口
  5. tSubscriber.setMessageListener(tListener);

9 消息(Message):三部分,头(必须的),属性和正文 (二者为可选)。

  消息头读写方法: getXXX(),setXXX()

  XXX是字段的名字,许多消息字段是由send()和publish()方法自动设置的,其他自动则由客户或者JMS程序设置。

1) 由send() or publish()设置的:

  1. JMSDestination
  2. JMSDeliveryMode
  3. JMSExpiration
  4. JMSPriority
  5. JMSMessageID
  6. JMSTimestamp
  7. JMSCorrelationID: 关联消息ID
  8. JMSReplyTo
  9. JMSType
  10. JMSRedelivered

2)由客户设置的:

  1. JMSCorrelationID: 关联消息ID
  2. JMSReplyTo
  3. JMSType

3) 由jms提供者设置的:

  1. JMSRedelivered

## 属性读写方法:   getXXX(name), setXXX(name,value)

  消息正文:包含消息,JMS支持6种消息格式,称为消息类型

TextMessage:  有文本组成的String对象
MapMessage:  可以是按顺序或随机访问的 key-value对,key为String, value为primitive
BytesMessage: 字节信息(如存放图像)
StreamMessage: 包含顺序读取值的流
ObjectMessage: 可以序列化的java对象
Message:  无消息正文时可以使用

创建TextMessage消息:

  1. TextMessage msg=qSession.createTextMessage();
  2. msg.setText("myMessage");
  3. qSender.send(msg);

读取TextMessage消息:

  1. Message msg=qr.receive();
  2. if(msg instanceof TextMessage){
  3. TextMessage txtMsg=(TextMessage) msg;
  4. System.out.println("Imcoming message: "+txtMsg.getText());
  5. }else{
  6. //handle error
  7. }

10. 消息选择器:消息使用者使用消息选择器(message selector)选择收到的消息。消息选择器使用条件表达式(符合WHERE子句的SQL-92标准)作为选择条件。
创建并使用消息选择器:

  1. String criteria="Customer='1234'";
  2. TopicSubscriber tSubscriber=ts.createSubscriber(myTopic,criteria,false); //只从主题中接收Customer='1234'的消息

### 向队列发送消息

  1. SendToQueue.java:
  2.  
  3. package test.jms;
  4.  
  5. import java.util.Properties;
  6. import javax.jms.*;
  7. import javax.naming.*;
  8.  
  9. public class SendToQueue {
  10. public static void main(String[] args) {
  11. final int msgCount;
  12. if( (args.length<1) || ( args.length>2)) {
  13. System.out.println("Usage: java test.jms.SendToQueue queueName [sendCount]");
  14. System.exit(1);
  15. }
  16.  
  17. String qName=new String(args[0]);
  18. if(args.length==2) {
  19. msgCount=(new Integer(args[1]).intValue());
  20. }else {
  21. msgCount=1;
  22. }
  23.  
  24. QueueConnection qc=null;
  25. try {
  26. Properties p = new Properties();
  27. p.setProperty(Context.INITIAL_CONTEXT_FACTORY,
  28. "org.jnp.interfaces.NamingContextFactory");
  29. p.setProperty(Context.PROVIDER_URL, "localhost:1099");
  30.  
  31. InitialContext jc=new InitialContext(p);
  32. QueueConnectionFactory qcf=(QueueConnectionFactory)jc.lookup("QueueConnectionFactory");
  33. Queue q=(Queue)jc.lookup(qName);
  34. qc=qcf.createQueueConnection();
  35. QueueSession qs=qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
  36. QueueSender qSender=qs.createSender(q);
  37. TextMessage msg=qs.createTextMessage();
  38. for(int i=0;i<msgCount;i++) {
  39. msg.setText("Welcome number "+(i+1));
  40. qSender.send(msg);
  41. System.out.println("Send Message To "+qName+" : "+msg.getText()+"/n");
  42. }
  43. qSender.close();
  44. qs.close();
  45. }catch(Exception e) {
  46. e.printStackTrace();
  47. }finally {
  48. if(qc!=null) {
  49. try {
  50. qc.close();
  51. }catch(JMSException e) {}
  52. }
  53. }
  54. }
  55. }

运行: java test.jms.SendToQueue queue/A 10

### 从队列接收消息 

  1. ReceiveFromQueue.java:
  2.  
  3. package test.jms;
  4.  
  5. import java.io.*;
  6. import java.util.Properties;
  7. import javax.jms.*;
  8. import javax.naming.*;
  9.  
  10. public class ReceiveFromQueue {
  11. public static void doReceive(String qName) {
  12. Message msg;
  13. TextMessage txtMsg;
  14.  
  15. QueueConnection qc = null;
  16. try {
  17. Properties p = new Properties();
  18. p.setProperty(Context.INITIAL_CONTEXT_FACTORY,
  19. "org.jnp.interfaces.NamingContextFactory");
  20. p.setProperty(Context.PROVIDER_URL, "localhost:1099");
  21.  
  22. InitialContext jc = new InitialContext(p);
  23. QueueConnectionFactory qcf = (QueueConnectionFactory) jc
  24. .lookup("QueueConnectionFactory");
  25. Queue q = (Queue) jc.lookup(qName);
  26. qc = qcf.createQueueConnection();
  27. QueueSession qs = qc.createQueueSession(false,
  28. Session.AUTO_ACKNOWLEDGE);
  29. QueueReceiver qr = qs.createReceiver(q);
  30. qc.start();
  31.  
  32. System.out.println("begin receive messge from " + qName + "...");
  33. msg = qr.receive(1000); // 如果不加间隔参数,会一直等着,知道消息到来。
  34. while (msg != null) {
  35. if (msg instanceof TextMessage) {
  36. txtMsg = (TextMessage) msg;
  37. System.out.println("Receive Msg from " + qName + " : "
  38. + txtMsg.getText());
  39. }
  40. msg = qr.receive(1000);
  41. }
  42. System.out.println("no message available!");
  43.  
  44. qr.close();
  45. qs.close();
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. } finally {
  49. if (qc != null) {
  50. try {
  51. qc.close();
  52. } catch (JMSException e) {
  53. }
  54. }
  55. }
  56. }
  57.  
  58. public static void doListen(String qName) {
  59. Message msg;
  60. TextMessage txtMsg;
  61.  
  62. QueueConnection qc = null;
  63. try {
  64. Properties p = new Properties();
  65. p.setProperty(Context.INITIAL_CONTEXT_FACTORY,
  66. "org.jnp.interfaces.NamingContextFactory");
  67. p.setProperty(Context.PROVIDER_URL, "localhost:1099");
  68.  
  69. InitialContext jc = new InitialContext(p);
  70. QueueConnectionFactory qcf = (QueueConnectionFactory) jc
  71. .lookup("QueueConnectionFactory");
  72. Queue q = (Queue) jc.lookup(qName);
  73. qc = qcf.createQueueConnection();
  74. QueueSession qs = qc.createQueueSession(false,
  75. Session.AUTO_ACKNOWLEDGE);
  76. QueueReceiver qr = qs.createReceiver(q);
  77. qc.start();
  78.  
  79. System.out.println("begin listen to messge from " + qName + "...");
  80. TextListener tListener = new TextListener();
  81. qr.setMessageListener(tListener);
  82. qc.start();
  83. System.out.println("Enter 'q' and press <return> to exit ");
  84. InputStreamReader isr = new InputStreamReader(System.in);
  85.  
  86. char response = '/0';
  87. while (!((response == 'q') || (response == 'Q'))) {
  88. try {
  89. response = (char) isr.read();
  90. } catch (IOException e) {
  91. e.printStackTrace();
  92. }
  93. }
  94. System.out.println("End listening!");
  95.  
  96. qr.close();
  97. qs.close();
  98. } catch (Exception e) {
  99. e.printStackTrace();
  100. } finally {
  101. if (qc != null) {
  102. try {
  103. qc.close();
  104. } catch (JMSException e) {
  105. }
  106. }
  107. }
  108. }
  109.  
  110. public static void main(String[] args) {
  111. if ((args.length != 1)) {
  112. System.out
  113. .println("Usage: java test.jms.ReceiveFromQueue queueName");
  114. System.exit(1);
  115. }
  116.  
  117. String qName = new String(args[0]);
  118.  
  119. doReceive(qName); //通过QueueReceiver.receive()读消息
  120. doListen(qName); // 通过消息监听器读消息
  121. }
  122.  
  123. }

运行: java test.jms.ReceiveFromQueue queue/A

### 向主题发送消息

  1. PublishToTopic.java:
  2. package test.jms;
  3.  
  4. import java.util.Properties;
  5.  
  6. import javax.jms.*;
  7. import javax.naming.*;
  8.  
  9. public class PublishToTopic {
  10. public static void main(String[] args) {
  11. final int msgCount;
  12. if( (args.length<1) || ( args.length>2)) {
  13. System.out.println("Usage: java test.jms.PublishToTopic topicName [sendCount]");
  14. System.exit(1);
  15. }
  16.  
  17. String tName=new String(args[0]);
  18. if(args.length==2) {
  19. msgCount=(new Integer(args[1]).intValue());
  20. }else {
  21. msgCount=10;
  22. }
  23.  
  24. TopicConnection tc=null;
  25. try {
  26. Properties p = new Properties();
  27. p.setProperty(Context.INITIAL_CONTEXT_FACTORY,
  28. "org.jnp.interfaces.NamingContextFactory");
  29. p.setProperty(Context.PROVIDER_URL, "localhost:1099");
  30.  
  31. InitialContext jc=new InitialContext(p);
  32. TopicConnectionFactory tcf=(TopicConnectionFactory)jc.lookup("TopicConnectionFactory");
  33. Topic t=(Topic)jc.lookup(tName);
  34. tc=tcf.createTopicConnection();
  35. TopicSession ts=tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
  36. TopicPublisher tp=ts.createPublisher(t);
  37. TextMessage msg=ts.createTextMessage();
  38. for(int i=0;i<msgCount;i++) {
  39. msg.setText("Welcome number "+(i+1));
  40. tp.publish(msg);
  41. System.out.println("Publish Message To "+tName+" : "+msg.getText()+"/n");
  42. }
  43. tp.close();
  44. ts.close();
  45. }catch(Exception e) {
  46. e.printStackTrace();
  47. }finally {
  48. if(tc!=null) {
  49. try {
  50. tc.close();
  51. }catch(JMSException e) {}
  52. }
  53. }
  54. }
  55. }

运 行: java -classpath d:/jboss-4.0.5/client/jnp-client.jar;d:/jboss-4.0.5/client/jbossmq-client.jar;d:/jboss-4.0.5/client/jboss-j2ee.jar;d:/jboss-4.0.5/client/jbossall-client.jar;. test.jms.PublishToTopic topic/testTopic

### 从主题接收消息 

  1. SubscribeFromTopic.java:
  2. package test.jms;
  3.  
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Properties;
  7.  
  8. import javax.jms.*;
  9. import javax.naming.*;
  10.  
  11. public class SubscribeFromTopic {
  12. public static void doListen(String tName) {
  13. char response='/0';
  14.  
  15. TopicConnection tc=null;
  16. try {
  17. Properties p = new Properties();
  18. p.setProperty(Context.INITIAL_CONTEXT_FACTORY,
  19. "org.jnp.interfaces.NamingContextFactory");
  20. p.setProperty(Context.PROVIDER_URL, "localhost:1099");
  21.  
  22. InitialContext jc=new InitialContext(p);
  23. TopicConnectionFactory tcf=(TopicConnectionFactory)jc.lookup("TopicConnectionFactory");
  24. Topic t=(Topic)jc.lookup(tName);
  25. tc=tcf.createTopicConnection();
  26. TopicSession ts=tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
  27. TopicSubscriber tSubscriber=ts.createSubscriber(t);
  28.  
  29. System.out.println("begin listen to messge from "+tName+"...");
  30. TextListener tListener=new TextListener();
  31. tSubscriber.setMessageListener(tListener);
  32. tc.start();
  33. System.out.println("Enter 'q' and press <return> to exit ");
  34. InputStreamReader isr=new InputStreamReader(System.in);
  35. while(!( (response=='q') || (response=='Q' ))) {
  36. try {
  37. response=(char)isr.read();
  38. }catch(IOException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. System.out.println("End listening!");
  43.  
  44. tSubscriber.close();
  45. ts.close();
  46. }catch(Exception e) {
  47. e.printStackTrace();
  48. }finally {
  49. if(tc!=null) {
  50. try {
  51. tc.close();
  52. }catch(JMSException e) {}
  53. }
  54. }
  55. }
  56.  
  57. public static void doSubscribe(String tName) {
  58. Message msg;
  59. TextMessage txtMsg;
  60. TopicConnection tc=null;
  61. try {
  62. Properties p = new Properties();
  63. p.setProperty(Context.INITIAL_CONTEXT_FACTORY,
  64. "org.jnp.interfaces.NamingContextFactory");
  65. p.setProperty(Context.PROVIDER_URL, "localhost:1099");
  66.  
  67. InitialContext jc=new InitialContext(p);
  68. TopicConnectionFactory tcf=(TopicConnectionFactory)jc.lookup("TopicConnectionFactory");
  69. Topic t=(Topic)jc.lookup(tName);
  70. tc=tcf.createTopicConnection();
  71. TopicSession ts=tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
  72. TopicSubscriber tSubscriber=ts.createSubscriber(t);
  73.  
  74. System.out.println("begin listen to messge from "+tName+"...");
  75.  
  76. System.out.println("begin receive messge from " + tName + "...");
  77. msg = tSubscriber.receive(1000);
  78. while (msg != null) {
  79. if (msg instanceof TextMessage) {
  80. txtMsg = (TextMessage) msg;
  81. System.out.println("Receive Msg from " + tName + " : "
  82. + txtMsg.getText());
  83. }
  84. msg = tSubscriber.receive(1000);
  85. }
  86. System.out.println("no message available!");
  87.  
  88. tSubscriber.close();
  89. ts.close();
  90. }catch(Exception e) {
  91. e.printStackTrace();
  92. }finally {
  93. if(tc!=null) {
  94. try {
  95. tc.close();
  96. }catch(JMSException e) {}
  97. }
  98. }
  99. }
  100.  
  101. public static void main(String[] args) {
  102. if( (args.length!=1)) {
  103. System.out.println("Usage: java test.jms.SubscribeFromTopic topicName");
  104. System.exit(1);
  105. }
  106.  
  107. String tName=new String(args[0]);
  108. doListen(tName); //通过消息监听器接收消息
  109. // doSubscribe(tName); //通过receive()来接收消息
  110. }
  111. }

运 行: java -classpath d:/jboss-4.0.5/client/jnp-client.jar;d:/jboss-4.0.5/client/jbossmq-client.jar;d:/jboss-4.0.5/client/jboss-j2ee.jar;d:/jboss-4.0.5/client/jbossall-client.jar;. test.jms.SubscribeFromTopic topic/testTopic

附:TextListener.java:

  1. SubscribeFromTopic.java:
  2. package test.jms;
  3.  
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Properties;
  7.  
  8. import javax.jms.*;
  9. import javax.naming.*;
  10.  
  11. public class SubscribeFromTopic {
  12. public static void doListen(String tName) {
  13. char response='/0';
  14.  
  15. TopicConnection tc=null;
  16. try {
  17. Properties p = new Properties();
  18. p.setProperty(Context.INITIAL_CONTEXT_FACTORY,
  19. "org.jnp.interfaces.NamingContextFactory");
  20. p.setProperty(Context.PROVIDER_URL, "localhost:1099");
  21.  
  22. InitialContext jc=new InitialContext(p);
  23. TopicConnectionFactory tcf=(TopicConnectionFactory)jc.lookup("TopicConnectionFactory");
  24. Topic t=(Topic)jc.lookup(tName);
  25. tc=tcf.createTopicConnection();
  26. TopicSession ts=tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
  27. TopicSubscriber tSubscriber=ts.createSubscriber(t);
  28.  
  29. System.out.println("begin listen to messge from "+tName+"...");
  30. TextListener tListener=new TextListener();
  31. tSubscriber.setMessageListener(tListener);
  32. tc.start();
  33. System.out.println("Enter 'q' and press <return> to exit ");
  34. InputStreamReader isr=new InputStreamReader(System.in);
  35. while(!( (response=='q') || (response=='Q' ))) {
  36. try {
  37. response=(char)isr.read();
  38. }catch(IOException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. System.out.println("End listening!");
  43.  
  44. tSubscriber.close();
  45. ts.close();
  46. }catch(Exception e) {
  47. e.printStackTrace();
  48. }finally {
  49. if(tc!=null) {
  50. try {
  51. tc.close();
  52. }catch(JMSException e) {}
  53. }
  54. }
  55. }
  56.  
  57. public static void doSubscribe(String tName) {
  58. Message msg;
  59. TextMessage txtMsg;
  60. TopicConnection tc=null;
  61. try {
  62. Properties p = new Properties();
  63. p.setProperty(Context.INITIAL_CONTEXT_FACTORY,
  64. "org.jnp.interfaces.NamingContextFactory");
  65. p.setProperty(Context.PROVIDER_URL, "localhost:1099");
  66.  
  67. InitialContext jc=new InitialContext(p);
  68. TopicConnectionFactory tcf=(TopicConnectionFactory)jc.lookup("TopicConnectionFactory");
  69. Topic t=(Topic)jc.lookup(tName);
  70. tc=tcf.createTopicConnection();
  71. TopicSession ts=tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
  72. TopicSubscriber tSubscriber=ts.createSubscriber(t);
  73.  
  74. System.out.println("begin listen to messge from "+tName+"...");
  75.  
  76. System.out.println("begin receive messge from " + tName + "...");
  77. msg = tSubscriber.receive(1000);
  78. while (msg != null) {
  79. if (msg instanceof TextMessage) {
  80. txtMsg = (TextMessage) msg;
  81. System.out.println("Receive Msg from " + tName + " : "
  82. + txtMsg.getText());
  83. }
  84. msg = tSubscriber.receive(1000);
  85. }
  86. System.out.println("no message available!");
  87.  
  88. tSubscriber.close();
  89. ts.close();
  90. }catch(Exception e) {
  91. e.printStackTrace();
  92. }finally {
  93. if(tc!=null) {
  94. try {
  95. tc.close();
  96. }catch(JMSException e) {}
  97. }
  98. }
  99. }
  100.  
  101. public static void main(String[] args) {
  102. if( (args.length!=1)) {
  103. System.out.println("Usage: java test.jms.SubscribeFromTopic topicName");
  104. System.exit(1);
  105. }
  106.  
  107. String tName=new String(args[0]);
  108. doListen(tName); //通过消息监听器接收消息
  109. // doSubscribe(tName); //通过receive()来接收消息
  110. }
  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值