AMQ学习笔记 - 17. 事务的测试

概述


对事务机制进行测试。

测试实例


测试实例 结果预测
发送正常 3条消息入队
发送异常 0条消息入队
接收正常 3条消息出队
接收异常 0条消息出队

demo设计


设计图

测试分工

测试类 测试方法
TransactedProducer.java
- 测试发送
sendNormal():void
- 测试“发送正常”
sendIntentional():void
- 测试“发送异常”
TransactedConsumer.java
- 测试接收
receiveNormal():void
- 测试“接收正常”
receiveIntentional():void
- 测试“接收异常”

测试步骤和结果


1.测试发送

1.1.测试发送正常

测试步骤
  1. 在ActiveMQ管理页面,删除example.queue
    - 如果存在就删除
  2. 运行TransactedProducer#sendNormal()
    - 发送消息到example.queue
  3. 查看ActiveMQ管理页面
    - example.queue中有3条消息入队
测试结果
符合预期。

1.2.测试发送异常

测试步骤
  1. 在ActiveMQ管理页面,删除example.queue
    - 如果存在就删除
  2. 运行TransactedProducer#sendIntentional()
    - 发送消息到example.queue
  3. 查看控制台和ActiveMQ管理页面
    - 控制台有异常抛出
    - example.queue中入队消息0
测试结果
符合预期。

2.测试接收

2.1.测试接收正常

测试步骤
  1. 在ActiveMQ管理页面,删除example.queue
    - 如果存在就删除
  2. 运行TransactedProducer#sendNormal()
    - 发送消息到example.queue
  3. 查看ActiveMQ管理页面
    - example.queue中有3条消息入队
  4. 运行TransactedConsumer#receiveNormal()
    - 从example.queue中接收消息
  5. 查看控制台和ActiveMQ管理页面
    - 控制台打印出3条信息
    - 管理页面看到example.queue中的3条消息出队
测试结果
符合预期。

2.2.测试接收异常

测试步骤
  1. 在ActiveMQ管理页面,删除example.queue
    - 如果存在就删除
  2. 运行TransactedProducer#sendNormal()
    - 发送消息到example.queue
  3. 查看ActiveMQ管理页面
    - example.queue中有3条消息入队
  4. 运行TransactedConsumer#receiveIntentional()
    - 从example.queue中接收消息
  5. 查看控制台和ActiveMQ管理页面
    - 控制台打印出3条消息和异常信息
    - 管理页面看到example.queue中出队消息0条
测试结果
符合预期。

代码


文件目录结构

jms-producer
    |---- src/main/resources/
              |---- jndi.properties
    |---- src/main/java/
              |---- cn.sinobest.asj.producer.jms.transaction
                        |---- TransactedProducer.java # 测试发送
jms-consumer
    |---- src/main/resources/
              |---- jndi.properties # 和jms-producer中的jndi.properties一致
    |---- src/main/java/
              |---- cn.sinobest.asj.consumer.jms.transaction
                        |---- TransactedConsumer.java # 测试接收

文件内容

1.jndi.propertie

 1 java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory
 2 
 3 # use the following property to configure the default connector
 4 java.naming.provider.url=tcp://localhost:61616
 5 
 6 # register some queues in JNDI using the form
 7 # queue.[jndiName] = [physicalName]
 8 queue.exampleQueue=example.queue
 9 
10 # register some topics in JNDI using the form
11 # topic.[jndiName] = [physicalName]
12 topic.exampleTopic=example.topic

