disruptor 生产者消费者

package hua;

/**
 * 事件
 */
public class LongEvent {
    public String name;

    public long value;

    public long getValue() {
        return value;
    }

    public void setValue(long value) {
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}



 

package hua;

import com.lmax.disruptor.EventFactory;

/**
  事件工厂
 */
public class LongEventFactory implements EventFactory<LongEvent> {

    public LongEvent newInstance()
    {
        return new LongEvent();
    }
}
package hua;

import com.lmax.disruptor.EventHandler;

/**
  消费者
 */
public class LongEventConsumer implements EventHandler<LongEvent>
{
    public void onEvent(LongEvent event, long sequence, boolean endOfBatch)
    {
        //获取数据处理自己的业务
        System.out.println( event.getName() +" "+ event.value + " sequence="+sequence + " endOfBatch="+endOfBatch);
    }
}

package hua;
 
import com.lmax.disruptor.RingBuffer; 
/**
 * 生产者 方式1
 */
public class LongEventProducer
{
    private final RingBuffer<LongEvent> ringBuffer;

    public LongEventProducer(RingBuffer<LongEvent> ringBuffer)
    {
        this.ringBuffer = ringBuffer;
    }

    public void onData(LongEvent bb)
    {
        long sequence = ringBuffer.next();  // Grab the next sequence
        try
        {
            LongEvent event = ringBuffer.get(sequence); // Get the entry in the Disruptor
            event.setName(bb.getName());
            event.setValue(bb.getValue());
        }
        finally
        {
            ringBuffer.publish(sequence);
        }
    }
}
package hua;

/**
 * 生产者 方式2
 */
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.EventTranslatorOneArg;
 

public class LongEventProducerWithTranslator
{
    private final RingBuffer<LongEvent> ringBuffer;

    public LongEventProducerWithTranslator(RingBuffer<LongEvent> ringBuffer)
    {
        this.ringBuffer = ringBuffer;
    }

    private static final EventTranslatorOneArg<LongEvent, LongEvent> TRANSLATOR = new EventTranslatorOneArg<LongEvent, LongEvent>()
            {
                public void translateTo(LongEvent event, long sequence, LongEvent bb)
                {
                    event.setName(bb.getName());
                    event.setValue(bb.getValue());
                }
            };

    public void onData(LongEvent bb)
    {
        ringBuffer.publishEvent(TRANSLATOR, bb);
    }
}
package hua;

/**
 
 */
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.util.DaemonThreadFactory;


public class LongEventMain
{
    public static void main(String[] args) throws Exception{
        LongEventFactory factory = new LongEventFactory();
        int bufferSize = 1024;
        Disruptor<LongEvent> disruptor = new Disruptor<>(factory, bufferSize, DaemonThreadFactory.INSTANCE);
        disruptor.handleEventsWith(new LongEventConsumer());
        disruptor.start();

        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
        LongEventProducerWithTranslator producer = new LongEventProducerWithTranslator(ringBuffer);
        for (long l = 0; true; l++){
            String name = "test"+l;
            LongEvent e = new LongEvent();
            e.setName(name);
            e.setValue(l);
            producer.onData(e);
            Thread.sleep(1000);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lihuayingmail

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

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

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

打赏作者

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

抵扣说明:

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

余额充值