ActiveMQ与Spring整合
与Spring整合开发的话,非常方便。
1. pom.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/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>org.ygy</groupId>
- <artifactId>activemq</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>activemq</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <spring.version>3.1.1.RELEASE</spring.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.10</version>
- <scope>test</scope>
- </dependency>
- <!-- activemq,学习中 -->
- <dependency>
- <groupId>org.apache.activemq</groupId>
- <artifactId>activemq-core</artifactId>
- <version>5.7.0</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.5.6</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.5.6</version>
- </dependency>
- <!-- 与Spring集成 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jms</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.activemq</groupId>
- <artifactId>activemq-spring</artifactId>
- <version>5.8.0</version>
- </dependency>
- </dependencies>
- </project>
2. 生产者
- package org.ygy.mq.lesson03;
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.Session;
- import org.springframework.jms.core.JmsTemplate;
- import org.springframework.jms.core.MessageCreator;
- /**
- *
- * @author yuguiyang
- *
- */
- public class SpringProducer {
- //Spring的模板,封装了很多功能
- private JmsTemplate jmsTemplate;
- public void setJmsTemplate(JmsTemplate jmsTemplate) {
- this.jmsTemplate = jmsTemplate;
- }
- public void send() {
- //使用JMSTemplate可以很简单的实现发送消息
- jmsTemplate.send(new MessageCreator() {
- public Message createMessage(Session session) throws JMSException {
- return session.createTextMessage("地瓜!地瓜!");
- }
- });
- }
- }
3. 消费者
- package org.ygy.mq.lesson03;
- import javax.jms.TextMessage;
- import org.springframework.jms.core.JmsTemplate;
- /**
- *
- * @author yuguiyang
- * @description 消费者
- * @time 2013-10-14
- * @version V1.0
- */
- public class SpringConsumer {
- private JmsTemplate jmsTemplate;
- public void setJmsTemplate(JmsTemplate jmsTemplate) {
- this.jmsTemplate = jmsTemplate;
- }
- //接收消息
- public void recive() {
- while (true) {
- try {
- //使用JMSTemplate接收消息
- TextMessage txtmsg = (TextMessage) jmsTemplate.receive();
- if (null != txtmsg) {
- System.out.println("--- 收到消息内容为: " + txtmsg.getText());
- } else {
- break;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
4. 配置文件
其他的代码基本上就是Spring的配置文件了
- <?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:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <!-- 1.配置connectionFactory -->
- <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
- <property name="connectionFactory">
- <bean class="org.apache.activemq.ActiveMQConnectionFactory">
- <property name="brokerURL">
- <value>tcp://127.0.0.1:61616</value>
- </property>
- </bean>
- </property>
- <property name="maxConnections" value="100"></property>
- </bean>
- <!-- 2.定义消息目标 -->
- <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
- <constructor-arg index="0" value="queue_temp"></constructor-arg>
- </bean>
- <!-- 3.配置Spring JMS Template -->
- <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
- <!-- 3.1 注入jmsFactory -->
- <property name="connectionFactory" ref="jmsFactory"></property>
- <!-- 3.2 配置消息目标 -->
- <property name="defaultDestination" ref="destination" />
- </bean>
- <!-- 4.配置生产者 -->
- <bean id="springProducer" class="org.ygy.mq.lesson03.SpringProducer">
- <property name="jmsTemplate" ref="jmsTemplate"/>
- </bean>
- <!-- 5.配置消费者 -->
- <bean id="springConsumer" class="org.ygy.mq.lesson03.SpringConsumer">
- <property name="jmsTemplate" ref="jmsTemplate"></property>
- </bean>
- </beans>
5. 测试
- package org.ygy.mq.lesson03;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * ActiveMQ与Spring整合 ===初步整合
- * 1.消费者
- * 2.生产者
- * 3.配置文件
- *
- * @author Administrator
- *
- */
- public class JMSTest {
- public static void main(String[] args) {
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-jms.xml");
- SpringProducer producer = (SpringProducer)applicationContext.getBean("springProducer");
- producer.send();
- SpringConsumer consumer = (SpringConsumer) applicationContext.getBean("springConsumer");
- consumer.recive();
- }
- }