模型:
1、 生产者
package com.june.rabbitmq.routing;
import com.june.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Send {
private static final String EXCHANGE_NAME = "june_exchange_direct";
public static void main(String[] args) {
try {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
//声明交换机
channel.exchangeDeclare(EXCHANGE_NAME, "direct");//"fanout"分发
String routingKey = "info";
String msg = "routingKey = " + routingKey + ", juneli direct!";
channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes());
System.out.println("routingKey = " + routingKey + ", send message : " + msg );
channel.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
2、 消费者1
package com.june.rabbitmq.routing;
import com.june.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Recevice1 {
private static final String QUEUE_NAME = "june_work_queue1";
private static final String EXCHANGE_NAME = "june_exchange_direct";
public static void main(String[] args) {
try {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String routingKey = "error";
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME , routingKey);
//设置同时只消费一条
channel.basicQos(1);
DefaultConsumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, "utf-8");
System.out.println("[1] recevice message : " + message);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
System.out.println("[1] done!");
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
//自动应答改为false
boolean autoAck = false;
//监听队列 注册一个监听事件
channel.basicConsume(QUEUE_NAME, autoAck, consumer);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
3、 消费者2
package com.june.rabbitmq.routing;
import com.june.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Recevice2 {
private static final String QUEUE_NAME = "june_work_queue2";
private static final String EXCHANGE_NAME = "june_exchange_direct";
public static void main(String[] args) {
try {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String routingKey1 = "info";
String routingKey2 = "error";
String routingKey3 = "log";
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME , routingKey1);
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME , routingKey2);
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME , routingKey3);
channel.basicQos(1);
DefaultConsumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, "utf-8");
System.out.println("[2] recevice message : " + message);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
System.out.println("[2] done!");
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
boolean autoAck = false;
//监听队列 注册一个监听事件
channel.basicConsume(QUEUE_NAME, autoAck, consumer);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}