ActiveMQ整合Spring框架

  前面文章介绍了ActiveMQ的相关内容,本文介绍ActiveMQ和Spring的整合开发

整合Spring框架

1.相关jar包

activemq-all-5.9.0.jar
aopalliance-1.0.jar
commons-logging-1.2.jar
spring-aop-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jms-4.1.6.RELEASE.jar
spring-messaging-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
xbean-spring-4.5.jar

相关maven坐标

<dependencies>
	
	<dependency>
		<groupId>org.apache.activemqgroupId>
		<artifactId>activemq-allartifactId>
		<version>5.9.0version>
	dependency>
	
	<dependency>
		<groupId>org.apache.xbeangroupId>
		<artifactId>xbean-springartifactId>
		<version>4.5version>
	dependency>
	
	<dependency>
		<groupId>org.springframeworkgroupId>
		<artifactId>spring-jmsartifactId>
		<version>4.1.6.RELEASEversion>
	dependency>
	
	<dependency>
		<groupId>org.springframeworkgroupId>
		<artifactId>spring-contextartifactId>
		<version>4.1.6.RELEASEversion>
	dependency>
dependencies>

2.定义消费者

package com.dpb.consumer;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import com.dpb.bean.Order;
/**
 * ActiveMQ Consumer消费者
 * @author dengp
 *
 */
public class OrderConsumer implements MessageListener{

	@Override
	public void onMessage(Message message) {
		try{
			ObjectMessage objectMassage = (ObjectMessage) message;
			Order order = (Order) objectMassage.getObject();
			System.out.println("the order is : " + order);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

3.定义生产者

package com.dpb.producer;

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

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import com.dpb.bean.Order;

/**
 * ActiveMQ producer 生产者
 * @author dengp
 *
 */
public class OrderProducer {

	private JmsTemplate template;
	
	public JmsTemplate getTemplate() {
		return template;
	}

	public void setTemplate(JmsTemplate template) {
		this.template = template;
	}

	/**
	 * 生产者发送消息
	 * @param destinationName  目的地名称
	 * @param order  需要发送的订单数据
	 */
	public void sendOrder(String destinationName, Order order){
		try{
			template.send(destinationName, new MessageCreator() {
				
				/**
				 * 通过模板模式暴露的方法设置发送的信息
				 */
				@Override
				public Message createMessage(Session session) throws JMSException {
					ObjectMessage objectMessage = session.createObjectMessage(order);
					return objectMessage;
				}
			});
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException("send order to MQ server error!!");
		}
	}
}

4.Spring配置文件整合ActiveMQ


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:amq="http://activemq.apache.org/schema/core"
	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  
        http://activemq.apache.org/schema/core  
        http://activemq.apache.org/schema/core/activemq-core-5.9.0.xsd">
    
	<context:component-scan base-package="com.dpb.*">context:component-scan>
	
	
	
	
	<amq:connectionFactory id="amqConnectionFactory"
		brokerURL="tcp://192.168.88.121:61616" userName="admin" password="admin" />

	
	
	<bean id="connectionFactory"
		class="org.springframework.jms.connection.CachingConnectionFactory">
		
		<property name="targetConnectionFactory" ref="amqConnectionFactory">property>
		
		<property name="sessionCacheSize" value="100" />
	bean>

	

	
	<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
		
		<constructor-arg ref="connectionFactory" />
		
		
	bean>
	
	
	<bean id="orderProducer" class="com.dpb.producer.OrderProducer">
		
		<property name="template" ref="jmsQueueTemplate">property>
	bean>
	
	

	

	
	<jms:listener-container destination-type="queue"
		container-type="default" connection-factory="connectionFactory"
		acknowledge="auto">
		
		<jms:listener destination="test-spring" ref="orderReciver" />
	jms:listener-container>
	
	
	<bean id="orderReciver" class="com.dpb.consumer.OrderConsumer"/>

	
beans>

5.测试

public class Test {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		OrderProducer pro = ac.getBean("orderProducer",OrderProducer.class);
		Order order = new Order();
		order.setId("101");
		order.setNick("波波烤鸭");
		order.setPrice(10000L);
		order.setCreateTime(new Date());
		pro.sendOrder("test-spring", order);
	}
}

在这里插入图片描述
ActiveMQ整合Spring框架_第1张图片
整合完成~~

你可能感兴趣的:(消息中间件专栏)