环境:OS X EI Capitan 10.11.3 + ActiveMQ 5.13.0
Exclusive Consumers 是一个Queue的排他消费者,就是同一时刻只能有一个消费者消费Queue中的消息。
当这个消费者退出后,其它消费者可以争抢这个唯一的消费名额。
1. Queue_Exclusive_Consumer_AutoAck_Sync.java
package com.travelsky.activemq;
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.ActiveMQConnectionFactory;
public class Queue_Exclusive_Consumer_AutoAck_Sync {
public static void main(String[] args) {
// ConnectionFactory:连接工厂,JMS用它创建连接
ConnectionFactory connectionFactory;
// Connection:JMS客户端到JMS Provider的连接
Connection connection = null;
// Session:一个发送或接收消息的线程
Session session;
// Destination:消息的目的地;消息发送给谁.
Destination destination;
// MessageConsumer:消息消费者
MessageConsumer consumer;
connectionFactory = new ActiveMQConnectionFactory(
"admin",
"admin",
"failover:(tcp://127.0.0.1:61616)");
try {
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("FirstQueue?consumer.exclusive=true&consumer.priority=10");
consumer = session.createConsumer(destination);
while (true) {
TextMessage message = (TextMessage) consumer.receive(100000);
if (null != message) {
System.out.println("收到消息" + message.getText());
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection) {
connection.close();
}
} catch (Throwable ignore) {
}
}
}
}
consumer.priority的数字越高,表明优先级越大。
参考文献:
1. http://marcelojabali.blogspot.com/2011/11/preserving-message-order-with-apache.html
Exclusive Consumers 是一个Queue的排他消费者,就是同一时刻只能有一个消费者消费Queue中的消息。
当这个消费者退出后,其它消费者可以争抢这个唯一的消费名额。
1. Queue_Exclusive_Consumer_AutoAck_Sync.java
package com.travelsky.activemq;
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.ActiveMQConnectionFactory;
public class Queue_Exclusive_Consumer_AutoAck_Sync {
public static void main(String[] args) {
// ConnectionFactory:连接工厂,JMS用它创建连接
ConnectionFactory connectionFactory;
// Connection:JMS客户端到JMS Provider的连接
Connection connection = null;
// Session:一个发送或接收消息的线程
Session session;
// Destination:消息的目的地;消息发送给谁.
Destination destination;
// MessageConsumer:消息消费者
MessageConsumer consumer;
connectionFactory = new ActiveMQConnectionFactory(
"admin",
"admin",
"failover:(tcp://127.0.0.1:61616)");
try {
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("FirstQueue?consumer.exclusive=true&consumer.priority=10");
consumer = session.createConsumer(destination);
while (true) {
TextMessage message = (TextMessage) consumer.receive(100000);
if (null != message) {
System.out.println("收到消息" + message.getText());
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection) {
connection.close();
}
} catch (Throwable ignore) {
}
}
}
}
consumer.priority的数字越高,表明优先级越大。
参考文献:
1. http://marcelojabali.blogspot.com/2011/11/preserving-message-order-with-apache.html
没有评论:
发表评论