一个生产者,一个默认的交换机,一个队列,两个消费者
public class WorkTest {
@Test
public void pubTest() throws IOException {//发送消息
Connection connection = MQFactory.getConnection();//建立连接
Channel channel = connection.createChannel();//创建信道对象
for (int i = 0; i < 10; i++) {
channel.basicPublish("","work_queue",null,("hello work:"+i).getBytes());//发送消息
}
}
@Test
public void conTest1() throws IOException {//接收消息
Connection connection = MQFactory.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("work_queue",true,false,false,null);//创建队列
DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {//处理消息的一个回调
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String mag = new String(body, "utf-8");
System.out.println("消费者1:"+mag);
}
};
channel.basicConsume("work_queue",true,defaultConsumer);//消费者的消费方法
System.in.read();
}
@Test
public void conTest2() throws IOException {//接收消息
Connection connection = MQFactory.getConnection();
Channel channel = connection.createChannel();
DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {//处理消息的一个回调
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String mag = new String(body, "utf-8");
System.out.println("消费者2:"+mag);
}
};
channel.basicConsume("work_queue",true,defaultConsumer);//消费者的消费方法
System.in.read();
}
}
这时 两台消费者拿到的是均分的
只需要在消费者端,添加Qos能力以及更改为手动ack即可让消费者,根据自己的能力去消费指定的消息,而不是默认情况下由RabbitMQ平均分配了,生产者不变,正常发布消息到默认的exchange,并指定routing消费者指定Qoa和手动ack
public class WorkTest {
@Test
public void pubTest() throws IOException {//发送消息
Connection connection = MQFactory.getConnection();//建立连接
Channel channel = connection.createChannel();//创建信道对象
for (int i = 0; i < 10; i++) {
channel.basicPublish("","work_queue",null,("hello work:"+i).getBytes());//发送消息
}
}
@Test
public void conTest1() throws IOException {//接收消息
Connection connection = MQFactory.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("work_queue",true,false,false,null);//创建队列
channel.basicQos(1);
DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {//处理消息的一个回调
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String mag = new String(body, "utf-8");
System.out.println("消费者1:"+mag);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
channel.basicAck(envelope.getDeliveryTag(),false);
}
};
channel.basicConsume("work_queue",false,defaultConsumer);//消费者的消费方法
System.in.read();
}
@Test
public void conTest2() throws IOException {//接收消息
Connection connection = MQFactory.getConnection();
Channel channel = connection.createChannel();
// 指定当前消费者,一次消费多少个消息
channel.basicQos(1);
DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {//处理消息的一个回调
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String mag = new String(body, "utf-8");
System.out.println("消费者2:"+mag);
//手动ack
//envelope.getDeliveryTag 消息的标识 false 是否批量操作
channel.basicAck(envelope.getDeliveryTag(),false);
}
};
channel.basicConsume("work_queue",false,defaultConsumer);//消费者的消费方法
System.in.read();
}
}
手动ack 跟设置当前消费者一次消费多少消息 就可以看自己性能接收消息