47 lines
1.5 KiB
Java
Executable File
47 lines
1.5 KiB
Java
Executable File
package me.ehlxr.rabbitmq.routing;
|
|
|
|
import com.rabbitmq.client.Channel;
|
|
import com.rabbitmq.client.Connection;
|
|
import com.rabbitmq.client.QueueingConsumer;
|
|
import me.ehlxr.rabbitmq.ConnectionUtil;
|
|
|
|
import java.io.IOException;
|
|
import java.util.concurrent.TimeoutException;
|
|
|
|
/**
|
|
* 发送消息到交换机并且要指定路由 key ,消费者将队列绑定到交换机时需要指定路由 key
|
|
*
|
|
* @author lixiangrong
|
|
* @since 2019-01-22.
|
|
*/
|
|
public class Receiver1 {
|
|
private final static String QUEUE_NAME = "queue_routing";
|
|
private final static String EXCHANGE_NAME = "exchange_direct";
|
|
|
|
public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
|
|
// 获取到连接以及mq通道
|
|
Connection connection = ConnectionUtil.getConnection();
|
|
Channel channel = connection.createChannel();
|
|
|
|
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
|
|
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "key");
|
|
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "key2");
|
|
|
|
channel.basicQos(1);
|
|
|
|
QueueingConsumer consumer = new QueueingConsumer(channel);
|
|
channel.basicConsume(QUEUE_NAME, false, consumer);
|
|
|
|
while (true) {
|
|
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
|
|
String message = new String(delivery.getBody());
|
|
System.out.println("[x] Received1 " + message);
|
|
Thread.sleep(10);
|
|
|
|
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
|
|
}
|
|
|
|
|
|
}
|
|
}
|