activemq中queue的简单负载示例

  • 生产者代码
package cn.wonhigh.dc.amq.consumer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

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

public class QueueProducerTest {

	public static final String QUEUE_NAME = "cn.wonhigh.dc.test1";

	public static void main(String[] args) throws Exception {
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
				ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD,
				"tcp://172.17.210.156:3046");
		Connection connection = connectionFactory.createConnection();
		connection.start();
		Session session = connection.createSession(false,
				Session.AUTO_ACKNOWLEDGE);
		Queue queue = session.createQueue(QUEUE_NAME);
		MessageProducer producer = session.createProducer(queue);
		for (int i = 0; i < 20; i++) {
			TextMessage textMessage = session.createTextMessage("message-->"
					+ i);
			producer.send(textMessage);
		}
	}
}


  • 消费者代码(同一个session)
package cn.wonhigh.dc.amq.consumer;

import java.util.Date;

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

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

/**
 * 消费者队列
 * 
 * @author wang.w
 * 
 */
public class QueueConsumerTest {

	public static final String QUEUE_NAME = "cn.wonhigh.dc.test1";
	
	public static void main(String[] args) {
		ConnectionFactory connectionFactory = null;
		Connection connection = null;
		Session session = null;
		Destination destination = null;
		MessageConsumer consumer = null;
		MessageConsumer consumer2 = null;
		connectionFactory = new ActiveMQConnectionFactory(
				ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD, "tcp://172.17.210.156:3046");
		try {
			connection = connectionFactory.createConnection();
			connection.start();
			session = connection.createSession(Boolean.FALSE,
					Session.AUTO_ACKNOWLEDGE);
			destination = session.createQueue(QUEUE_NAME);
			consumer = session.createConsumer(destination);
			//final Date date = new Date();
			MessageListener messageListener1 = new MessageListener() {

				@Override
				public void onMessage(Message message) {
					try {
						System.out.println("111 => receive from cn.wonhigh.dc.test1: "
								+ message);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

			};
			consumer.setMessageListener(messageListener1);
			
			consumer2 = session.createConsumer(destination);
			MessageListener messageListener2 = new MessageListener() {

				@Override
				public void onMessage(Message message) {
					try {
						System.out.println("222 => receive from cn.wonhigh.dc.test1: "
								+ message);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

			};
			consumer2.setMessageListener(messageListener2);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
  • 消费者端代码(不同的session)
package cn.wonhigh.dc.amq.consumer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

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

public class QueueProducerTest {

	public static final String QUEUE_NAME = "cn.wonhigh.dc.test1";

	public static void main(String[] args) throws Exception {
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
				ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD,
				"tcp://172.17.210.156:3046");
		Connection connection = connectionFactory.createConnection();
		connection.start();
		Session session = connection.createSession(false,
				Session.AUTO_ACKNOWLEDGE);
		Queue queue = session.createQueue(QUEUE_NAME);
		MessageProducer producer = session.createProducer(queue);
		for (int i = 0; i < 20; i++) {
			TextMessage textMessage = session.createTextMessage("message-->"
					+ i);
			producer.send(textMessage);
		}
	}
}


  • 结果输出(同一session的):
111 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 5, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652534, arrival = 0, brokerInTime = 1416224490774, brokerOutTime = 1416224490776, correlationId = null, replyTo = null, persistent = true, 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 = message-->0}
222 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 6, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:2, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652552, arrival = 0, brokerInTime = 1416224490792, brokerOutTime = 1416224490794, correlationId = null, replyTo = null, persistent = true, 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 = message-->1}
111 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 7, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:3, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652560, arrival = 0, brokerInTime = 1416224490800, brokerOutTime = 1416224490801, correlationId = null, replyTo = null, persistent = true, 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 = message-->2}
222 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 8, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:4, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652598, arrival = 0, brokerInTime = 1416224490837, brokerOutTime = 1416224490838, correlationId = null, replyTo = null, persistent = true, 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 = message-->3}
111 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 9, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:5, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652604, arrival = 0, brokerInTime = 1416224490844, brokerOutTime = 1416224490845, correlationId = null, replyTo = null, persistent = true, 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 = message-->4}
222 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 10, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:6, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652612, arrival = 0, brokerInTime = 1416224490852, brokerOutTime = 1416224490853, correlationId = null, replyTo = null, persistent = true, 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 = message-->5}
111 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 11, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:7, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652620, arrival = 0, brokerInTime = 1416224490860, brokerOutTime = 1416224490860, correlationId = null, replyTo = null, persistent = true, 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 = message-->6}
222 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 12, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:8, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652628, arrival = 0, brokerInTime = 1416224490867, brokerOutTime = 1416224490868, correlationId = null, replyTo = null, persistent = true, 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 = message-->7}
111 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 13, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:9, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652637, arrival = 0, brokerInTime = 1416224490876, brokerOutTime = 1416224490877, correlationId = null, replyTo = null, persistent = true, 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 = message-->8}
222 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 14, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:10, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652645, arrival = 0, brokerInTime = 1416224490884, brokerOutTime = 1416224490885, correlationId = null, replyTo = null, persistent = true, 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 = message-->9}
111 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 15, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:11, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652653, arrival = 0, brokerInTime = 1416224490892, brokerOutTime = 1416224490893, correlationId = null, replyTo = null, persistent = true, 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 = message-->10}
222 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 16, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:12, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652661, arrival = 0, brokerInTime = 1416224490900, brokerOutTime = 1416224490901, correlationId = null, replyTo = null, persistent = true, 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 = message-->11}
111 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 17, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:13, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652669, arrival = 0, brokerInTime = 1416224490908, brokerOutTime = 1416224490909, correlationId = null, replyTo = null, persistent = true, 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 = message-->12}
222 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 18, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:14, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652676, arrival = 0, brokerInTime = 1416224490916, brokerOutTime = 1416224490917, correlationId = null, replyTo = null, persistent = true, 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 = message-->13}
111 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 19, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:15, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652716, arrival = 0, brokerInTime = 1416224490955, brokerOutTime = 1416224490956, correlationId = null, replyTo = null, persistent = true, 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 = message-->14}
222 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 20, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:16, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652724, arrival = 0, brokerInTime = 1416224490964, brokerOutTime = 1416224490965, correlationId = null, replyTo = null, persistent = true, 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 = message-->15}
111 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 21, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:17, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652732, arrival = 0, brokerInTime = 1416224490972, brokerOutTime = 1416224490973, correlationId = null, replyTo = null, persistent = true, 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 = message-->16}
222 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 22, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:18, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652740, arrival = 0, brokerInTime = 1416224490980, brokerOutTime = 1416224490981, correlationId = null, replyTo = null, persistent = true, 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 = message-->17}
111 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 23, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:19, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652748, arrival = 0, brokerInTime = 1416224490988, brokerOutTime = 1416224490991, correlationId = null, replyTo = null, persistent = true, 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 = message-->18}
222 => receive from cn.wonhigh.dc.test1: ActiveMQTextMessage {commandId = 24, responseRequired = true, messageId = ID:szsknb093-58193-1416224652306-1:1:1:1:20, originalDestination = null, originalTransactionId = null, producerId = ID:szsknb093-58193-1416224652306-1:1:1:1, destination = queue://cn.wonhigh.dc.test1, transactionId = null, expiration = 0, timestamp = 1416224652756, arrival = 0, brokerInTime = 1416224490995, brokerOutTime = 1416224490996, correlationId = null, replyTo = null, persistent = true, 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 = message-->19}

结论:多个消费者监听同一个队列,能随机的被其中某个读取且不重复读

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值