activeMQ测试demo

一、安装MQ

官网下载activeMQ的Linux 包,这里使用的是apache-activemq-5.12.0-bin.tar.gz,安装过程很简单,包复制到指定目录下并解压即可,不许其他配置;

解压后的文件

进入解压后的文件夹

启动MQ:进入bin文件夹,运行activemq就行

如何检查是否测试成功呢,访问http://192.168.25.128:8161/admin/,账号密码都是admin,若能打开activeMq管理后台,说明就是启动成功的;ip是你安装mq机器的IP;

注意:这里有的会出现可以访问MQ管理后台页面,但是点击页面内头部菜单QUEUES时。会提示503错误,那就是linux机器的设置问题了,需要将你的linux用户名和127.0.0.1做一下映射:然后重启MQ;

二、编写项目测试代码

创建maven项目,引入MQjar包;这里的版本号使用5.11.2,5.12.0版本的jar包会出现spring的jar冲突;

<!-- activemq的jar包 -->
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-all</artifactId>
            <version>5.11.2</version>
		</dependency>

编写测试代码,包括了queue,topic两种方式的测试

package com.taotao.fastdfs;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;
import org.springframework.jca.cci.connection.ConnectionFactoryUtils;

public class ActiveMqTest {
	
	@Test
	public void mqProduce(){
		//创建链接工厂
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.128:61616");
		//使用工厂创建链接对象
		try {
			Connection connection = connectionFactory.createConnection();
			//开启链接
			connection.start();
			//连接对象创建session
			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			//创建目的地对象,设置queue ,topic消费方式
			Queue queue = session.createQueue("test-queue");
			//使用session创建生产者对象
			MessageProducer messageProducer = session.createProducer(queue);
			//创建消息对象
			TextMessage message = session.createTextMessage("hello world 这是我发送的第3个消息!不要回答!不要回答!不要回答!");
			//生成这发送消息
			messageProducer.send(message);
			//发送成功后关闭资源
			messageProducer.close();
			session.close();
			connection.close();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Test
	public void queueConsumer(){
		//创建链接工厂
				ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.128:61616");
				//使用工厂创建链接对象
				try {
					Connection connection = connectionFactory.createConnection();
					//开启链接
					connection.start();
					//连接对象创建session
					Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
					//创建目的地对象,目的地名称必须和生产者保持一致
					Queue queue = session.createQueue("test-queue");
					//创建消费者
					MessageConsumer messageConsumer = session.createConsumer(queue);
					//消费者对象设置消息监听,用来接收消息
					messageConsumer.setMessageListener(new MessageListener() {
						
						@Override
						public void onMessage(Message message) {
							// TODO Auto-generated method stub
							//这里用来获取消息
							if(message instanceof TextMessage){
								TextMessage textMessage = (TextMessage)message;
								try {
									String msg = textMessage.getText();
									System.out.println(msg);
								} catch (JMSException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						}
					});
					//键盘输入就关闭资源
					System.in.read();
					//发送成功后关闭资源
					messageConsumer.close();
					session.close();
					connection.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
	}
	
	@Test
	public void topicProduce(){
		//创建链接工厂
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.128:61616");
		//使用工厂创建链接对象
		try {
			Connection connection = connectionFactory.createConnection();
			//开启链接
			connection.start();
			//连接对象创建session
			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			//创建目的地对象,设置queue ,topic消费方式
			Topic queue = session.createTopic("test-topic");
			//使用session创建生产者对象
			MessageProducer messageProducer = session.createProducer(queue);
			//创建消息对象
			TextMessage message = session.createTextMessage("hello world 这是我发送的第2个消息!不要回答!不要回答!不要回答!");
			//生成这发送消息
			messageProducer.send(message);
			//发送成功后关闭资源
			messageProducer.close();
			session.close();
			connection.close();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Test
	public void topicConsumer(){
		//创建链接工厂
				ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.128:61616");
				//使用工厂创建链接对象
				try {
					Connection connection = connectionFactory.createConnection();
					//开启链接
					connection.start();
					//连接对象创建session
					Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
					//创建目的地对象,目的地名称必须和生产者保持一致
					Topic queue = session.createTopic("test-topic");
					//创建消费者
					MessageConsumer messageConsumer = session.createConsumer(queue);
					//消费者对象设置消息监听,用来接收消息
					messageConsumer.setMessageListener(new MessageListener() {
						
						@Override
						public void onMessage(Message message) {
							// TODO Auto-generated method stub
							//这里用来获取消息
							if(message instanceof TextMessage){
								TextMessage textMessage = (TextMessage)message;
								try {
									String msg = textMessage.getText();
									System.out.println(msg);
								} catch (JMSException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						}
					});
					System.out.println("我是一号监听员03");
					//键盘输入就关闭资源
					System.in.read();
					//发送成功后关闭资源
					messageConsumer.close();
					session.close();
					connection.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
	}
}

三、spring整合MQ

生产者配置文件:applicationContext-activemq.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">
	<!-- JMS服务厂商提供的connectfactory -->
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<constructor-arg name="brokerURL" value="tcp://192.168.25.128:61616"></constructor-arg>
	</bean>	
	<!-- spring对connectfactory封装 -->
	<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
		<property name="targetConnectionFactory" ref="targetConnectionFactory"></property>
	</bean>
	<!-- 配置JMSTemplate -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="connectionFactory"/>
	</bean>
	<!-- 配置消息的destination -->
	<!-- queue队列 -->
	<bean id="test-queue" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg name="name" value="test-queue"></constructor-arg>
	</bean>
	<!-- topic订阅 -->
	<bean id="test-topic" class="org.apache.activemq.command.ActiveMQTopic">
		<constructor-arg name="name" value="test-topic"></constructor-arg>
	</bean>
</beans>

测试方法:

package com.taotao.fastdfs;

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

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class SpringMQDemo {
	@Test
	public void MQprovider(){
		//初始化Spring容器
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
		//容器中获取JMStemplate对象
		JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);
		//从容器中获取destination
		Destination destination = (Destination) applicationContext.getBean("test-queue");
		//发送消息
		jmsTemplate.send(destination, new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				// TODO Auto-generated method stub
				TextMessage text = session.createTextMessage("这是三体世界发送的第2个消息!不要回答!不要回答!不要回答!");
				return text;
			}
		});
	}
}

消费者配置文件:applicationContext-activemq.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">
	<!-- JMS服务厂商提供的connectfactory -->
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<constructor-arg name="brokerURL" value="tcp://192.168.25.128:61616"></constructor-arg>
	</bean>	
	<!-- spring对connectfactory封装 -->
	<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
		<property name="targetConnectionFactory" ref="targetConnectionFactory"></property>
	</bean>
	<!-- 配置JMSTemplate -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="connectionFactory"/>
	</bean>
	<!-- 配置消息的destination -->
	<!-- queue队列 -->
	<bean id="test-queue" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg name="name" value="test-queue"></constructor-arg>
	</bean>
	<!-- topic订阅 -->
	<bean id="test-topic" class="org.apache.activemq.command.ActiveMQTopic">
		<constructor-arg name="name" value="test-topic"></constructor-arg>
	</bean>
	<!-- 配置消息消费者 -->
	<bean id="myMessageListener" class="com.taotao.portal.MyMessageListener"></bean>
	<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory"></property>
		<property name="destination" ref="test-queue"></property>
		<property name="messageListener" ref="myMessageListener"></property>
	</bean>
</beans>

自定义消息监听类:

package com.taotao.portal;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class MyMessageListener implements MessageListener{

	@Override
	public void onMessage(Message message) {
		// TODO Auto-generated method stub
		TextMessage textMessage = (TextMessage) message;
		try {
			String text = textMessage.getText();
			System.out.println(text);
                    //此处以后增加业务代码
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

消费者测试方法:


import java.io.IOException;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class ConsumertMq {
	@Test
	public void test() {
			ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
			//等待
			try {
				System.in.read();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
}

测试了连续运行4次生产者,消费者效果如图:

猜你喜欢

转载自blog.csdn.net/tonglei111/article/details/107837869