Disruptor 实战

依赖 : disruptor-2.10.4.jar

package com.lmax.disruptor;

/**
 * User: caiyuan
 * Date: 13-2-18 16:56
 */
public final class DispatchEventHandler<T> implements EventHandler<T>, LifecycleAware {

    private final EventHandler<T>[] eventHandlers;
    private final int size;
    private int index = -1;

    public DispatchEventHandler(final EventHandler<T>... eventHandlers) {
        this.eventHandlers = eventHandlers;
        this.size = eventHandlers.length;
    }

    @Override
    public void onEvent(final T event, final long sequence, final boolean endOfBatch) throws Exception {
        if (index == size || index == -1) index = 0;
        EventHandler<T> eventHandler = eventHandlers[index];
        eventHandler.onEvent(event, sequence, endOfBatch);
        index = index + 1;
    }

    @Override
    public void onStart() {
        for (final EventHandler<T> eventHandler : eventHandlers) {
            if (eventHandler instanceof LifecycleAware) {
                ((LifecycleAware) eventHandler).onStart();
            }
        }
    }

    @Override
    public void onShutdown() {
        for (final EventHandler<T> eventHandler : eventHandlers) {
            if (eventHandler instanceof LifecycleAware) {
                ((LifecycleAware) eventHandler).onShutdown();
            }
        }
    }

}

package com.lmax.disruptor;

/**
 * POJO
 */
public class SimpleEvent {

    private String value;

    public String getValue() {
        return value;
    }

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

}

package com.lmax.disruptor;

/**
 * 事件生产者
 */
public class SimpleEventFactory {

    public final static EventFactory<SimpleEvent> EVENT_FACTORY = new EventFactory<SimpleEvent>() {
        public SimpleEvent newInstance() {
            return new SimpleEvent();
        }
    };

}

package com.lmax.disruptor;

/**
 * 事件处理器
 */
public class SimpleEventHandler implements EventHandler<SimpleEvent> {

    private RingBuffer<SimpleEvent> ringBuffer;

    public SimpleEventHandler(RingBuffer<SimpleEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    @Override
    public void onEvent(SimpleEvent event, long sequence, boolean endOfBatch) throws Exception {
        if (ringBuffer != null) {
            String value = event.getValue();
            long index = ringBuffer.next();
            SimpleEvent entity = ringBuffer.get(index);
            entity.setValue(value + "B");
            ringBuffer.publish(index);
        } else {
            System.out.println(sequence +"\t" +event.getValue());
        }
    }

}

package com.lmax.disruptor;

/**
 * 装载事件,事件处理器 和 RingBuffer
 */
public class SimpleTest {

    public static void main(String[] args) {

        final RingBuffer<SimpleEvent> rb1 = new RingBuffer<SimpleEvent>(
                SimpleEventFactory.EVENT_FACTORY,
                new MultiThreadedClaimStrategy(2048),
                new YieldingWaitStrategy());

        final RingBuffer<SimpleEvent> rb2 = new RingBuffer<SimpleEvent>(
                SimpleEventFactory.EVENT_FACTORY,
                new MultiThreadedClaimStrategy(2048),
                new YieldingWaitStrategy());

        EventHandler seh1 = new SimpleEventHandler(null);
        EventHandler seh2 = new SimpleEventHandler(null);
        EventHandler seh3 = new SimpleEventHandler(rb2);
        EventHandler seh4 = new SimpleEventHandler(null);
        EventHandler seh5 = new SimpleEventHandler(null);

        final EventHandler<SimpleEvent> eh1 = new DispatchEventHandler<SimpleEvent>(seh1, seh2);
        final EventHandler<SimpleEvent> eh2 = new AggregateEventHandler<SimpleEvent>(eh1, seh3);
        final EventHandler<SimpleEvent> eh3 = new AggregateEventHandler<SimpleEvent>(seh4, seh5);

        final EventProcessor ep1 = new BatchEventProcessor<SimpleEvent>(
                rb1,
                rb1.newBarrier(),
                eh2);

        final EventProcessor ep2 = new BatchEventProcessor<SimpleEvent>(
                rb2,
                rb2.newBarrier(),
                eh3);

        rb1.setGatingSequences(ep1.getSequence());
        rb2.setGatingSequences(ep2.getSequence());

        final long start = System.currentTimeMillis();

        new Thread(ep1, "EP1").start();
        new Thread(ep2, "EP2").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                int count = 0;
                while (count < 9000000) {
                    long sequence = rb1.next();
                    SimpleEvent event = rb1.get(sequence);
                    event.setValue("A");
                    rb1.publish(sequence);
                    count = count + 1;
                }
                System.out.println("T" + (System.currentTimeMillis() - start));
            }
        }, "P1").start();

    }

}


输出:
0 A
1 A
2 A
0 AB
3 A
0 AB
4 A
5 A
6 A
7 A
8 A
9 A
10 A
1 AB
11 A
1 AB
12 A
13 A
....

转载于:https://my.oschina.net/caiyuan/blog/109141

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值