`
sunxboy
  • 浏览: 2828340 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

ActiveMQ JMS的测试

阅读更多

有二种方式可以测试。

1. 透过testng, 在测试类中完成的jms服务器的启动与关闭。

a.初始化jms服务器

@BeforeClass(groups = "jms")
	    public void setupActiveMQ() throws Exception {
	        BrokerService broker = new BrokerService();
	        broker.setPersistent(false);
	        broker.setUseJmx(false);
	        broker.start();
	        URI uri = broker.getVmConnectorURI();
	        ConnectionFactory factory = new ActiveMQConnectionFactory(uri);
	        //String uri = "vm://localhost?broker.persistent=false";
	        //ConnectionFactory factory = new ActiveMQConnectionFactory(uri);
	        Connection connection = factory.createConnection();
	        connection.start();
	        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	        destination = session.createQueue("TestQueue@router1");
	    }

 b. 运行测试类

@Test(groups = "jms")
	    public void sendMessage() throws JMSException {
	        TextMessage msg = session.createTextMessage();
	        msg.setText("hello");
	        session.createProducer(destination).send(msg);
	    }

c. 验证接收到的消息

 @Test(groups = "jms", dependsOnMethods = "sendMessage", timeOut = 1000)
	    public void receiveMessage() throws JMSException {
	        MessageConsumer consumer = session.createConsumer(destination);
	        TextMessage msg = (TextMessage) consumer.receive();
	        assert "hello".equals(msg.getText());
	    }

 

2. 透过maven-activemq-plugin插件,运行jms服务器,然后分别实现发送与接收

a. maven 配置

<plugin>
				<groupId>org.apache.activemq.tooling</groupId>
				<artifactId>maven-activemq-plugin</artifactId>
				<version>5.2.0</version>
				<configuration>
					<configUri>
						xbean:file:src/main/resources/net/sunbeta/jms/activemq.xml
					</configUri>
					<fork>false</fork>
					<systemProperties>
						<property>
							<name>javax.net.ssl.keyStorePassword</name>
							<value>password</value>
						</property>
						<property>
							<name>org.apache.activemq.default.directory.prefix</name>
							<value>./target/</value>
						</property>
					</systemProperties>
				</configuration>
				<dependencies>
					<dependency>
						<groupId>org.springframework</groupId>
						<artifactId>spring</artifactId>
						<version>2.5.6</version>
					</dependency>
					<dependency>
						<groupId>org.mortbay.jetty</groupId>
						<artifactId>jetty-xbean</artifactId>
						<version>6.1.11</version>
					</dependency>
					<dependency>
						<groupId>org.apache.camel</groupId>
						<artifactId>camel-activemq</artifactId>
						<version>1.1.0</version>
					</dependency>
				</dependencies>
			</plugin>

 其中 activemq.xml 的配置为:

<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:amq="http://activemq.apache.org/schema/core"
  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-2.0.xsd
  http://activemq.apache.org/schema/core
  http://activemq.apache.org/schema/core/activemq-core.xsd">
  
  <broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="./data">
    <!-- The transport connectors ActiveMQ will listen to -->
    <transportConnectors>
      <transportConnector name="openwire" uri="tcp://localhost:61616"/>
    </transportConnectors>
  </broker>
</beans>

 b.消息发送

public class JmsProducer {

	private ConnectionFactory factory;
	private Connection connection;
	private Session session;
	private MessageProducer producer;

	public JmsProducer(ConnectionFactory factory, String queueName)
			throws JMSException {
		this.factory = factory;
		connection = factory.createConnection();
		connection.start();
		session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Destination destination = session.createQueue(queueName);
		producer = session.createProducer(destination);
	}

	public void run() throws JMSException {
		for (int i = 0; i < 100; i++) {
			System.out.println("Creating Message " + i);
			Message message = session.createTextMessage("Hello World!");
			producer.send(message);
		}
	}

	public void close() throws JMSException {
		if (connection != null) {
			connection.close();
		}
	}
}

 创建一个broker去调用它

public class JmsBroker {

	public static String brokerURL = "tcp://localhost:61616";

	// mvn clean compile exec:java
	// -Dexec.mainClass=net.sunbeta.test.jms.JmsBroker
	public static void main(String[] args) throws Exception {
		// setup the connection to ActiveMQ
		ConnectionFactory factory = new ActiveMQConnectionFactory(brokerURL);

		JmsProducer producer = new JmsProducer(factory, "test");
		producer.run();
		producer.close();
	}
}

 c. 消息接收

public class JmsConsumer implements MessageListener{
	public static String brokerURL = "tcp://localhost:61616";

	private ConnectionFactory factory;
	private Connection connection;
	private Session session;
	private MessageConsumer consumer;

	public static void main(String[] args) {
		JmsConsumer app = new JmsConsumer();
		app.run();
	}

	public void run() {
		try {
			ConnectionFactory factory = new ActiveMQConnectionFactory(brokerURL);
			connection = factory.createConnection();
			connection.start();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createQueue("test");
			consumer = session.createConsumer(destination);
			consumer.setMessageListener(this);
		} catch (Exception e) {
			System.out.println("Caught:" + e);
			e.printStackTrace();
		}
	}

	public void onMessage(Message message) {
		try {
			if (message instanceof TextMessage) {
				TextMessage txtMessage = (TextMessage) message;
				System.out.println("Message received: " + txtMessage.getText());
			} else {
				System.out.println("Invalid message received.");
			}
		} catch (JMSException e) {
			System.out.println("Caught:" + e);
			e.printStackTrace();
		}
	}

 

OK !

分享到:
评论

相关推荐

    Jmeter测试ActiveMQ性能报告

    Jmeter压力测试 ActiveMQ性能 JMS性能测试

    sprin jms activemq 测试

    sprin jms activemq 测试 两个 一个是jndi 一个是activemq

    测试activeMQ的java程序

    activeMQ是jms的一种,是java是实现两个系统之间交互的方式,MQ分为队列模式和订阅模式,对这两种模式分别进行了测试通过。

    jms-test.zip_jms activemq_jms test

    jms测试程序,将tomcat和activeMq整合在一起做的一个发送接受的发布订阅的例子

    activemq +jms(原生和集成spring-jms)

    activemq集成spring,和原生的active测试active的p2p和pub/sub方式

    JMS模拟ActiveMQ代理服务器并实现消息收发

    java JMS模拟ActiveMQ代理服务器并实现消息收发,这里是工程源码,已经过测试,可以直接使用。

    SpringMvc+redis+activeMq实现消息发布订阅(测试通过)

    SpringMvc+redis+activeMq实现消息发布订阅(测试通过) redis和activeMq jms各自需要的Jar包在其它资源中上传,大家可以下载。 这个例子拿到项目中可直接用

    Spring 实现远程访问详解——jms和activemq

    本章我将通过spring jms和activemq实现单Web项目服务器间异步访问和多Web项目服务器间异步访问。 一. 简介 1. 什么是Apache ActiveMq Apache ActiveMq是最流行和最强大的开源消息和集成服务器。同时Apache ActiveMq...

    activemq-all-5.15.2.jar 和 jms-1.1.jar

    ActiveMQ测试用例依赖的jar包,activemq-all-5.15.2.jar 和 jms-1.1.jar

    apache-activemq-5.11.2

    ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位. ⒈ 多种语言和协议编写客户端。语言: Java,C,C++,C#,...

    jms基础实例(内有ActiveMQ中间件)

    ActiveMQ默认使用的TCP连接端口是61616, 通过查看该端口的信息可以测试ActiveMQ是否成功启动 netstat -an|find "61616" C:\Documents and Settings\Administrator&gt;netstat -an|find "61616" TCP 0.0.0.0:61616 0.0....

    activemq新手大全

    一、JMS基本概念 二、activemq介绍及安装 1、消息中间件简介 2、activemq 2.1、activemq简介 2.2、activemq下载 2.3、运行activemq服务 2.4、测试 2.5、监控 3、activemq特性 4、activemq使用场景 三、...

    activemq的windowns编译库、centos7编译库和mac编译库(含头文件和库文件)

    ActiveMQ 是Apache出品,最流行的,能力...可以用作内存JMS提供程序,非常适合单元测试JMS 支持可插拔传输协议,例如in-VM,TCP,SSL,NIO,UDP,多播,JGroups和JXTA传输 使用JDBC和高性能日志支持非常快速的持久性

    ActiveMQ入门及深入使用的例子

    JMS、ActiveMQ入门及深入使用的例子,这些例子都是我测试的

    spring+activeMQ集成

    spring集成activeMQ框架 配置方式(内含三种常见的消息接受监听方式的配置)JMS 配置测试等等

    ActiveMQ.rar

    包括:通过源码安装、基本的配置示例、启动、测试运行、关闭等 n 三:理解和掌握JMS 包括:基本概念、消息结构、可靠性机制、PTP、Pub/Sub、API结构、JMS应用开 发的基本步骤、持久和非持久的Topic等 n 四:用...

    JMS实时消息通讯源码-ActiveMQ Java服务端和Android客户端源码

    1、解压安装文件,执行\apache-activemq-5.13.3-bin\apache-activemq-5.13.3\bin\win64\wrapper.exe. 2、测试发送消息,打开本地服务器地址http://localhost:8161,登录服务器,默认用户名密码:admin,admin。登录...

    Centos 7 服务器Apache-ActiveMQ安装指南

    虽然ActiveMQ目前已经不是开发时的主要消息中间件, 但是对于简单使用JMS的场景而言, ActiveMQ仍然是一个比较成熟、稳定的框架,可以供初学者、小微企业快速上手。 虽然Apache ActiveMQ目前有多种安装方式, 使用...

    ActiveMQ Plugin-开源

    该项目是一个Eclipse插件,可帮助配置和管理ActiveMQ JMS Messaging Broker,以在受控环境中测试部署模型。 还提供编辑器来配置控制ActiveMQ的文档。

    消息中间件ActiveMQ及Spring整合JMS.docx

    ——学习参考资料:仅用于个人学习使用! 本代码仅作学习交流,切勿用于商业用途,否则后果自负。若涉及侵权,请联系,会尽快处理! 未进行详尽测试,请自行调试!

Global site tag (gtag.js) - Google Analytics