使用Disruptor完成多个消费者不重复消费消息

上一篇https://blog.csdn.net/tianyaleixiaowu/article/details/79787377里讲了Disruptor完成多个消费者并行、顺序重复消费Event。重复消费类似于kafka中,同一个topic被不同的group的消费者消费。这样的场景比较常见。当然更常见的场景是不重复消费,也就是一个消息只能被消费一次。

Disruptor同样可以完成不重复消费的功能。

上一篇消费者消费Handler是这样的,需要实现EventHandler。

/**
 * @author wuweifeng wrote on 2018/3/29.
 */
public class LastEventHandler implements EventHandler<LongEvent> {

    @Override
    public void onEvent(LongEvent secondEvent, long sequence, boolean endOfBatch) throws Exception {
        System.out.println("-------Last:" + secondEvent);
    }
}

这次的消费者需要实现不同的接口,代码如下,注意,代码是接续上一篇的,需要先下载上一篇的代码。

import a.LongEvent;
import com.lmax.disruptor.WorkHandler;

/**
 * @author wuweifeng wrote on 2018/4/8.
 */
public class Consumer implements WorkHandler<LongEvent> {
    @Override
    public void onEvent(LongEvent longEvent) throws Exception {
        System.out.println(Thread.currentThread().getName() + "消费者消费了消息:" + longEvent.toString());
    }
}
import a.LongEvent;
import a.LongEventFactory;
import a.LongEventProducer;
import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.nio.ByteBuffer;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * @author wuweifeng wrote on 2018/4/8.
 */
@SuppressWarnings("ALL")
public class Test {
    public static void main(String[] args) {
        ThreadFactory producerFactory = Executors.defaultThreadFactory();
        // 创建缓冲池
        LongEventFactory eventFactory = new LongEventFactory();

        // 创建bufferSize ,也就是RingBuffer大小,必须是2的N次方
        int ringBufferSize = 1024 * 1024;
        Disruptor<LongEvent> disruptor = new Disruptor<>(eventFactory, ringBufferSize, producerFactory,
                ProducerType.SINGLE, new BlockingWaitStrategy());
        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();

        // 创建10个消费者来处理同一个生产者发的消息(这10个消费者不重复消费消息)
        Consumer[] consumers = new Consumer[10];
        for (int i = 0; i < consumers.length; i++) {
            consumers[i] = new Consumer();
        }
        disruptor.handleEventsWithWorkerPool(consumers);

        disruptor.start();

        LongEventProducer longEventProducer = new LongEventProducer(ringBuffer);
        ByteBuffer bb = ByteBuffer.allocate(8);
        for (long i = 0; i < 100L; i++) {
            bb.putLong(0, i);
            longEventProducer.onData(bb);
        }

        disruptor.shutdown();
    }
}

这个是测试类,可以发现和上一篇的主要区别就是disruptor.handleEventsWithWorkerPool。其他的都是一样的。

使用handleEventsWithWorkerPool就可以完成不重复消费,使用handleEventsWith就是重复消费。这里定义了10个消费者,那么就会启动10个线程来不重复消费生产者发出的100条消息。


  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天涯泪小武

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值