spring对消息队列的支持

具体配置如下

poi.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringJMS</groupId>
<artifactId>SpringJMS</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringJMS Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jms -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-oxm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.8.RELEASE</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-core -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>SpringJMS</finalName>
</build>
</project>

生产者

package com.julongtech.producer;

/**
* 生产者接口服务
* @author julong
* @date 2017-9-18 下午10:27:58
*/
public interface ProducerService {

public abstract void send(String message);
}
package com.julongtech.producer.impl;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import com.julongtech.producer.ProducerService;

/**
* 生产者
* @author julong
* @date 2017-9-18 下午10:28:21
*/
public class ProducerServiceImpl implements ProducerService {

/**
* 配置jms模板类
* @author julong
* @date 2017-9-17 下午6:30:58
*/
@Autowired
private JmsTemplate jmsTemplate;
@Resource(name="activeMQQueue")
private Destination destination;

@Override
public void send(final String message) {
// TODO Auto-generated method stub
this.jmsTemplate.send(destination, new MessageCreator() {

@Override
public Message createMessage(Session session) throws JMSException {
// TODO Auto-generated method stub
//创建消息对象
TextMessage textMessage = session.createTextMessage(message);
System.out.println("消息发送:"+textMessage);
return textMessage;
}
});
}


}

生产者配置文件 producer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 开启自动注解 -->
<context:annotation-config></context:annotation-config>
<!-- 创建activemq 连接工厂对象 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- 连接地址 -->
<property name="brokerURL" value="tcp://127.0.0.1:61616"></property>
</bean>

<!-- 创建spring jms 工厂对象 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 引入activemq工厂对象 -->
<property name="targetConnectionFactory" ref="targetConnectionFactory"></property>
</bean>
<!-- 队列的目的地 -->
<bean id="activeMQQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue"></constructor-arg>
</bean>
<!-- jms模板对象 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
</bean>

<bean id="producerService" class="com.julongtech.producer.impl.ProducerServiceImpl"></bean>
</beans>

消费者consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- 创建activemq 连接工厂对象 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- 连接地址 -->
<property name="brokerURL" value="tcp://127.0.0.1:61616"></property>
</bean>

<!-- 创建spring jms 工厂对象 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 引入activemq工厂对象 -->
<property name="targetConnectionFactory" ref="targetConnectionFactory"></property>
</bean>
<!-- 队列的目的地 -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue"></constructor-arg>
</bean>
<!-- 配置消息监听器 -->
<bean id="messageListener" class="com.julongtech.consumer.ConsumerMessageListenerContainer">
</bean>
<!-- 配置消息容器 -->
<bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="destination" ref="destination"></property>
<property name="messageListener" ref="messageListener"></property>
</bean>
</beans>
package com.julongtech.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* 消费者测试类
* @author julong
* @date 2017-9-18 下午10:33:03
*/
public class ConsumerTest {

/**
* @param args
* @author julong
* @date 2017-9-17 下午7:12:11
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
}

}
package com.julongtech.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.julongtech.producer.ProducerService;

/**
* 生产者测试类
* @author julong
* @date 2017-9-18 下午10:32:51
*/
public class ProducerTest {

public static void main(String[] args){

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("producer.xml");

ProducerService producerService = (ProducerService) applicationContext.getBean("producerService");
for (int i = 0; i < 100; i++) {
producerService.send("发送消息"+i+"次");
}
//清理资源
applicationContext.close();
}
}