【RabbitMQ Learning Journey】工作(公平性)队列

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

测试运行:

先启动消费者,再启动生产者

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值