ActiveMQ

官网下载地址

http://activemq.apache.org/activemq-580-release.html

ActiveMQ服务

ActiveMQ源码

接收方

package com.ninemax.ak.mq;

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

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

public class Receiver {

	public static void main(String[] args) {
		// ConnectionFactory :连接工厂,JMS 用它创建连接
		ConnectionFactory connectionFactory;
		// Connection :JMS 客户端到JMS Provider 的连接
		Connection connection = null;
		// Session: 一个发送或接收消息的线程
		Session session ;
		// Destination :消息的目的地;消息发送给谁.
		Destination destination;
		// 消费者,消息接收者
		MessageConsumer consumer;
		// 初始化工厂
		connectionFactory = new ActiveMQConnectionFactory(
				ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
		try {
			// 从工厂获取连接对象
			connection = connectionFactory.createConnection();
			// 启动
			connection.start();
			// 获取操作连接
			session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
			// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在ActiveMq的console配置  
			// 获取消息目的地:队列
			destination = session.createQueue("FirstQueue");
			// 接收消息
			consumer = session.createConsumer(destination);
			while(true){
				// 设置接收者接收消息的时间,为了便于测试,这里设定为100s
				TextMessage message = (TextMessage)consumer.receive(100000);
				System.out.println("message:" + message);
				if(null != message){
					System.out.println("接收到消息:" + message.getText());
				}else{
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != connection) {
					connection.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

}

发送方

package com.ninemax.ak.mq;

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

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

public class Sender {

	private static final int SEND_NUMBER = 5;

	public static void main(String[] args) {
		// ConnectionFactory :连接工厂,JMS 用它创建连接
		ConnectionFactory connectionFactory;
		// Connection :JMS 客户端到JMS
		// Provider 的连接
		Connection connection = null;
		// Session: 一个发送或接收消息的线程
		Session session;
		// Destination :消息的目的地;消息发送给谁.
		Destination destination;
		// MessageProducer:消息发送者
		MessageProducer producer;
		// 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
		connectionFactory = new ActiveMQConnectionFactory(
				ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
		try {
			// 从工厂获取连接对象
			connection = connectionFactory.createConnection();
			// 启动
			connection.start();
			// 获取操作连接
			session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
			// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
			// 获取消息目的地
			destination = session.createQueue("FirstQueue");
			// 获取发送者
			producer = session.createProducer(destination);
			// 设置不持久化,此处学习,实际根据项目决定
			producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
			// 构造消息,此处写死,项目就是参数,或者方法获取
			sendMessage(session, producer);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != connection)
					connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static void sendMessage(Session session, MessageProducer producer)
			throws Exception {

		for (int i = 1; i <= SEND_NUMBER; i++) {
			TextMessage message = session.createTextMessage("ActiveMq 发送消息" + i);
			// 发送消息到目的地方
			System.out.println("发送消息:" + "ActiveMq 发送消息" + i);
			producer.send(message);
		}
	}
}

运行结果

发送消息:ActiveMq 发送消息1
发送消息:ActiveMq 发送消息2
发送消息:ActiveMq 发送消息3
发送消息:ActiveMq 发送消息4
发送消息:ActiveMq 发送消息5
message:ActiveMQTextMessage {commandId = 6, responseRequired = false, messageId = ID:darker-PC-56612-1494316069422-1:1:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:darker-PC-56612-1494316069422-1:1:1:1, destination = queue://FirstQueue, transactionId = TX:ID:darker-PC-56612-1494316069422-1:1:1, expiration = 0, timestamp = 1494316069629, arrival = 0, brokerInTime = 1494316069630, brokerOutTime = 1494316069674, correlationId = null, replyTo = null, persistent = false, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, text = ActiveMq 发送消息1}
接收到消息:ActiveMq 发送消息1
message:ActiveMQTextMessage {commandId = 7, responseRequired = false, messageId = ID:darker-PC-56612-1494316069422-1:1:1:1:2, originalDestination = null, originalTransactionId = null, producerId = ID:darker-PC-56612-1494316069422-1:1:1:1, destination = queue://FirstQueue, transactionId = TX:ID:darker-PC-56612-1494316069422-1:1:1, expiration = 0, timestamp = 1494316069630, arrival = 0, brokerInTime = 1494316069630, brokerOutTime = 1494316069674, correlationId = null, replyTo = null, persistent = false, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, text = ActiveMq 发送消息2}
接收到消息:ActiveMq 发送消息2
message:ActiveMQTextMessage {commandId = 8, responseRequired = false, messageId = ID:darker-PC-56612-1494316069422-1:1:1:1:3, originalDestination = null, originalTransactionId = null, producerId = ID:darker-PC-56612-1494316069422-1:1:1:1, destination = queue://FirstQueue, transactionId = TX:ID:darker-PC-56612-1494316069422-1:1:1, expiration = 0, timestamp = 1494316069630, arrival = 0, brokerInTime = 1494316069630, brokerOutTime = 1494316069674, correlationId = null, replyTo = null, persistent = false, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, text = ActiveMq 发送消息3}
接收到消息:ActiveMq 发送消息3
message:ActiveMQTextMessage {commandId = 9, responseRequired = false, messageId = ID:darker-PC-56612-1494316069422-1:1:1:1:4, originalDestination = null, originalTransactionId = null, producerId = ID:darker-PC-56612-1494316069422-1:1:1:1, destination = queue://FirstQueue, transactionId = TX:ID:darker-PC-56612-1494316069422-1:1:1, expiration = 0, timestamp = 1494316069630, arrival = 0, brokerInTime = 1494316069630, brokerOutTime = 1494316069675, correlationId = null, replyTo = null, persistent = false, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, text = ActiveMq 发送消息4}
接收到消息:ActiveMq 发送消息4
message:ActiveMQTextMessage {commandId = 10, responseRequired = false, messageId = ID:darker-PC-56612-1494316069422-1:1:1:1:5, originalDestination = null, originalTransactionId = null, producerId = ID:darker-PC-56612-1494316069422-1:1:1:1, destination = queue://FirstQueue, transactionId = TX:ID:darker-PC-56612-1494316069422-1:1:1, expiration = 0, timestamp = 1494316069630, arrival = 0, brokerInTime = 1494316069630, brokerOutTime = 1494316069675, correlationId = null, replyTo = null, persistent = false, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, text = ActiveMq 发送消息5}
接收到消息:ActiveMq 发送消息5

需要的JAR

企鹅:260052172

 

转载于:https://my.oschina.net/Tsher2015/blog/895828

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值