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

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;
	}
}
 

 

什么是消息 消息是一个用于在组件和应用程序之间通讯的的方法。消息之间的传递是点对点的。任何终端之间都可以相互接受和发送消息。并且每个终端都必须遵守如下的规则 -> 创建消息 -> 发送消息 -> 接收消息 -> 读取消息 为什么要使用消息 理由很简单,消息是一个分布式的低耦合通讯方案。A发送一个消息到一个agent ,B作为接受者去agent上获取消息。但是A,B不需要同时到agent上去注册。agent作为一个中转为A,B提供搞效率的通讯服务。 Java消息服务支持两种消息模型:Point-to-Point消息(P2P)和发布订阅消息(Publish Subscribe messaging,简称Pub/Sub)。JMS规范并不要求供应商同时支持这两种消息模型,但开发者应该熟悉这两种消息模型的优势与缺点。 企业消息产品(或者有时称为面向消息的中间件产品)正逐渐成为公司内操作集成的关 键组件。这些产品可以将分离的业务组件组合成一个可靠灵活的系统。 除了传统的 MOM 供应商,企业消息产品也可以由数据库供应商和许多与网络相关的公 司来提供。 Java 语言的客户端和 Java 语言的中间层服务必须能够使用这些消息系统。JMSJava 语言程序提供了一个通用的方式来获取这些系统。 JMS 是一个接口和相关语义的集合,那些语义定义了 JMS 客户端如何获取企业消息产品 的功能。 由于消息是点对点的,所以 JMS 的所有用户都称为客户端(clients)。JMS 应用由定义 消息的应用和一系列与他们交互的客户端组成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值