disruptor 核心链路的高级操作
disruptor 控制核心链路十分方便,并且可以串行,并行,菱形,多边形链路操作。
启动类
package com.tkn.disruptor.heigh.chain;
import com.lmax.disruptor.BusySpinWaitStrategy;
import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.EventHandlerGroup;
import com.lmax.disruptor.dsl.ProducerType;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
public static void main(String[] args) throws InterruptedException {
//构建一个线程池用于提交任务
ExecutorService el = Executors.newFixedThreadPool(4);
// 1 构建disruptor
Disruptor<Trade> disruptor = new Disruptor<Trade>(
new EventFactory<Trade>() {
@Override
public Trade newInstance() {
return new Trade();
}
},
1024 * 1024,
Executors.defaultThreadFactory(),
ProducerType.SINGLE,
new BusySpinWaitStrategy()
);
//2 把消费者设置到Disruptor中 handleEventsWith
//2.1 串行操作:
disruptor.handleEventsWith(new Handler1())
.handleEventsWith(new Handler2())
.handleEventsWith(new Handler3());
//2.2 并行操作: 可以有两种方式去进行
//1 handleEventsWith方法 添加多个handler实现即可
disruptor.handleEventsWith(new Handler1(),new Handler3(),new Handler2());
//2 handleEventsWith方法 分别进行调用
disruptor.handleEventsWith(new Handler1());
disruptor.handleEventsWith(new Handler2());
disruptor.handleEventsWith(new Handler3());
//2.3 菱形操作 (一)
disruptor.handleEventsWith(new Handler1(), new Handler2()).handleEventsWith(new Handler3());
//2.3 菱形操作 (二)
EventHandlerGroup<Trade> handlerGroup = disruptor.handleEventsWith(new Handler1(), new Handler2());
handlerGroup.then(new Handler3());
//2.4 六边形操作
Handler1 h1 = new Handler1();
Handler2 h2 = new Handler2();
Handler3 h3 = new Handler3();
Handler4 h4 = new Handler4();
Handler5 h5 = new Handler5();
disruptor.handleEventsWith(h1, h4);
disruptor.after(h1).handleEventsWith(h2);
disruptor.after(h4).handleEventsWith(h5);
disruptor.after(h2,h5).handleEventsWith(h3);
//3 启动disruptor
RingBuffer<Trade> ringBuffer = disruptor.start();
// 阻塞业务操作
CountDownLatch countDownLatch = new CountDownLatch(1);
long begin = System.currentTimeMillis();
el.submit(new TradePushlisher(countDownLatch, disruptor));
countDownLatch.await();//进行向下
disruptor.shutdown();
el.shutdown();
System.err.println("总耗时: " + (System.currentTimeMillis() - begin));
}
}
实体类
package com.tkn.disruptor.heigh.chain;
import lombok.Data;
import java.util.concurrent.atomic.AtomicInteger;
@Data
public class Trade {
private String id;
private String name;
private double price;
private AtomicInteger count = new AtomicInteger(0);
}
生产者
package com.tkn.disruptor.heigh.chain;
import com.lmax.disruptor.EventTranslator;
import com.lmax.disruptor.dsl.Disruptor;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
public class TradePushlisher implements Runnable {
private CountDownLatch countDownLatch;
private Disruptor<Trade> disruptor;
private static int PUBLISH_COUNT = 1;
public TradePushlisher(CountDownLatch countDownLatch, Disruptor<Trade> disruptor) {
this.countDownLatch=countDownLatch;
this.disruptor=disruptor;
}
@Override
public void run() {
for (int i = 0; i < PUBLISH_COUNT; i++) {
//新的提交任务的方式
disruptor.publishEvent(new EventTranslator<Trade>() {
private Random random = new Random();
@Override
public void translateTo(Trade trade, long l) {
trade.setPrice(random.nextDouble() * 9999);
}
});
}
countDownLatch.countDown();
}
}
消费者1
package com.tkn.disruptor.heigh.chain;
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.WorkHandler;
public class Handler1 implements EventHandler<Trade>, WorkHandler<Trade>{
//EventHandler
public void onEvent(Trade event, long sequence, boolean endOfBatch) throws Exception {
this.onEvent(event);
}
//WorkHandler
public void onEvent(Trade event) throws Exception {
System.err.println("handler 1 : SET NAME");
Thread.sleep(1000);
event.setName("H1");
}
}
消费者2
package com.tkn.disruptor.heigh.chain;
import java.util.UUID;
import com.lmax.disruptor.EventHandler;
public class Handler2 implements EventHandler<Trade> {
public void onEvent(Trade event, long sequence, boolean endOfBatch) throws Exception {
System.err.println("handler 2 : SET ID");
Thread.sleep(2000);
event.setId(UUID.randomUUID().toString());
}
}
消费者3
package com.tkn.disruptor.heigh.chain;
import com.lmax.disruptor.EventHandler;
public class Handler3 implements EventHandler<Trade> {
public void onEvent(Trade event, long sequence, boolean endOfBatch) throws Exception {
System.err.println("handler 3 : NAME: "
+ event.getName()
+ ", ID: "
+ event.getId()
+ ", PRICE: "
+ event.getPrice()
+ " INSTANCE : " + event.toString());
}
}
消费者4
package com.tkn.disruptor.heigh.chain;
import com.lmax.disruptor.EventHandler;
public class Handler4 implements EventHandler<Trade> {
public void onEvent(Trade event, long sequence, boolean endOfBatch) throws Exception {
System.err.println("handler 4 : SET PRICE");
Thread.sleep(1000);
event.setPrice(17.0);
}
}
消费者5
package com.tkn.disruptor.heigh.chain;
import com.lmax.disruptor.EventHandler;
public class Handler5 implements EventHandler<Trade> {
public void onEvent(Trade event, long sequence, boolean endOfBatch) throws Exception {
System.err.println("handler 5 : GET PRICE: " + event.getPrice());
Thread.sleep(1000);
event.setPrice(event.getPrice() + 3.0);
}
}
---------------------
作者:天空鸟_时光不老
来源:CSDN
原文:https://blog.csdn.net/KongZhongNiao/article/details/86083417
版权声明:本文为博主原创文章,转载请附上博文链接!