005_Queue消息模式发送字节消息

1. 新建一个名为JMSActiveMQBytesMessage的Java项目, 同时拷入相关jar包

2. 编辑QueueBytesMessageProducer.java

package com.jms.activemq.qbm;

import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class QueueBytesMessageProducer {
	// 默认连接用户名
	private static final String dftUsr = ActiveMQConnection.DEFAULT_USER;
	// 默认用户密码
	private static final String dftPwd = ActiveMQConnection.DEFAULT_PASSWORD;
	// 默认连接地址
	private static final String dftUrl = ActiveMQConnection.DEFAULT_BROKER_URL;
	// 队列名称
	private static final String queueName = "queueBytesMsg";
	
	public static void main(String[] args) {
		// 1. 创建一个连接工厂
		QueueConnectionFactory cf = new ActiveMQConnectionFactory(dftUsr, dftPwd, dftUrl);
		// 连接对象
		QueueConnection conn = null;
		// 会话对象
		QueueSession session = null;
		
		try {
			// 2. 创建连接
			conn = cf.createQueueConnection();
			// 3. 启动连接
			conn.start();
			// 4. 创建会话
			session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
			// 5. 创建消息目的地。如果是点对点, 那么它的实现是Queue; 如果是订阅模式, 那它的实现是Topic。这里我们创建一个名为queueBytesMsg的消息队列。
			Queue queue = session.createQueue(queueName);
			// 6. 消息生产者
			QueueSender sender = session.createSender(queue);
			// 7. 创建文本消息和发送消息
			BytesMessage message = session.createBytesMessage();
			message.writeBytes("JMS BytesMessage消息类型".getBytes());
			sender.send(message);
			System.out.println("JMSDestination = " + message.getJMSDestination() + ", JMSDeliveryMode = " + message.getJMSDeliveryMode()
			 + ", JMSExpiration = " + message.getJMSExpiration() + ", JMSPriority = " + message.getJMSPriority()
			 + ",\r\nJMSMessageID = " + message.getJMSMessageID() + ", JMSTimestamp = " + message.getJMSTimestamp()
			 + ", JMSCorrelationID = " + message.getJMSCorrelationID() + ",\r\nJMSReplyTo = " + message.getJMSReplyTo()
			 + ", JMSType = " + message.getJMSType() + ", JMSRedelivered = " + message.getJMSRedelivered());
		} catch (JMSException e) {
			e.printStackTrace();
		}finally {
			if (session != null) {
				try {
					session.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

3. 运行QueueBytesMessageProducer.java

4. 编辑QueueBytesMessageConsumer.java

package com.jms.activemq.qbm;

import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class QueueBytesMessageConsumer {
	// 默认连接用户名
	private static final String dftUsr = ActiveMQConnection.DEFAULT_USER;
	// 默认用户密码
	private static final String dftPwd = ActiveMQConnection.DEFAULT_PASSWORD;
	// 默认连接地址
	private static final String dftUrl = ActiveMQConnection.DEFAULT_BROKER_URL;
	// 队列名称
	private static final String queueName = "queueBytesMsg";
		
	public static void main(String[] args) {
		// 1. 创建一个连接工厂
		QueueConnectionFactory cf = new ActiveMQConnectionFactory(dftUsr, dftPwd, dftUrl);
		// 连接对象
		QueueConnection conn = null;
		// 会话对象
		QueueSession session = null;
		
		try {
			// 2. 创建连接
			conn = cf.createQueueConnection();
			// 3. 创建会话
			session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
			// 4. 创建消息目的地。如果是点对点, 那么它的实现是Queue; 如果是订阅模式, 那它的实现是Topic。这里我们创建一个名为queueBytesMsg的消息队列。
			Queue queue = session.createQueue(queueName);
			// 5. 消息消费者
			QueueReceiver receiver = session.createReceiver(queue);
			// 6. 启动连接
			conn.start();
			// 7. 接收消息
			BytesMessage message = (BytesMessage) receiver.receive(1000 * 3);
			byte[] bytes = new byte[(int) message.getBodyLength()];
			message.readBytes(bytes);
			System.out.println("接收: " + new String(bytes));
			System.out.println("JMSDestination = " + message.getJMSDestination() + ", JMSDeliveryMode = " + message.getJMSDeliveryMode()
			 + ", JMSExpiration = " + message.getJMSExpiration() + ", JMSPriority = " + message.getJMSPriority()
			 + ",\r\nJMSMessageID = " + message.getJMSMessageID() + ", JMSTimestamp = " + message.getJMSTimestamp()
			 + ", JMSCorrelationID = " + message.getJMSCorrelationID() + ",\r\nJMSReplyTo = " + message.getJMSReplyTo()
			 + ", JMSType = " + message.getJMSType() + ", JMSRedelivered = " + message.getJMSRedelivered());
		} catch (JMSException e) {
			e.printStackTrace();
		} finally {
			try {
				if (session != null) {
					session.close();
				}
			} catch (JMSException e1) {
				e1.printStackTrace();
			} finally {
				if (conn != null) {
					try {
						conn.close();
					} catch (JMSException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}

5. 运行QueueBytesMessageConsumer.java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值