首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

ActiveMQ实战(二):测试其是否正常工作【转】

2012-12-18 
ActiveMQ实战(2):测试其是否正常工作【转】既然ActiveMQ安装好了并启动成功,接下来我们就编写一个测试程序,

ActiveMQ实战(2):测试其是否正常工作【转】

既然ActiveMQ安装好了并启动成功,接下来我们就编写一个测试程序,向它发送JMS消息,并从它上面接收JMS消息,看看是否能正常工作。

安装目录/usr/local/apache-activemq-5.3.0中,有一个jar包activemq-all-5.3.0.jar,记得把它下载下来,加入到测试程序的CLASSPATH中。

发送JMS消息的程序如下:

package jmstest;

import javax.jms.Connection;

import javax.jms.ConnectionFactory;

import javax.jms.Destination;

import javax.jms.JMSException;

import javax.jms.MessageProducer;

import javax.jms.Session;

import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

import org.apache.activemq.command.ActiveMQQueue;

/**

?*

?* @author Frank Wen

?*/

public class Send {

?? ?public static void main(String[] args) {

?? ? ? ?ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://192.168.1.109:61616");

?? ? ? ?Connection conn = null;

?? ? ? ?Session session = null;

?? ? ? ?try {

?? ? ? ? ? ?conn = cf.createConnection();

?? ? ? ? ? ?session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

?? ? ? ? ? ?Destination destination = new ActiveMQQueue("jms_test_queue");

?? ? ? ? ? ?MessageProducer producer = session.createProducer(destination);

?? ? ? ? ? ?TextMessage message = session.createTextMessage();

?? ? ? ? ? ?message.setText("Hello World!");

?? ? ? ? ? ?producer.send(message);

?? ? ? ? ? ?System.out.println("Send a message: Hello World!");

?? ? ? ?} catch (JMSException ex) {

?? ? ? ? ? ?ex.printStackTrace();

?? ? ? ?} finally {

?? ? ? ? ? ?try {

?? ? ? ? ? ? ? ?if(session != null) {

?? ? ? ? ? ? ? ? ? ?session.close();

?? ? ? ? ? ? ? ?}

?? ? ? ? ? ? ? ?if(conn != null) {

?? ? ? ? ? ? ? ? ? ?conn.close();

?? ? ? ? ? ? ? ?}

?? ? ? ? ? ?} catch (JMSException ex) {

?? ? ? ? ? ? ? ?ex.printStackTrace();

?? ? ? ? ? ?}

?? ? ? ?}

?? ?}

}

接收JMS消息的程序如下:

package jmstest;

import javax.jms.Connection;

import javax.jms.ConnectionFactory;

import javax.jms.Destination;

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.MessageConsumer;

import javax.jms.Session;

import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

import org.apache.activemq.command.ActiveMQQueue;

/**

?*

?* @author Frank Wen

?*/

public class Receive {

?? ?public static void main(String[] args) {

?? ? ? ?ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://192.168.1.109:61616");

?? ? ? ?Connection conn = null;

?? ? ? ?Session session = null;

?? ? ? ?try {

?? ? ? ? ? ?conn = cf.createConnection();

?? ? ? ? ? ?session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

?? ? ? ? ? ?Destination destination = new ActiveMQQueue("jms_test_queue");

?? ? ? ? ? ?MessageConsumer consumer = session.createConsumer(destination);

?? ? ? ? ? ?conn.start();

?? ? ? ? ? ?Message message = consumer.receive();

?? ? ? ? ? ?TextMessage textMessage = (TextMessage) message;

?? ? ? ? ? ?System.out.println("Receive a message: " + textMessage.getText());

?? ? ? ?} catch (JMSException ex) {

?? ? ? ? ? ?ex.printStackTrace();

?? ? ? ?} finally {

?? ? ? ? ? ?try {

?? ? ? ? ? ? ? ?if(session != null) {

?? ? ? ? ? ? ? ? ? ?session.close();

?? ? ? ? ? ? ? ?}

?? ? ? ? ? ? ? ?if(conn != null) {

?? ? ? ? ? ? ? ? ? ?conn.close();

?? ? ? ? ? ? ? ?}

?? ? ? ? ? ?} catch (JMSException ex) {

?? ? ? ? ? ? ? ?ex.printStackTrace();

?? ? ? ? ? ?}

?? ? ? ?}

?? ?}

}

先后执行以上两个程序,如果能正确发送和接收JMS消息,则表明ActiveMQ正常工作。

热点排行