发布于2020-11-19 20:27 阅读(1002) 评论(0) 点赞(30) 收藏(4)
package com.fang.java_client.work_queue_2;
import com.fang.java_client.utils.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
/**
* @Author Mr. Sun.
* @Date 2020-11-12 16:44
*
* 这种模型默认平均分配
* 解决办法就是把默认确认机制关掉
*/
public class Provider {
public static void main(String[] args) throws Exception {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
// 申明队列 这些true false 什么的点进去看,有英文注释!
channel.queueDeclare("worker", true, false, false, null);
// 生产消息
for (int i=0;i<100;i++) {
channel.basicPublish("", "worker", null, ("hello work queue"+i).getBytes());
}
// 关闭资源
RabbitMQUtils.closeConnectAndChannel(channel,connection);
}
}
// 两个消费者
package com.fang.java_client.work_queue_2;
import com.fang.java_client.utils.RabbitMQUtils;
import com.rabbitmq.client.*;
import lombok.SneakyThrows;
import java.io.IOException;
/**
* @Author Mr. Sun.
* @Date 2020-11-12 17:35
*/
public class Consumer_1 {
public static void main(String[] args) throws Exception {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("worker", true, false, false, null);
// 告诉通道一次只消费一个
// 解决能者多劳!
channel.basicQos(1);
// 默认确认机制 autoAck
channel.basicConsume("worker", false, new DefaultConsumer(channel) {
@SneakyThrows
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("consumer1" + new String(body));
Thread.sleep(500);
// 手动确认
//@param multiple 是否开启多个消息同时确认
channel.basicAck(envelope.getDeliveryTag(), false);
}
});
}
}
-------------------------------------------------------------------
package com.fang.java_client.work_queue_2;
import com.fang.java_client.utils.RabbitMQUtils;
import com.rabbitmq.client.*;
import lombok.SneakyThrows;
import java.io.IOException;
/**
* @Author Mr. Sun.
* @Date 2020-11-12 17:35
*/
public class Consumer_2 {
public static void main(String[] args) throws Exception {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
// 告诉通道一次只消费一个
channel.basicQos(1);
channel.queueDeclare("worker", true, false, false, null);
channel.basicConsume("worker", false, new DefaultConsumer(channel) {
@SneakyThrows
@Override
public void handleDelivery(java.lang.String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("consumer2" + new String(body));
Thread.sleep(1000);
// 手动确认
channel.basicAck(envelope.getDeliveryTag(), false);
}
});
}
}
@Test// worke queues 模型
void test_work_queue() {
for (int i = 0; i < 10; i++) {
rabbitTemplate.convertAndSend("work", "worker 模型"+i);
}
}
// worker 模型默认公平模型~
@RabbitListener(queuesToDeclare=@Queue(value = "work"))
private void receiveMsgWork1(String msg){
System.out.println("worker_1:"+msg);
}
@RabbitListener(queuesToDeclare=@Queue(value = "work"))
private void receiveMsgWork2(String msg){
System.out.println("worker_2:"+msg);
}
作者:天使之恋
链接:http://www.javaheidong.com/blog/article/986/68f8e03d0ada45a58de2/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!