2.TransactedProducer.java

  1 package cn.sinobest.asj.producer.jms.transaction;
  2 import javax.jms.Connection;
  3 import javax.jms.ConnectionFactory;
  4 import javax.jms.Destination;
  5 import javax.jms.JMSException;
  6 import javax.jms.MessageProducer;
  7 import javax.jms.Session;
  8 import javax.jms.TextMessage;
  9 import javax.naming.Context;
 10 import javax.naming.InitialContext;
 11 import javax.naming.NamingException;
 12 import org.junit.Test;
 13 /**
 14  * 事务的生产者.<br>
 15  * 测试发送正常和发送异常.
 16  * @author lijinlong
 17  *
 18  */
 19 public class TransactedProducer {
 20     /** JNDI name for ConnectionFactory */
 21     static final String CONNECTION_FACTORY_JNDI_NAME = "ConnectionFactory";
 22     /** JNDI name for Queue Destination (use for PTP Mode) */
 23     static final String QUEUE_JNDI_NAME = "exampleQueue";
 24     
 25     /** 是否故意异常 */
 26     boolean intentional = false;
 27     
 28     /**
 29      * 测试发送正常.
 30      */
 31     @Test
 32     public void sendNormal() {
 33         this.intentional = false;
 34         send(QUEUE_JNDI_NAME);
 35     }
 36     
 37     /**
 38      * 测试发送异常.
 39      */
 40     @Test
 41     public void sendIntentional() {
 42         this.intentional = true;
 43         send(QUEUE_JNDI_NAME);
 44     }
 45     
 46     /**
 47      * 是否故意抛出异常.<br>
 48      * 如果不抛出异常,会有3条消息入队;否则没有消息入队.
 49      * @return
 50      */
 51     private boolean isIntentional() {
 52         return intentional;
 53     }
 54     
 55     /**
 56      * 发送到指定的目的地.
 57      * 
 58      * @param destJndiName
 59      *            目的地的JNDI name.
 60      */
 61     private void send(String destJndiName) {
 62         Context jndiContext = null;
 63         ConnectionFactory connectionFactory = null;
 64         Connection connection = null;
 65         Session session = null;
 66         Destination destination = null;
 67         MessageProducer producer = null;
 68         // create a JNDI API IntialContext object
 69         try {
 70             jndiContext = new InitialContext();
 71         } catch (NamingException e) {
 72             System.out.println("Could not create JNDI Context:"
 73                     + e.getMessage());
 74             System.exit(1);
 75         }
 76         // look up ConnectionFactory and Destination
 77         try {
 78             connectionFactory = (ConnectionFactory) jndiContext
 79                     .lookup(CONNECTION_FACTORY_JNDI_NAME);
 80             destination = (Destination) jndiContext.lookup(destJndiName);
 81         } catch (NamingException e) {
 82             System.out.println("JNDI look up failed:" + e.getMessage());
 83             System.exit(1);
 84         }
 85         // send Messages and finally release the resources.
 86         try {
 87             connection = connectionFactory.createConnection();
 88             session = connection.createSession(Boolean.TRUE,
 89                     Session.SESSION_TRANSACTED);
 90             producer = session.createProducer(destination);
 91             
 92             TextMessage message = session.createTextMessage();
 93             for (int i = 0; i < 3; i++) {
 94                 message.setText(String.format("This is the %dth message.",
 95                         i + 1));
 96                 producer.send(message);
 97             }
 98             
 99             if (isIntentional()) {
100                 throw new JMSException("这是一个故意抛出的异常。");
101             }
102             
103             session.commit(); // 在最后提交
104         } catch (JMSException e) {
105             try {
106                 session.rollback();
107             } catch (JMSException e1) {
108                 e1.printStackTrace();
109             }
110             e.printStackTrace();
111         } finally {
112             try {
113                 if (session != null)
114                     session.close();
115                 if (connection != null)
116                     connection.close();
117             } catch (JMSException e) {
118                 e.printStackTrace();
119             }
120         }
121     }
122 }
TransactedProducer.java

