activeMQ 与 Spring 整合

activeMQ的作用:当我们去处理某些数据的时候,添加或者删除等给数据做出了一些修改,我们需要对关联这些数据的地方做出相应的修改,使用activeMQ消息队列可以帮助我们实现这一功能。运用到的JAVA的设计模式也就是我们所说的观察者模式,当然,消息队列中体现了一些其他的设计模式,比如装饰者模式等。

观察者模式:观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己。
观察者模式在对象之间存在一对多关系时使用,例如,如果一个对象被修改,它的依赖对象所有观察者将被自动通知。

整合的配置 applicationContext-myactivemq.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	
	<bean id="targetConnection" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://192.168.25.128:61616"></property>
	</bean>
	
	<!-- 通用的connectionfacotry 指定真正使用的连接工厂 -->
	<!-- 如果后面也有新的实现方式,只需添加新的实现,不用修改原来的实现,修改通用的connectionfacotry即可 -->
	<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
		<property name="targetConnectionFactory" ref="targetConnection"></property>
	</bean>
	
	
	<!-- 接收和发送消息时使用的类 -->
	<bean class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="connectionFactory"></property>
	</bean>
	
	<!-- destination   queue实现方式 -->
	<!-- <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg name="name" value="item-change-queue"></constructor-arg>
	</bean> --> 
	
	<!-- destination   topic实现方式 -->
	<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
		<constructor-arg name="name" value="item-change-topic"></constructor-arg>
	</bean>
	
	
	<bean id="myMessageListener" class="com.itheima.myactivemq.myspring.MyMessageListenner02"></bean>
	<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory"></property>
		<property name="destination" ref="topicDestination"></property>
		<property name="messageListener" ref="myMessageListener"></property>
	</bean>
	
	
	<bean id="myMessageListener2" class="com.itheima.myactivemq.myspring.MyMessageListenner02"></bean>
	<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory"></property>
		<property name="destination" ref="topicDestination"></property>
		<property name="messageListener" ref="myMessageListener2"></property>
	</bean>
</beans>

消费者监听:
 

public class MyMessageListenner02 implements MessageListener {
	
	@Override
	public void onMessage(Message message) {
		
		
		if(message instanceof TextMessage){
			TextMessage textMessage = (TextMessage)message;
			try {
				System.out.println(this+textMessage.getText());
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}

	}

}

生产者:

public class MyProducer {

	private static ApplicationContext applicationContext;

	public static void main(String[] args) {
		
		applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext-myactivemq.xml");
		JmsTemplate template = applicationContext.getBean(JmsTemplate.class);
		ActiveMQDestination destination = applicationContext.getBean(ActiveMQDestination.class);
		
		
		template.send(destination, new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				
				TextMessage message = session.createTextMessage("spring 发送的消息 2");
				return message;
			}
		});

	}

}

 

配置中开启了两个监听器,对于topic,发布——订阅模式,一个消息可以被多个消费者消费
执行生产者,结果如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值