一个简单的JMS实例(使用ActiveMQ)

原文地址:http://blog.****.net/robinjwong/article/details/38820259

选择ActiveMQ。

建立一个简单的Maven工程,pom.xml如下:

[html] view plain copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   
  5.   <groupId>com.gof</groupId>  
  6.   <artifactId>jms-test</artifactId>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <packaging>jar</packaging>  
  9.   
  10.   <name>jms-test</name>  
  11.   <url>http://maven.apache.org</url>  
  12.   
  13.   <properties>  
  14.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.   </properties>  
  16.   
  17.   <dependencies>  
  18.     <dependency>  
  19.       <groupId>junit</groupId>  
  20.       <artifactId>junit</artifactId>  
  21.       <version>3.8.1</version>  
  22.       <scope>test</scope>  
  23.     </dependency>  
  24.       
  25.     <!-- Add by WXB -->  
  26.     <dependency>  
  27.       <groupId>org.apache.activemq</groupId>  
  28.       <artifactId>activemq-client</artifactId>  
  29.       <version>5.10.0</version>  
  30.     </dependency>  
  31.       
  32.   </dependencies>  
  33. </project>  

创建测试类:

[java] view plain copy
  1. package com.gof.jms.test;  
  2.   
  3. import javax.jms.Connection;  
  4. import javax.jms.ConnectionFactory;  
  5. import javax.jms.DeliveryMode;  
  6. import javax.jms.Destination;  
  7. import javax.jms.Message;  
  8. import javax.jms.MessageConsumer;  
  9. import javax.jms.MessageProducer;  
  10. import javax.jms.Session;  
  11. import javax.jms.TextMessage;  
  12.   
  13. import org.apache.activemq.ActiveMQConnectionFactory;  
  14.   
  15. public class SimpleMessageSendandReceiveApp {  
  16.     public static final String user = "system";  
  17.     public static final String password = "manager";  
  18.     public static final String url = "tcp://localhost:61616";  
  19.     public static final String queueName = "test_queue";  
  20.     public static final String messageBody = "Hello JMS!";  
  21.     public static final boolean transacted = false;  
  22.     public static final boolean persistent = false;  
  23.       
  24.     public static void main(String[] args){  
  25.         Connection connection = null;  
  26.         Session session = null;  
  27.           
  28.         try{  
  29.             // create the connection  
  30.             ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);  
  31.             connection = connectionFactory.createConnection();  
  32.             connection.start();  
  33.               
  34.             // create the session  
  35.             session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);  
  36.             Destination destination = session.createQueue(queueName);  
  37.               
  38.             // create the producer  
  39.             MessageProducer producer = session.createProducer(destination);  
  40.             if (persistent){  
  41.                 producer.setDeliveryMode(DeliveryMode.PERSISTENT);  
  42.             }else{  
  43.                 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);  
  44.             }  
  45.               
  46.             // create text message  
  47.             Message message = session.createTextMessage(messageBody);  
  48.               
  49.             // send the message  
  50.             producer.send(message);  
  51.             System.out.println("Send message: " + ((TextMessage)message).getText());  
  52.               
  53.             // create the consumer  
  54.             MessageConsumer consumer = session.createConsumer(destination);  
  55.             // blocking till receive the message  
  56.             Message recvMessage = consumer.receive();  
  57.             System.out.println("Receive message: " + ((TextMessage)recvMessage).getText());  
  58.               
  59.         }catch (Exception e){  
  60.             e.printStackTrace();  
  61.         }finally{  
  62.             try{  
  63.                 // close session and connection  
  64.                 if (session != null){  
  65.                     session.close();  
  66.                 }  
  67.                 if (connection != null){  
  68.                     connection.close();  
  69.                 }  
  70.             }catch (Exception e){  
  71.                 e.printStackTrace();  
  72.             }  
  73.         }  
  74.     }  
  75. }
  76.   


开始测试:

启动ActiveMQ,在admin页面(http://localhost:8161/admin)中创建名为test_queue的Queue:

一个简单的JMS实例(使用ActiveMQ)


分步运行程序,当调用:

[java] view plain copy
  1. producer.send(message);  

可以看到ActiveMQ接收到了这个消息:

一个简单的JMS实例(使用ActiveMQ)

一个简单的JMS实例(使用ActiveMQ)

同时也可以看到该Queue当前的Active Producer:

一个简单的JMS实例(使用ActiveMQ)

当调用:

[java] view plain copy
  1. consumer.receive();  

可以看到该消息被消费掉:

一个简单的JMS实例(使用ActiveMQ)


在控制台中,可以看到输出的结果:

[html] view plain copy
  1. Send message: Hello JMS!  
  2. Receive message: Hello JMS!