RabbitMQ(二)JavaClient SpringBoot整合 Work queues
Java Client
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);
}
});
}
}
SpringBoot
@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);
}
相關文章
- RabbitMQ學習筆記-Work QueuesMQ筆記
- Java Spring boot 整合RabbitMQ(二):工作佇列(Work queues)-B2B2C小程式電子商務JavaSpring BootMQ佇列
- RabbitMQ -springboot整合rabbitmqMQSpring Boot
- SpringBoot 整合 rabbitmqSpring BootMQ
- Springboot整合RabbitMQSpring BootMQ
- Java SpringBoot 整合 RabbitMQJavaSpring BootMQ
- RabbitMQ入門到進階(Spring整合RabbitMQ&SpringBoot整合RabbitMQ)MQSpring Boot
- 每日一學:你知道如何在 RabbitMQ 中實現 Work queues工作佇列模式嗎?MQ佇列模式
- springboot2.0整合rabbitmqSpring BootMQ
- SpringBoot專案整合RabbitMQSpring BootMQ
- Rabbit學習---SpringBoot整合RabbitMQSpring BootMQ
- RabbitMQ-Work模式MQ模式
- SpringBoot整合RabbitMQ之典型應用場景實戰二Spring BootMQ
- (二)springboot中使用RabbitmqSpring BootMQ
- SpringBoot整合RabbitMQ(一)快速入門Spring BootMQ
- RabbitMQ簡介以及與SpringBoot整合示例MQSpring Boot
- 個人學習系列 - SpringBoot整合RabbitMQSpring BootMQ
- SpringBoot2.0應用(三):SpringBoot2.0整合RabbitMQSpring BootMQ
- SpringBoot2.0原始碼分析(三):整合RabbitMQ分析Spring Boot原始碼MQ
- SpringBoot整合RabbitMQ實戰附加死信交換機Spring BootMQ
- SpringBoot 整合 RabbitMQ 實現訊息可靠傳輸Spring BootMQ
- SpringBoot資料訪問(二) SpringBoot整合JPASpring Boot
- SpringBoot整合rabbitMq實現訊息延時傳送Spring BootMQ
- RabbitMQ 入門案例 - Work 模式 - 輪詢模式MQ模式
- RabbitMQ-Spring整合RabbitMQMQSpring
- SpringBoot整合RabbitMQ之典型應用場景實戰一Spring BootMQ
- SpringBoot2.0應用(二):SpringBoot2.0整合ActiveMQSpring BootMQ
- Springboot + rabbitMq佇列Spring BootMQ佇列
- Spring Data JPA(二):SpringBoot整合H2Spring Boot
- 整合RabbitMQ&SpringMQSpring
- Spring Boot整合rabbitmqSpring BootMQ
- Spring Boot 整合 rabbitmqSpring BootMQ
- spring boot-整合RabbitMq(RabbitMq基礎)Spring BootMQ
- SpringBoot邊學邊用(二)SpringBoot 整合 Mybatis(xml檔案形式)Spring BootMyBatisXML
- RabbitMQ(三):RabbitMQ與Spring Boot簡單整合MQSpring Boot
- SpringBoot2.0原始碼分析(二):整合ActiveMQ分析Spring Boot原始碼MQ
- RabbitMQ - SpringBoot 案例 - direct 模式MQSpring Boot模式
- RabbitMQ - SpringBoot 案例 - topic 模式MQSpring Boot模式