3.TransactedConsumer.java

  1 package cn.sinobest.asj.consumer.jms.transaction;
  2 import javax.jms.Connection;
  3 import javax.jms.ConnectionFactory;
  4 import javax.jms.Destination;
  5 import javax.jms.JMSException;
  6 import javax.jms.Message;
  7 import javax.jms.MessageConsumer;
  8 import javax.jms.Session;
  9 import javax.jms.TextMessage;
 10 import javax.naming.Context;
 11 import javax.naming.InitialContext;
 12 import javax.naming.NamingException;
 13 import org.junit.Test;
 14 /**
 15  * 事务的接收者.<br>
 16  * 测试接收正常和接收异常.
 17  * @author lijinlong
 18  *
 19  */
 20 public class TransactedConsumer {
 21     /** JNDI name for ConnectionFactory */
 22     static final String CONNECTION_FACTORY_JNDI_NAME = "ConnectionFactory";
 23     /** JNDI name for Queue Destination (use for PTP Mode) */
 24     static final String QUEUE_JNDI_NAME = "exampleQueue";
 25     
 26     /** 是否故意异常 */
 27     boolean intentional = false;
 28     
 29     @Test
 30     public void receiveNormal() {
 31         intentional = false;
 32         receive(QUEUE_JNDI_NAME);
 33     }
 34     
 35     @Test
 36     public void receiveIntentional() {
 37         intentional = true;
 38         receive(QUEUE_JNDI_NAME);
 39     }
 40     
 41     /**
 42      * 是否故意抛出异常,以检查消息的出队情况.
 43      * @return
 44      */
 45     private boolean isIntentional() {
 46         return intentional;
 47     }
 48     
 49     /**
 50      * 接收消息.<br>
 51      * 
 52      * @param destJndiName
 53      *            目的地的JNDI name.
 54      */
 55     private void receive(String destJndiName) {
 56         Context jndiContext = null;
 57         ConnectionFactory connectionFactory = null;
 58         Connection connection = null;
 59         Session session = null;
 60         Destination destination = null;
 61         MessageConsumer consumer = null;
 62         // create a JNDI API IntialContext object
 63         try {
 64             jndiContext = new InitialContext();
 65         } catch (NamingException e) {
 66             System.out.println("Could not create JNDI Context:"
 67                     + e.getMessage());
 68             System.exit(1);
 69         }
 70         // look up ConnectionFactory and Destination
 71         try {
 72             connectionFactory = (ConnectionFactory) jndiContext
 73                     .lookup(CONNECTION_FACTORY_JNDI_NAME);
 74             destination = (Destination) jndiContext.lookup(destJndiName);
 75         } catch (NamingException e) {
 76             System.out.println("JNDI look up failed:" + e.getMessage());
 77             System.exit(1);
 78         }
 79         // receive Messages and finally release the resources.
 80         try {
 81             connection = connectionFactory.createConnection();
 82             connection.start(); // connection should be called in
 83                                 // receiver-client
 84             session = connection.createSession(Boolean.TRUE,
 85                     Session.SESSION_TRANSACTED);
 86             consumer = session.createConsumer(destination);
 87             
 88             long timeout = 5 * 1000; // timeout:5 seconds
 89             for (Message message = consumer.receive(timeout); message != null; message = consumer.receive(timeout)) {
 90                 String text = ((TextMessage) message).getText();
 91                 System.out.println(text);
 92             }
 93             
 94             if (isIntentional()) {
 95                 throw new JMSException("这是一个故意抛出的异常。");
 96             }
 97             
 98             session.commit();
 99         } catch (JMSException e) {
100             try {
101                 session.rollback();
102             } catch (JMSException e1) {
103                 e1.printStackTrace();
104             }
105             e.printStackTrace();
106         } finally {
107             try {
108                 if (session != null)
109                     session.close();
110                 if (connection != null)
111                     connection.close();
112             } catch (JMSException e) {
113                 e.printStackTrace();
114             }
115         }
116     }
117 }
TransactedConsumer.java

 



来自为知笔记(Wiz)



posted on 2016-04-26 13:52  一尾金鱼  阅读(1453)  评论(0编辑  收藏  举报