ActiveMQ(5.5.1)的简单示例

1. 在http://activemq.apache.org/下载ActiveMQ


2. 解压压缩包,执行bin目录下的ActiveMQ启动脚本。启动ActiveMQ后,可登录到http://localhost:8161/admin/进行一些管理操作。


3. 创建一个新的工程,导入ActiveMQ相关的jar包。


4. 消息发送:

package com.huey.dream.producer;

import java.util.Date;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 
 * @author  huey
 * @version 1.0 
 * @created 2014-12-9
 */
public class MsgSender {

	public static void main(String[] args) {
		
		// 创建连接工厂实例
		ConnectionFactory connFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                "tcp://localhost:61616");
		
		Connection conn = null;
		try {
			// 取得连接对象实例
			conn = connFactory.createConnection();
			// 启动连接
			conn.start();
			// 创建会话对象实例
			Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
			// 创建消息目的地
			Destination destination = session.createQueue("helloQueue");
			// 创建消息生产者
			MessageProducer msgProducer = session.createProducer(destination);
			// 设置非持久化
			msgProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
			
			for (int i = 1; i <= 5; i++) {
				Date time = new Date();
				
				// 创建消息对象实例message = session.createMapMessage().
				Message textMsg = session.createTextMessage("Hello,这是第" + i + "个消息。");
				// 发送消息
				msgProducer.send(textMsg);
				// 会话提交
				session.commit();
				System.out.println("第" + i + "个消息的发送时间: " + time);
				Thread.sleep(3 * 1000);
			}
		} catch (JMSException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {		
			if (conn != null) {
				try {
					conn.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
}

5. 消息接收:

package com.huey.dream.receiver;

import java.util.Date;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 
 * @author  huey
 * @version 1.0 
 * @created 2014-12-9
 */
public class MsgReceiver {

	public static void main(String[] args) {
		
		// 创建连接工厂实例
		ConnectionFactory connFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                "tcp://localhost:61616");
		
		Connection conn = null;
		try {
			// 取得连接对象实例
			conn = connFactory.createConnection();
			// 启动连接
			conn.start();						
			// 创建会话对象实例
			Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			// 创建消息目的地
			Destination destination = session.createQueue("helloQueue");
			// 创建消息消费者
			MessageConsumer msgConsumer = session.createConsumer(destination);
			
			System.out.println("等待消息...");
			while(true) {
				// 取消息
				TextMessage textMsg = (TextMessage)msgConsumer.receive(40 * 1000);
				if (textMsg != null) {
					System.out.println(textMsg.getText() + " 接收时间:" + new Date());
				} else {
					System.out.println("没有消息了。");
					break;
				}
			}
		} catch (JMSException e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值