ActiveMQF发布订阅模式及Spring整合

Topic主题发布和订阅消息

前面讲的案例都是点对点的消息,即一个生产者发送的一条消息只能被一个消费者消费,然后就移除了。 在这里插入图片描述 而topic模式一条消息可以被多个消费者订阅,关系如下: 在这里插入图片描述

定义生产者

package com.sxt.demo;

import java.net.URI;
import java.util.Date;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQMessage;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.apache.activemq.command.ActiveMQTextMessage;

import com.sxt.bean.User;

/**
 * 定义生产者
 * @author Administrator
 *
 */
public class ActivemqdemoProducer {
	private static String userName="admin";
	private static String password="admin";
	private static String brokerURL="tcp://192.168.119.12:61616";

	public static void main(String[] args) throws Exception {
			TopicConnectionFactory factory = new ActiveMQConnectionFactory(userName,password,brokerURL);
			TopicConnection connection = factory.createTopicConnection();
			connection.start();
			
			TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
			Topic topic = session.createTopic("topic-hello");
			TopicPublisher publisher = session.createPublisher(topic);
			 MapMessage message = session.createMapMessage();
			 message.setString("name", "lisi");
			 message.setString("age", "18");
			publisher.send(message );
			publisher.close();
			session.close();
			connection.close();

	}
}

定义消费者

package com.sxt.demo;

import java.net.URI;
import java.util.Date;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQMessage;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.apache.activemq.command.ActiveMQTextMessage;

import com.sxt.bean.User;

/**
 * 定义消费者
 * @author Administrator
 *
 */
public class ActivemqdemoConsumer {
	private static String userName="admin";
	private static String password="admin";
	private static String brokerURL="tcp://192.168.119.12:61616";

	public static void main(String[] args) throws Exception {
			TopicConnectionFactory factory = new ActiveMQConnectionFactory(userName,password,brokerURL);
			TopicConnection connection = factory.createTopicConnection();
			connection.start();
			
			TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
			Topic topic = session.createTopic("topic-hello");
			TopicPublisher publisher = session.createPublisher(topic);
			TopicSubscriber subscriber = session.createSubscriber(topic);
			subscriber.setMessageListener(new MessageListener() {
				
				@Override
				public void onMessage(Message message) {
					if(message instanceof MapMessage){
						MapMessage mapMessage=(MapMessage)message;
						try {
							String name = mapMessage.getString("name");
							String age = mapMessage.getString("age");
							System.out.println(name+"+"+age);
						} catch (JMSException e) {
							// TODO Auto-generated catch block
							System.out.println("错误信息");
						}
					}
					
				}
			});
			Thread.sleep(30000);
			publisher.close();
			session.close();
			connection.close();

	}
}

测试

先启动消费者 在这里插入图片描述在这里插入图片描述 启动生产者 在这里插入图片描述在这里插入图片描述

ActiveMQ整合Spring框架

整合Spring框架

导入jar包

<dependencies>
	<!-- ActiveMQ客户端完整jar包依赖 -->
	<dependency>
		<groupId>org.apache.activemq</groupId>
		<artifactId>activemq-all</artifactId>
		<version>5.9.0</version>
	</dependency>
	<!-- ActiveMQ和Spring整合配置文件标签处理jar包依赖 -->
	<dependency>
		<groupId>org.apache.xbean</groupId>
		<artifactId>xbean-spring</artifactId>
		<version>4.5</version>
	</dependency>
	<!-- Spring-JMS插件相关jar包依赖 -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jms</artifactId>
		<version>4.1.6.RELEASE</version>
	</dependency>
	<!-- Spring框架上下文jar包依赖 -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>4.1.6.RELEASE</version>
	</dependency>
</dependencies>

定义消息载体对象

package com.sxt.bean;

import java.io.Serializable;

public class User implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String id;
	private String name;
	private String password;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
	}

}

定义生产者

package com.sxt.producer;

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

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

import com.sxt.bean.User;

public class OrderProducer {
		private JmsTemplate template;

		public JmsTemplate getTemplate() {
			return template;
		}

