ActiveMQ JMS生产者消费者模式收发通用类

本文介绍如何使用ActiveMQ作为JMS提供者,实现生产者和消费者模式,详细阐述了通用类的设计与应用,帮助理解JMS消息传递机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jms提供者为ActiveMQ

 

import java.util.Map;
import java.util.UUID;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.command.ActiveMQQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

/**
 * mq通用类
 * 
 * @author Fu Wei
 * 
 */
@Component
public class ActiveMQQueueCommon {
	private static final Logger LOG = LoggerFactory.getLogger(ActiveMQQueueCommon.class);

	@Autowired
	private JmsTemplate jmsTemplate;

	/**
	 * 异步发送 不支持特定消息
	 * 
	 * @param reqQueue
	 * @param text
	 */
	public void asyncSend(ActiveMQQueue reqQueue, final String text) {
		LOG.debug("发送的XML文内容:{}", text);
		final String correlationId = UUID.randomUUID().toString();
		jmsTemplate.send(reqQueue, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				TextMessage msg = session.createTextMessage(text);
				msg.setJMSCorrelationID(correlationId);
				return msg;
			}
		});
	}

	/**
	 * 异步发送,关联消息id
	 * 
	 * @param reqQueue
	 * @param text
	 * @param propertyName
	 * @param propertyValue 支持一个特定消息
	 */
	public void asyncSend(ActiveMQQueue reqQueue, final String text, final String propertyName,
	        final String propertyValue) {
		LOG.debug("发送的XML文内容:{}", text);
		final String correlationId = UUID.randomUUID().toString();
		jmsTemplate.send(reqQueue, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				TextMessage msg = session.createTextMessage(text);
				msg.setJMSCorrelationID(correlationId);
				msg.setStringProperty(propertyName, propertyValue);
				return msg;
			}
		});
	}

	/**
	 * 异步发送,关联消息id
	 * 
	 * @param reqQueue
	 * @param text
	 * @param propertyName
	 * @param propertyMap 选择器参数
	 */
	public void asyncSend(ActiveMQQueue reqQueue, final String text, final String propertyName,
	        final Map<String, String> propertyMap) {
		LOG.debug("发送的XML文内容:{}", text);
		final String correlationId = UUID.randomUUID().toString();
		jmsTemplate.send(reqQueue, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				TextMessage msg = session.createTextMessage(text);
				msg.setJMSCorrelationID(correlationId);
				if (propertyMap != null && propertyMap.size() > 0) {
					for (Map.Entry<String, String> map : propertyMap.entrySet()) {
						msg.setStringProperty(map.getKey(), map.getValue());
					}
				}
				return msg;
			}
		});
	}

	/**
	 * 同步发送,不带消息特定属性
	 * 
	 * @param reqQueue
	 * @param resQueue
	 * @param messText
	 * @param timeout
	 * @return
	 * @throws JMSException
	 */
	public String syncSend(ActiveMQQueue reqQueue, final ActiveMQQueue resQueue, final String messText, long timeout) {
		return syncSend(reqQueue, resQueue, messText, timeout, null);
	}

	/**
	 * 同步发送,带消息特定属性
	 * 
	 * @param reqQueue
	 * @param resQueue
	 * @param messText
	 * @param timeout
	 * @param propertyName
	 * @param propertyValue
	 * @return
	 * @throws JMSException
	 */
	public String syncSend(ActiveMQQueue reqQueue, final ActiveMQQueue resQueue, final String messText, long timeout,
	        final Map<String, String> propertyMap) {
		LOG.debug("转发的消息:{}, 超时时间:{}", messText, timeout);
		final String correlationId = UUID.randomUUID().toString();
		jmsTemplate.send(reqQueue, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				TextMessage msg = session.createTextMessage(messText);
				msg.setJMSReplyTo(resQueue);
				msg.setJMSCorrelationID(correlationId);
				// 添加消息特定属性
				if (propertyMap != null && propertyMap.size() > 0) {
					for (Map.Entry<String, String> map : propertyMap.entrySet()) {
						msg.setStringProperty(map.getKey(), map.getValue());
					}
				}
				return msg;
			}
		});
		jmsTemplate.setReceiveTimeout(timeout * 1000);
		TextMessage recvMsg = (TextMessage) jmsTemplate.receiveSelected(resQueue, "JMSCorrelationID = '"
		        + correlationId + "'");
		String recvMessText = null;
		try {
			recvMessText = recvMsg.getText();
		} catch (JMSException e) {
			LOG.error("jms错误", e);
		}
		LOG.debug("propertyMap: {}, 返回的信息:{}", propertyMap, recvMessText);
		return recvMessText;
	}
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值