1、mandatory
在之前的方法basicPublic方法中,存在mandatory参数,该参数的作用为:当交换器无法根据自身的类型和路由键找到一个符合条件的队列时,消息会被返回给生产者。
void basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body) throws IOException;
void basicPublish(String exchange, String routingKey, boolean mandatory, BasicProperties props, byte[] body) throws IOException;
void basicPublish(String exchange, String routingKey, boolean mandatory, boolean immediate, BasicProperties props, byte[] body) throws IOException;
定义一个生产者,两个消费者。消费者绑定的routingKey为error和info。生产者执行往errorinfo路由键发(该路由键不存在)。
import com.rabbitmq.client.*;
import ink.lmsy.mq_01.simplequeue.ConnectionUtils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* mandatory指当交换器无法根据自身的类型或者路由键找到一个符合条件的队列时,mandatory设置为true,消息将会返回给生产者
* 否则消息将会被丢弃。
*/
public class MandatoryProducer
{
private static final String EXCHANGE_NAME = "exchange";
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException
{
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
channel.basicPublish(EXCHANGE_NAME, "errorinfo", true, false, MessageProperties.PERSISTENT_TEXT_PLAIN, "===mandatory===".getBytes());
channel.addReturnListener(new ReturnListener()
{
@Override
public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties basicProperties, byte[] body) throws IOException
{
System.out.println("Basic.return返回的结果是:" + new String(body));
}
});
Thread.sleep(5000);
channel.close();
connection.close();
}
}
import com.rabbitmq.client.*;
import ink.lmsy.mq_01.simplequeue.ConnectionUtils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class MandatoryConsumer1
{
private static final String EXCHANGE_NAME = "exchange";
private static final String QUEUE_NAME = "mandatory_queue_1";
public static void main(String[] args) throws IOException, TimeoutException
{
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 队列绑定交换器
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error");
DefaultConsumer consumer = new DefaultConsumer(channel)
{
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException
{
System.out.println("C1 receive message is " + new String(body));
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
}
import com.rabbitmq.client.*;
import ink.lmsy.mq_01.simplequeue.ConnectionUtils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class MandatoryConsumer2
{
private static final String EXCHANGE_NAME = "exchange";
private static final String QUEUE_NAME = "mandatory_queue_2";
public static void main(String[] args) throws IOException, TimeoutException
{
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 队列绑定交换器
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info");
DefaultConsumer consumer = new DefaultConsumer(channel)
{
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException
{
System.out.println("C2 receive message is " + new String(body));
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
}
输出结果:
2、immediate
该参数为true时,表示交换器将消息路由到队列时发现队列上并不存在任何消费者,那么这条消息将不会存入队列中。将会被返回。