disruptor简单入门demo

首先写event

然后写eventFactory

然后写消费者eventHandler

和生产者eventProducer

最后写main

disruptor简单入门demo_第1张图片

例子:生产者每秒写入一个Long数字,消费者负责打印出来

disruptor简单入门demo_第2张图片

//event
package demo_1.event;

public class LongEvent {
    private long value;

    public void set(long value)
    {
        this.value = value;
    }
    public long getValue()
    {
       return this.value;
    }
}

//eventFactory
package demo_1.eventFactory;

import com.lmax.disruptor.EventFactory;
import demo_1.event.LongEvent;

public class LongEventFactory implements EventFactory {
    public LongEvent newInstance() {
        return new LongEvent();
    }
}

//EventHandler
package demo_1.eventHandler;

import com.lmax.disruptor.EventHandler;
import demo_1.event.LongEvent;

public class LongEventHandler implements EventHandler {
    public void onEvent(LongEvent event, long sequence, boolean endOfBatch) throws Exception {
        System.out.println("Event:  " + event.getValue()+"sequence:+"+sequence+"  endOfBatch:"+endOfBatch);
    }
}

//eventProducer
package demo_1.eventProducer;

import com.lmax.disruptor.RingBuffer;
import demo_1.event.LongEvent;

import java.nio.ByteBuffer;

public class LongEventProducer {
    private final RingBuffer ringBuffer;
    public LongEventProducer(RingBuffer ringBuffer)
    {
        this.ringBuffer = ringBuffer;
    }

    public void onData(ByteBuffer bb)
    {
        long sequence = ringBuffer.next();  // Grab the next sequence
        try
        {
            LongEvent event = ringBuffer.get(sequence); // Get the entry in the Disruptor
            // for the sequence
            event.set(bb.getLong(0));  // Fill with data
        }
        finally
        {
            ringBuffer.publish(sequence);
        }
    }

}

//最后整合 启动项目
package demo_1;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import demo_1.event.LongEvent;
import demo_1.eventFactory.LongEventFactory;
import demo_1.eventHandler.LongEventHandler;
import demo_1.eventProducer.LongEventProducer;

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

public class LongEventMain {
    public static void main(String[] args) throws Exception
    {
        // Executor that will be used to construct new threads for consumers
        Executor executor = Executors.newCachedThreadPool();

        // The factory for the event
        LongEventFactory factory = new LongEventFactory();

        // Specify the size of the ring buffer, must be power of 2.
        int bufferSize = 1024;

        // Construct the Disruptor
        Disruptor disruptor = new Disruptor<>(factory, bufferSize, executor);

        // Connect the handler
        disruptor.handleEventsWith(new LongEventHandler());

        // Start the Disruptor, starts all threads running
        disruptor.start();

        // Get the ring buffer from the Disruptor to be used for publishing.
        RingBuffer ringBuffer = disruptor.getRingBuffer();

        LongEventProducer producer = new LongEventProducer(ringBuffer);

        ByteBuffer bb = ByteBuffer.allocate(8);
        for (long l = 0; true; l++)
        {
            bb.putLong(0, l);
            producer.onData(bb);
        }
    }
}

 

你可能感兴趣的:(java)