Disruptor-07 常见模式代码范例

public class Test {

	private static Logger logger = LogManager.getLogger();

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws InterruptedException {

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

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

		// Construct the Disruptor
		Disruptor<TestEvent> disruptor = new Disruptor<TestEvent>(factory, bufferSize,
				DaemonThreadFactory.INSTANCE, ProducerType.SINGLE, new BlockingWaitStrategy());

		// Create EventHandler
		TestEventHandler handler1 = new TestEventHandler("handler1");
		TestEventHandler handler2 = new TestEventHandler("handler2");
		TestEventHandler handler3 = new TestEventHandler("handler3");
		TestEventHandler handler4 = new TestEventHandler("handler4");

		// Connect the handler

		int count = 100;

		// Unicast采取WorkPool方式,3个WorkHandler 累计执行100次。
		// Event到达时,哪个WorkHandler被调度不确定。
		// disruptor.handleEventsWithWorkerPool(handler1, handler2, handler3);

		// MulticastTest并发处理方式,3个EventHandler,各执行100次,累计300次;
		// 每个Event到达时,EventHandler的处理顺序不确定。
		// 并发( handler1, handler2, handler3)
		// EventHandler:handler1--85:k-v
		// EventHandler:handler3--85:k-v
		// EventHandler:handler2--85:k-v

		// EventHandler:handler1--86:k-v
		// EventHandler:handler3--86:k-v
		// EventHandler:handler2--86:k-v <-----并发顺序不确定

		// EventHandler:handler2--87:k-v <-----并发顺序不确定
		// EventHandler:handler1--87:k-v
		// EventHandler:handler3--87:k-v

		// EventHandler:handler3--88:k-v
		// EventHandler:handler2--88:k-v
		// EventHandler:handler1--88:k-v

		// disruptor.handleEventsWith(handler1, handler2, handler3);

		// Pipeline串行处理方式,3个EventHandler,各执行100次,累计300次。
		// 每个Event到达时,EventHandler的处理顺序与handleEventsWith的顺序一致。
		// 顺序:handler1->handler2->handler3
		// EventHandler:handler1--97:k-v
		// EventHandler:handler2--97:k-v
		// EventHandler:handler3--97:k-v

		// EventHandler:handler1--98:k-v
		// EventHandler:handler2--98:k-v
		// EventHandler:handler3--98:k-v

		// EventHandler:handler1--99:k-v
		// EventHandler:handler2--99:k-v
		// EventHandler:handler3--99:k-v

		// EventHandler:handler1--100:k-v
		// EventHandler:handler2--100:k-v
		// EventHandler:handler3--100:k-v
		
		//disruptor.handleEventsWith(handler1).handleEventsWith(handler2).handleEventsWith(handler3);
		
		//Diamond
		//按照 handler1-> 并发(handler2, hander3) ->handler4 调度
		disruptor.handleEventsWith(handler1).handleEventsWith(handler2,handler3).handleEventsWith(handler4);

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

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

		TestEventProducer producer = new TestEventProducer(ringBuffer);

		for (int i = 1; i <= count; i++) {
			producer.onEvent("k", "v");
			Thread.sleep(100);
		}

		Thread.sleep(10000);
	}

}
public class TestEvent implements Event {

	private String key;
	private String value;

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getValue() {
		return value;
	}

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

}

 

package com.lands.disruptor.unicast;

import com.lmax.disruptor.EventFactory;

public class TestEventFactory implements EventFactory<TestEvent> {

	public TestEvent newInstance() {
		return new TestEvent();
	}

}

 

package com.lands.disruptor.unicast;

import java.util.concurrent.atomic.AtomicInteger;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.WorkHandler;

public class TestEventHandler implements EventHandler<TestEvent>, WorkHandler<TestEvent> {

	private static Logger logger = LogManager.getLogger();

	private String handlerName;

	private AtomicInteger count = new AtomicInteger();

	public TestEventHandler(String name) {
		this.handlerName = name;
	}

	public String getHandlerName() {
		return handlerName;
	}

	public void onEvent(TestEvent event) throws Exception {

		logger.info("WorkHandler:" + this.handlerName + "-" + count.decrementAndGet() + ":" + event.getKey() + "-"
				+ event.getValue());

		//Thread.sleep(100);
	}

	public void onEvent(TestEvent event, long sequence, boolean endOfBatch) throws Exception {
		logger.info("EventHandler:" + this.handlerName + "-" + count.decrementAndGet() + ":" + event.getKey() + "-"
				+ event.getValue());
		//Thread.sleep(100);
	}

}

 

package com.lands.disruptor.unicast;

import com.lands.disruptor.EventProducer;
import com.lmax.disruptor.RingBuffer;

public class TestEventProducer extends EventProducer<TestEvent> {

	public TestEventProducer(RingBuffer<TestEvent> ringBuffer) {
		super(ringBuffer);
	}

	@Override
	public void process(TestEvent event, String... data) {

		event.setKey(data[0]);
		event.setValue(data[1]);

	}

}

 

 

 

 

 

 

转载于:https://my.oschina.net/blacklands/blog/3086092

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Java Disruptor 代码示例: ```java import com.lmax.disruptor.RingBuffer; import com.lmax.disruptor.dsl.Disruptor; import java.nio.ByteBuffer; import java.util.concurrent.Executor; import java.util.concurrent.Executors; public class LongEvent { private long value; public void set(long value) { this.value = value; } } class LongEventFactory implements EventFactory<LongEvent> { public LongEvent newInstance() { return new LongEvent(); } } class LongEventHandler implements EventHandler<LongEvent> { public void onEvent(LongEvent event, long sequence, boolean endOfBatch) { System.out.println("Event: " + event); } } public class DisruptorExample { public static void main(String[] args) { Executor executor = Executors.newCachedThreadPool(); LongEventFactory factory = new LongEventFactory(); int bufferSize = 1024; Disruptor<LongEvent> disruptor = new Disruptor<>(factory, bufferSize, executor); disruptor.handleEventsWith(new LongEventHandler()); RingBuffer<LongEvent> ringBuffer = disruptor.start(); ByteBuffer bb = ByteBuffer.allocate(8); for (long l = 0; true; l++) { bb.putLong(0, l); ringBuffer.publishEvent(new EventTranslatorOneArg<LongEvent, ByteBuffer>() { public void translateTo(LongEvent event, long sequence, ByteBuffer arg0) { event.set(arg0.getLong(0)); } }, bb); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } ``` 该代码使用 LMAX Disruptor 实现了一个简单的事件处理程序。Disruptor 可以高效地处理并发事件处理任务,例如高性能日志记录、消息传递和网络通信。在这个例子中,我们定义了一个事件对象 LongEvent,包含一个 long 值。我们使用 LongEventFactory 为 Disruptor 创建新的事件对象,使用 LongEventHandler 处理事件。我们还定义了一个 ByteBuffer,用于在主线程中创建事件并将其发布到 Disruptor 中。在主线程中,我们每秒钟创建一个新事件并将其发布到 Disruptor 中。在事件处理程序中,我们简单地打印事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值