		public void setTemplate(JmsTemplate template) {
			this.template = template;
		}
		/**
		 * 发送消息的方法
		 */
		public void sendOrder(String destinationName,User user){
			template.send(destinationName, new MessageCreator() {
				/**
				 * 模板模式中暴露给调用者的方法
				 */
				@Override
				public Message createMessage(Session session) throws JMSException {
					return session.createObjectMessage(user);
				}
			});
		}

		
}

定义消费者

package com.sxt.consumer;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

import org.apache.activemq.command.ActiveMQObjectMessage;

public class OrderConsumer implements MessageListener{

	@Override
	public void onMessage(Message message) {
		ActiveMQObjectMessage msg = (ActiveMQObjectMessage) message;
		try {
			System.out.println(msg.getObject());
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

Spring配置文件配置

<?xml version="1.0" encoding="UTF-8"?>
<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">
	<!-- ActiveMQ 连接工厂 -->
	<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
	<!-- 需提供访问路径tcp://ip:61616;以及用户名,密码 -->
	<amq:connectionFactory id="amqConnectionFactory"
		brokerURL="tcp://192.168.119.12:61616" userName="admin" password="admin" />

	<!-- Spring Caching连接工厂 -->
	<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
	<bean id="connectionFactory"
		class="org.springframework.jms.connection.CachingConnectionFactory">
		<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
		<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
		<!-- Session缓存数量 -->
		<property name="sessionCacheSize" value="100" />
	</bean>

	<!-- 消息生产者 start -->

	<!-- 定义JmsTemplate对象. 此类型由Spring框架JMS组件提供. 用于访问ActiveMQ使用. -->
	<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
		<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
		<constructor-arg ref="connectionFactory" />
		<!-- 非pub/sub模型(发布/订阅),即队列模式, 默认数据可省略配置 -->
		<!-- <property name="pubSubDomain" value="false" /> -->
	</bean>
	
	<!-- 定义生成者对象 -->
	<bean id="orderProducer" class="com.sxt.producer.OrderProducer">
		<!-- 为属性赋值 -->
		<property name="template" ref="jmsQueueTemplate"></property>
	</bean>
	
	<!--消息生产者 end -->

	<!-- 消息消费者 start -->

	<!-- 定义消息监听器, 此组件为spring-jms组件定义. 可以一次注册若干消息监听器.
		属性解释:
			destination-type - 目的地类型, queue代表消息队列
				可选值: queue | topic | durableTopic
				queue - 默认值. 代表消息队列
				topic - 代表消息队列集合
				durableTopic - 持久化的消息队列集合. ActiveMQ会保证消息的消费者一定接收到此消息.
			container-type - 容器类型
				可选值: default | simple
				default - 默认值. 默认容器类型, 对应DefaultMessageListenerContainer
				simple - 简单容器类型, 对应SimpleMessageListenerContainer
			connection-factory - 链接工厂, 注入的是Spring-JMS组件提供的链接工厂对象.
			acknowledge - 确认方式
				可选值: auto | client | dups-ok | transacted
				auto - 默认值, 即自动确认消息
				client - 客户端确认消息
				dups-ok - 可使用副本的客户端确认消息
				transacted - 有事务的持久化消息确认机制. 需开启对ActiveMQ的事务控制才可应用. 
	 -->
	<jms:listener-container destination-type="queue"
		container-type="default" connection-factory="connectionFactory"
		acknowledge="auto">
		<!-- 注册消息监听器. 如果需要注册多个, 重复定义下述标签. -->
		<jms:listener destination="spring-MQ" ref="orderReciver" />
	</jms:listener-container>
	
	<!-- 容器管理消息监听器实现类对象 -->
	<bean id="orderReciver" class="com.sxt.consumer.OrderConsumer"/>

	<!-- 消息消费者 end -->
</beans>

测试

package com.sxt.test;

import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;

import com.sxt.bean.User;
import com.sxt.producer.OrderProducer;

public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
		OrderProducer bean = ac.getBean(OrderProducer.class);
		User user = new User();
		user.setId("2");
		user.setName("lisi");
		user.setPassword("123456");
		bean.sendOrder("spring-MQ", user);
		System.out.println("消息发送完成");
		
	}
}

在这里插入图片描述在这里插入图片描述

整合完成

转载于:https://my.oschina.net/u/4116655/blog/3048103

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值