JMS入门(六)--DeliveryMode

1.在下面的例子中,分别发送一个Persistent和nonpersistent的消息,然后关闭退出JMS。

发送端代码如下:

/**
 * @author Administrator
 * @description 分别发送一个Persistent和nonpersistent的消息,然后关闭退出JMS
 * 运行这个程序,当输出“Send messages sucessfully!”时,说明两个消息都已经发送成功,然后我们结束它,来停止JMS Provider。
 */
package com.wl.jms;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

public class DeliveryModeSendTest {

	/**
	 * @param args
	 * @throws JMSException 
	 */
	public static void main(String[] args) throws JMSException {
		// TODO Auto-generated method stub
		ActiveMQConnectionFactory factory=new ActiveMQConnectionFactory("vm://localhost");
		Connection connection=factory.createConnection();
		connection.start();
		
		Queue queue=new ActiveMQQueue("testQueue");
		Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		//创建一个消息的生产者
		MessageProducer producer=session.createProducer(queue);
		//设置消息的DeliveryMode为Persistent
		producer.setDeliveryMode(DeliveryMode.PERSISTENT);
		producer.send(session.createTextMessage("A persistent Message"));
		//设置消息的DeliveryMode为Non-Persistent
		producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		producer.send(session.createTextMessage("A non persistent Message"));
		
		System.out.println("Send messages successfully!");
	}

}
运行上面的程序,结果如下:


当输出“Send messages sucessfully!”时,说明两个消息都已经发送成功,然后我们结束它,来停止JMS Provider。
2. 接下来我们重新启动JMS Provicer,然后添加一个消费者:

/**
 * @author Administrator
 * @description 这个程序是基于DeliveryModeSendTest基础上来做的
 * 运行上面的程序,可以得到下面的输出结果:Consumer get:A persistent Message
 * 可以看出消息消费者只接收到一个消息,它是一个Persistent的消息。而刚才发送的non persistent消息已经丢失了。
 */
package com.wl.jms;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

public class DeliveryModeReceiveTest {

	/**
	 * @param args
	 * @throws JMSException 
	 */
	public static void main(String[] args) throws JMSException {
		// TODO Auto-generated method stub
		ActiveMQConnectionFactory factory=new ActiveMQConnectionFactory("vm://localhost");
		Connection connection=factory.createConnection();
		connection.start();
		
		Queue queue=new ActiveMQQueue("testQueue");
		Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		//创建消息的接收者,来接受DeliveryModeSendTest中发送的消息
		MessageConsumer consumer=session.createConsumer(queue);
		consumer.setMessageListener(new MessageListener(){

			public void onMessage(Message m) {
				// TODO Auto-generated method stub
				try {
					System.out.println("Consumer get: "+((TextMessage)m).getText());
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		});
	}

}
运行这个接受端的程序,结果如下:


可以看出消息消费者只接收到一个消息,它是一个Persistent的消息。而刚才发送的non persistent消息已经丢失了。
另外, 如果发送一个non persistent消息, 而刚好这个时候没有消费者在监听, 这个消息也会丢失.


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值