RabbitMQ学习资源来源于(蚂蚁课堂)
(点对点简单队列(消费者是集群会进行均摊消费,你一个我一个。均摊消费存在弊端,比如对性能差的服务不友好);
工作(公平性、)队列:能者多劳)
工作队列-生产者:
package com.google.util.workQueue;
import com.google.util.MQConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author wk
* @Description:生产者
* @date 2019/12/5 15:28
**/
public class WorkProducer {
static String QUEUE_NAME = Config.WORK_QUEUE_01;
public static void main(String[] args) throws IOException, TimeoutException {
System.out.println("工作队列,生产者启动!");
//获取连接
Connection connection = MQConnectionUtils.newConnection();
//创建通道
Channel channel = connection.createChannel();
//创建队列声明
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 保证一次只分发一次 限制发送给同一个消费者 不得超过一条消息
channel.basicQos(1);
for (int i = 1; i <= 20; i++) {
String message = "RabbitMQ消息" + i;
System.out.println("生产者发送消息: " + message);
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
}
channel.close();
connection.close();
}
}
消费者:
package com.google.util.workQueue;
/**
* @author wk
* @Description:
* @date 2019/12/5 17:39
**/
public class Config {
public static final String WORK_QUEUE_01 = "WORK_QUEUE_01";
}
package com.google.util.workQueue;
import com.google.util.MQConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author wk
* @Description:消费者
* @date 2019/12/5 17:39
**/
public class Consumer1 {
static String queue = Config.WORK_QUEUE_01;
public static void main(String[] args) throws IOException, TimeoutException {
customerMember_01();
}
public static void customerMember_01() throws IOException, TimeoutException {
System.out.println("工作队列-消费者1启动。。。");
//创建通道
Channel channel = MQConnectionUtils.getChannel();
//消费者关联队列
channel.queueDeclare(queue, false, 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 msg = new String(body, "UTF-8");
System.out.println("消费者——1——获取信息:" + msg);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
//手动应答模式,
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
//设置应答模式 如果为true情况下,表示为自动应答模式 false表示为手动应答 监听队列
//channel.basicConsume(queue, true, defaultConsumer);
// [ˈbeɪsɪk]
//默认false
channel.basicConsume(queue, false, defaultConsumer);
}
}
package com.google.util.workQueue;
import com.google.util.MQConnectionUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author wk
* @Description:
* @date 2019/12/6 18:07
**/
public class Consumer2 {
static String queue = Config.WORK_QUEUE_01;
public static void main(String[] args) throws IOException, TimeoutException {
customerMember_02();
}
public static void customerMember_02() throws IOException, TimeoutException {
System.out.println("工作队列-消费者2启动。。。");
//创建通道
Channel channel = MQConnectionUtils.getChannel();
//消费者关联队列
channel.queueDeclare(queue, false, 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 msg = new String(body, "UTF-8");
System.out.println("消费者——2——获取信息:" + msg);
//手动应答模式,
channel.basicAck(envelope.getDeliveryTag(), false);
}
};
channel.basicConsume(queue, false, defaultConsumer);
}
}
测试运行:
先启动消费者,再启动生产者