ActiveMQ(二)——ActiviteMQ实现方式之一:JMS

新建一个java project:ActiveMQTest

从apache-activemq-5.8.0\lib文件夹下导入activemq相关的jar包(该文件夹下的jar基本都能满足需求,只需从该文件夹下导入即可)。

 

消息发送方代码:

package com.mycom.activemq;

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;

/**
 * ActiviteMQ方式1:JMS方式
 * 消息接收方(消费者)
 * 
 * @author guweiqiang
 */
public class JMSReceiver {

	/**
	 * 接收消息
	 */
	public static void receiveMessage(String brokerUrl) {
		// ConnectionFactory:连接工厂
		ConnectionFactory connectionFactory;

		// Connection:JMS客户端到JMS Provider的连接
		Connection connection = null;

		// Session:发送/接收消息的会话
		Session session = null;

		// Destination:消息目的地
		Destination destination;

		// MessageConsumer:消息消费者
		MessageConsumer messageConsumer;

		try {
			// 创建连接工厂ConnectionFactory实例
			connectionFactory = new ActiveMQConnectionFactory(
					ActiveMQConnection.DEFAULT_USER,
					ActiveMQConnection.DEFAULT_PASSWORD, brokerUrl);

			// 创建连接对象
			connection = connectionFactory.createConnection();

			// 启动连接
			connection.start();

			// 创建发送/接收消息的会话
			session = connection.createSession(Boolean.TRUE,
					Session.AUTO_ACKNOWLEDGE);

			// 创建消息目的地
			destination = session.createQueue("FirstQueue");

			// 创建消息消费方
			messageConsumer = session.createConsumer(destination);

			// 准备工作已经完成,可以开始接收消息了
			while (true) {
				TextMessage message = (TextMessage) messageConsumer
						.receive(100 * 1000);
				if (message != null) {
					System.out.println("接收到消息:" + message.getText());
				} else {
					break;
				}
			}

			// 提交会话
			session.commit();

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			// 释放资源
			if (session != null) {
				try {
					session.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 测试
	 */
	public static void main(String[] args) {
		String brokerUrl = "tcp://127.0.0.1:61616";
		JMSReceiver.receiveMessage(brokerUrl);
	}
}

 

消息接收方代码:

package com.mycom.activemq;

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

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

/**
 * ActiviteMQ方式1:JMS方式
 * 消息发送方(生产者)
 * 
 * @author guweiqiang
 */
public class JMSSender {

	/**
	 * 发送消息
	 */
	public static void sendMessage(String brokerUrl, String msg) {
		// ConnectionFactory:连接工厂
		ConnectionFactory connectionFactory;

		// Connection:JMS客户端到JMS Provider的连接
		Connection connection = null;

		// Session:发送/接收消息的会话
		Session session = null;

		// Destination:消息目的地
		Destination destination;

		// MessageProducer:消息生产者
		MessageProducer messageProducer;

		try {
			// 创建一个连接工厂ConnectionFactory实例
			connectionFactory = new ActiveMQConnectionFactory(
					ActiveMQConnection.DEFAULT_USER,
					ActiveMQConnection.DEFAULT_PASSWORD, brokerUrl);

			// 创建连接对象
			connection = connectionFactory.createConnection();

			// 启动连接
			connection.start();

			// 创建发送/接收消息的会话
			session = connection.createSession(Boolean.TRUE,
					Session.AUTO_ACKNOWLEDGE);

			// 创建消息目的地
			destination = session.createQueue("FirstQueue");

			// 创建消息发送方(生产者)
			messageProducer = session.createProducer(destination);
			messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // 设置是否持久化

			// 准备工作已经完成,可以开始发送消息了
			// 发送消息
			TextMessage message = session.createTextMessage(msg);
			messageProducer.send(message);
			System.out.println("发送消息:" + message.getText());

			// 提交会话
			session.commit();

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			// 释放资源
			if (session != null) {
				try {
					session.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}

			if (connection != null) {
				try {
					connection.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}

	}

	/**
	 * 测试方法
	 */
	public static void main(String[] args) {
		String brokerUrl = "tcp://127.0.0.1:61616";
		String msg = "test mq msg";
		JMSSender.sendMessage(brokerUrl, msg);
	}
}

 

启动ActiveMQ,再在本地执行上述发送方和接收方代码,运行结果如下:

发送方console:

发送消息:test mq msg

 

接收方console:

接收到消息:test mq msg

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值