不说废话,直接上代码
一,生产者配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-4.0.xsd">
<bean id="connectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<description>JMS连接工厂</description>
<property name="targetConnectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${mq.brokerURL}" />
<property name="userName" value="${mq.userName}" />
<property name="password" value="${mq.password}" />
</bean>
</property>
<property name="sessionCacheSize" value="${mq.sessionCacheSize}" />
</bean>
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<description>队列模式模型</description>
<constructor-arg ref="connectionFactory" />
<property name="receiveTimeout" value="${mq.receiveTimeout}" />
<property name="pubSubDomain" value="false" />
</bean>
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<description>发布/订阅模式模型</description>
<constructor-arg ref="connectionFactory" />
<property name="receiveTimeout" value="${mq.receiveTimeout}" />
<property name="pubSubDomain" value="true" />
</bean>
</beans>
二,消费者配置XML文件:<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-4.0.xsd">
<!-- 定义Queue监听器 -->
<jms:listener-containerdestination-type="queue"
container-type="default"connection-factory="connectionFactory"
acknowledge="auto"><!-- 可写多个监听器 -->
<jms:listenerdestination="Lily.queue"ref="queueMessageListener"/>
</jms:listener-container>
<!-- 定义Topic监听器 -->
<jms:listener-containerdestination-type="topic"
container-type="default"connection-factory="connectionFactory"
acknowledge="auto"><!-- 可写多个监听器 -->
<jms:listenerdestination="Lily.topic"ref="topicMessageListener"/>
</jms:listener-container>
</beans>三、生产者代码类:
A(队列模式):
package com.innotek.core.support.mq;B(发布、订阅者模式):
import java.io.Serializable;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
/**
* 队列消息发送类
* @author ShenHuaJie
* @version 2016年5月20日 下午3:19:19
*/
@Component
public class QueueSender {
@Autowired
@Qualifier("jmsQueueTemplate")
private JmsTemplate jmsTemplate;
/**
* 发送一条消息到指定的队列(目标)
*
* @param queueName 队列名称
* @param message 消息内容
*/
public void send(String queueName, final Serializable message) {
jmsTemplate.send(queueName, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createObjectMessage(message);
}
});
}
}package com.innotek.core.support.mq;
import java.io.Serializable;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
/**
* @author ShenHuaJie
* @version 2016年5月20日 下午3:19:19
*/
@Component
public class TopicSender {
@Autowired
@Qualifier("jmsTopicTemplate")
private JmsTemplate jmsTemplate;
/**
* 发送一条消息到指定的队列(目标)
*
* @param queueName 队列名称
* @param message 消息内容
*/
public void send(String topicName, final Serializable message) {
jmsTemplate.send(topicName, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createObjectMessage(message);
}
});
}
}四,消息消费者:
A(队列模式消费者):
package com.innotek.service.mq.queue;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
@Service
public class QueueMessageListener implements MessageListener {
private final Logger logger = LogManager.getLogger();
public void onMessage(Message message) {
try {
System.out.println("QueueMessageListener接收到消息:" + ((ObjectMessage) message).getObject());
} catch (JMSException e) {
logger.error(e);
}
}
}
B(发布订阅者模式-消费者):package com.innotek.service.mq.topic;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
@Service
public class TopicMessageListener implements MessageListener {
private final Logger logger = LogManager.getLogger();
public void onMessage(Message message) {
try {
System.out.println("TopicMessageListener接收到消息:" + ((ObjectMessage) message).getObject());
} catch (JMSException e) {
logger.error(e);
}
}
}
具体详情可以参考其他内容。