AMQ学习笔记 - 18. 持久化的测试

时间:2023-03-08 21:57:55

概述


对持久化的有效性进行测试。

测试实例


测试实例 结果预测
持久化递送 重启ActiveMQ后,消息还在队列中
非持久化递送 重启ActiveMQ后,消息不在队列中

demo设计


 jms-producer
|---- src/main/java/
|---- cn.sinobest.asj.producer.jms.deliverymode
|---- ProducerTest.java
|---- sendPersistent():void # 测试持久化递送
|---- sendNoPersistent():void # 测试非持久化递送 

测试结果和步骤


1.持久化递送

测试步骤
  1. 进入ActiveMQ管理页面,删除example.queue
  2. 运行ProducerTest#sendPersistent()
    - 发送消息到example.queue
  3. 进入ActiveMQ管理页面,查看example.queue
    - 有3条消息入队
    - 点击队列的名称,可以看到3条消息的列表,Persistence属性值都是Persistent
  4. 重启ActiveMQ
  5. 进入ActiveMQ管理页面,查看example.queue
    - 3条消息还在队列中
测试结果
符合预期。

2.非持久化递送

测试步骤
  1. 进入ActiveMQ管理页面,删除example.queue
  2. 运行ProducerTest#sendNoPersistent()
    - 发送消息到example.queue
  3. 进入ActiveMQ管理页面,查看example.queue
    - 有3条消息入队
    - 点击队列的名称,可以看到3条消息的列表,Persistence属性值都是Non Persistent
  4. 重启ActiveMQ
  5. 进入ActiveMQ管理页面,查看example.queue
    - 消息已不在队列中
测试结果
符合预期。

代码


ProducerTest.java

 package cn.sinobest.asj.producer.jms.deliverymode;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.junit.Test;
/**
* 基于不同的Delivery Mode的测试类.
* @author lijinlong
*
*/
public class ProducerTest {
/** JNDI name for ConnectionFactory */
static final String CONNECTION_FACTORY_JNDI_NAME = "ConnectionFactory";
/** JNDI name for Queue Destination (use for PTP Mode) */
static final String QUEUE_JNDI_NAME = "exampleQueue"; /** deliveryMode */
int deliveryMode = DeliveryMode.NON_PERSISTENT; /**
* 测试持久模式的消息发送.
*/
@Test
public void sendPersistent() {
setDeliveryMode(DeliveryMode.PERSISTENT);
send(QUEUE_JNDI_NAME);
} /**
* 测试非持久模式的消息发送.
*/
@Test
public void sendNoPersistent() {
setDeliveryMode(DeliveryMode.NON_PERSISTENT);
send(QUEUE_JNDI_NAME);
} public int getDeliveryMode() {
return deliveryMode;
}
private void setDeliveryMode(int deliveryMode) {
this.deliveryMode = deliveryMode;
}
/**
* 发送到指定的目的地.
*
* @param destJndiName
* 目的地的JNDI name.
*/
private void send(String destJndiName) {
Context jndiContext = null;
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
Destination destination = null;
MessageProducer producer = null;
// create a JNDI API IntialContext object
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.out.println("Could not create JNDI Context:"
+ e.getMessage());
System.exit(1);
}
// look up ConnectionFactory and Destination
try {
connectionFactory = (ConnectionFactory) jndiContext
.lookup(CONNECTION_FACTORY_JNDI_NAME);
destination = (Destination) jndiContext.lookup(destJndiName);
} catch (NamingException e) {
System.out.println("JNDI look up failed:" + e.getMessage());
System.exit(1);
}
// send Messages and finally release the resources.
try {
connection = connectionFactory.createConnection();
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(destination);
producer.setDeliveryMode(getDeliveryMode()); TextMessage message = session.createTextMessage();
for (int i = 0; i < 3; i++) {
message.setText(String.format("This is the %dth message.",
i + 1));
producer.send(message);
} } catch (JMSException e) {
e.printStackTrace();
} finally {
try {
if (session != null)
session.close();
if (connection != null)
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}

ProducerTest.java