Apache-ActiveMQ试用手记

首先去apache.org下载activemq.

我下载的版本是5.2

然后解压缩 进入bin文件夹 双击启动activemq.bat

然后 打开http://localhost:8161/admin 可以看到mq的信息等等

 

然后开始试验吧

我们要建立两个类

一个是发送信息sendMessage

第二个是接受信息 reciveMessage

 

package com.apache.activemq;

import javax.jms.Connection;
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;

public class SendMessage {
	private static final String url = "tcp://localhost:61616";
	private static final String QUEUE_NAME = "kenter.queue";
	protected String expectedBody = "<hello>world!</hello>";

	/**
	 * 连接并且发送信息
	 * 
	 * @throws JMSException
	 */
	public void sendMessage() throws JMSException {
		Connection connection = null;
		try {
			ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
					url);
			connection = (Connection) connectionFactory.createConnection();
			connection.start();
			Session session = (Session) connection.createSession(false,
					Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createQueue(QUEUE_NAME);
			MessageProducer producer = session.createProducer(destination);
			TextMessage message = session.createTextMessage(expectedBody);
			message.setStringProperty("headname", "remoteB");
			producer.send(message);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		SendMessage sndMsg = new SendMessage();
		int i = 0;
		while (++i < 10) {
			try {
				sndMsg.sendMessage();
			} catch (Exception ex) {
				System.out.println(ex.toString());
			}
		}
		System.exit(1);
	}
}

 

package com.apache.activemq;

import javax.jms.Connection;
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;

public class ReceiveMessage {
	private static final String url = "tcp://localhost:61616";
	private static final String QUEUE_NAME = "kenter.queue";

	/**
	 * 连接
	 */
	public void receiveMessage() {
		Connection connection = null;
		try {
			try {
				ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
						url);
				connection = connectionFactory.createConnection();
			} catch (Exception e) {
				System.out.println(e.toString());
			}
			connection.start();
			Session session = connection.createSession(false,
					Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createQueue(QUEUE_NAME);
			MessageConsumer consumer = session.createConsumer(destination);
			consumeMessagesAndClose(connection, session, consumer);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}

	/**
	 * 从消息队列获取所有队列信息并关闭
	 * @param connection
	 * @param session
	 * @param consumer
	 * @throws JMSException
	 */
	protected void consumeMessagesAndClose(Connection connection,
			Session session, MessageConsumer consumer) throws JMSException {
		Message message = consumer.receive(1000);
		while (message != null) {
			onMessage(message);
			message = consumer.receive(1000);
		}
		System.out.println("Closing connection");
		consumer.close();
		session.close();
		connection.close();
	}

	/**
	 * 打印信息
	 * @param message
	 */
	public void onMessage(Message message) {
		try {
			if (message instanceof TextMessage) {
				TextMessage txtMsg = (TextMessage) message;
				String msg = txtMsg.getText();
				System.out.println("Received: " + msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String args[]) {
		ReceiveMessage rm = new ReceiveMessage();
		rm.receiveMessage();
	}

}

 

运行后可以监控 整个信息发送 存储 接收的流程

先看这么多吧 详细的以后再看

你可能感兴趣的:(apache,activemq,jms)