RabbitMQ(二)JavaClient SpringBoot整合 Work queues

芳哥_143671發表於2020-11-17

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);
    }
 

相關文章