概述


对事务机制进行测试。

测试实例


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

demo设计


设计图

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

测试分工

测试类 测试方法
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

相关文章:

  • 2022-12-23
  • 2021-06-18
  • 2022-12-23
  • 2021-12-29
  • 2021-11-09
  • 2022-12-23
猜你喜欢
  • 2021-11-04
  • 2021-08-27
  • 2021-12-01
相关资源
相似解决方案