在RabbitMQ中,可以设置队列的最大优先级和消息优先级。
channel.queueDelete("queue_normal3");
Map<String, Object> args1 = new HashMap<String, Object>();
args1.put("x-max-priority", 10);
channel.queueDeclare("queue_normal3", false, false, false, args1);
在上面这段代码中,用参数x-max-priority设置队列的最大优先级为10. 这存在两方面的意义:1、这个队列有优先级的概念。如果没有设置此参数,即使传入此队列的消息设置了优先级,也不起作用 2、设置了传入的消息可以设置的最大优先级。比如设置消息1的优先级为20,消息2的优先级为30.由于队列最大优先级是10,所以其实这两个消息的优先级相同,都是10.
下面举例:
例子1:先举一个正常的例子:
此处是发送端,我们向队列中放入三组消息,先放入3个低优先级消息,再放入3个高优先级消息,再放入3个低优先级消息。
public class Sender{
public static void main(String[] args) throws IOException, TimeoutException {
test1();
}
private static void test1() throws IOException {
Channel channel = ChannelUtils.getChannelInstance("testEmit");
channel.exchangeDelete("exchange_normal3");
channel.exchangeDeclare("exchange_normal3", "direct", false, false, null);
channel.queueDelete("queue_normal3");
Map<String, Object> args1 = new HashMap<String, Object>();
args1.put("x-max-priority", 10);
channel.queueDeclare("queue_normal3", false, false, false, args1);
channel.queueBind("queue_normal3", "exchange_normal3", "info");
for (int i = 0; i < 3; i++) {
AMQP.BasicProperties.Builder builder = new
AMQP.BasicProperties.Builder();
builder.priority(1);
AMQP.BasicProperties properties = builder.build();
channel.basicPublish("exchange_normal3", "info", properties, "低优先级1".getBytes());
}
for (int i = 0; i < 3; i++) {
AMQP.BasicProperties.Builder builder = new
AMQP.BasicProperties.Builder();
builder.priority(5);
AMQP.BasicProperties properties = builder.build();
channel.basicPublish("exchange_normal3", "info", properties, "高优先级".getBytes());
}
for (int i = 0; i < 3; i++) {
AMQP.BasicProperties.Builder builder = new
AMQP.BasicProperties.Builder();
builder.priority(1);
AMQP.BasicProperties properties = builder.build();
channel.basicPublish("exchange_normal3", "info", properties, "低优先级2".getBytes());
}
}
}
运行生产端,如上图,队列中放入了9个消息。
下面是消费端
public class ReceiveLogs {
public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException,
InterruptedException, TimeoutException {
test2();
}
private static void test2() throws IOException {
Channel channel = ChannelUtils.getChannelInstance("test2");
DefaultConsumer c = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
System.out.println(new String(body));
}
};
channel.basicConsume("queue_normal3", true,c);
}
}
输入如下:
高优先级
高优先级
高优先级
低优先级1
低优先级1
低优先级1
低优先级2
低优先级2
低优先级2
例子2:
我们将上面生产端发送的三组优先级从1,5,1换成10,50,10
再次运行代码,输出为:
低优先级1
低优先级1
低优先级1
高优先级
高优先级
高优先级
低优先级2
低优先级2
低优先级2
从输出可以看出,队列最大优先级为10,而消息的优先级>=10,都被当成优先级10处理。
附1:ChannelUtils 的源码
package utils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class ChannelUtils {
// AMQP的连接其实是对Socket做的封装, 注意以下AMQP协议的版本号,不同版本的协议用法可能不同。
public static Channel getChannelInstance(String ConnectionDescription) {
try {
ConnectionFactory connectionFactory = getConnectionFactory();
Connection connection = connectionFactory.newConnection(ConnectionDescription);
return connection.createChannel();
} catch (Exception e) {
throw new RuntimeException("获取Channel连接失败");
}
}
public static ConnectionFactory getConnectionFactory() {
ConnectionFactory connectionFactory = new ConnectionFactory();
//下面这段参数,需要依据实际情况修改
connectionFactory.setHost(“192.168.1.111”);
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost(“/”);
connectionFactory.setUsername(“drs”);
connectionFactory.setPassword(“123456”);
connectionFactory.setClientProperties(connectionFactoryPropertiesMap);
return connectionFactory;
}
}
附2:pom文件的依赖配置
com.rabbitmq
amqp-client
4.6.0