下面我們將要實現這個模型,所有的代碼將都是以這個模型爲基礎:
direct:
首先,我們設置的routingKey是 error,那麼按照路由規則,我們最終將向這兩個隊列發送消息:
生產者:
public class Send {
private static final String EXCHANGE_NAME = "test_exchange_direct";
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = RabbitConnection.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
//send a msg
String msg = "hello direct";
String routingKey = "info"; //這裏是定義的routingKey
channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes());
System.out.println("send:"+msg);
channel.close();
connection.close();
}
}
消費者1:(他只能接受error)
public class ReceiveOne {
private static final String QUEUE_NAME = "receive1_queue";
private static final String EXCHANGE_NAME = "test_exchange_direct";
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = RabbitConnection.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
channel.basicQos(1);
String routingKey = "error"; //表示只能接收error
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKey);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [ConsumerOne is] Received '" + message + "'");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false); //這裏是手動應答
}
};
boolean autoAck = false;
channel.basicConsume(QUEUE_NAME, autoAck, deliverCallback, consumerTag -> { });
}
}
消費者2:(能接收error,info 和 warning)
public class ReceiveTwo {
private static final String QUEUE_NAME = "receive2_queue";
private static final String EXCHANGE_NAME = "test_exchange_direct";
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = RabbitConnection.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
channel.basicQos(1);
// 設置了三種key
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error"); //注意這裏,我們設置了三個路由key
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info");
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "warning");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [ConsumerTwo is] Received '" + message + "'");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false); //這裏是手動應答
}
};
boolean autoAck = false;
channel.basicConsume(QUEUE_NAME, autoAck, deliverCallback, consumerTag -> { });}
}
Topics:(模式匹配)
“#” 匹配一個或多個
“ * ” 匹配一個
用法和上面的差不多,具體可以看 官方例子 。
下面再補充一點例子,關於message屬性的例子:
Send.java
public class Send {
private static final String QUEUE_NAME = "simple_mq";
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = RabbitConnection.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "hello simple_mq";
Map<String,Object> headpro = new HashMap<>();
headpro.put("name","king");
AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
.deliveryMode(2)
.contentEncoding("utf-8")
.expiration("10000")
.headers(headpro)
.build();
try {
channel.txSelect();
channel.basicPublish("", QUEUE_NAME, properties, message.getBytes());
channel.txCommit();
} catch (IOException e) {
channel.txRollback();
System.out.println("回滾~~");
}
System.out.println(" [x] Sent.......... '" + message + "'");
channel.close();
connection.close();
}
}
這裏我們主要看一下這段代碼:
Map<String,Object> headpro = new HashMap<>();
headpro.put("name","king");
AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
.deliveryMode(2)
.contentEncoding("utf-8")
.expiration("10000")
.headers(headpro)
.build();
try {
channel.txSelect();
channel.basicPublish("", QUEUE_NAME, properties, message.getBytes());
我們以流式的方式給message添加各種屬性