disruptor demo(三) 复杂一点的例子


disruptor demo(三) 复杂一点的例子_第1张图片

从中图可以看出需求是介样子的:生产者生产数据经过C1,C2处理完成后再到C3。

假设如下场景:

1、交易网关收到交易(P1)把交易数据发到RingBuffer中,

2、负责处理增值业务的消费者C1和负责数据存储的消费者C2负责处理交易

3、负责发送JMS消息的消费者C3在C1和C2处理完成后再进行处理。



1.

package p1;
//POJO 交易类
public class TradeTransaction {
	private String id; //交易ID
	private double price;//交易金额
	
	public TradeTransaction(){}
	
	public TradeTransaction(String id,double price){
		super();
		this.id = id;
		this.price = price;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}
	
}


2.事件处理器(消费者)

package p1;
import java.util.UUID;


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


public class TradeTransactionInDBHandler implements EventHandler{

	@Override
	public void onEvent(TradeTransaction event, long sequence, boolean endOfBatch) throws Exception {
		this.onEvent(event);
	}

	public void onEvent(TradeTransaction event) throws Exception {
		event.setId(UUID.randomUUID().toString());
		System.out.println(event.getId());
		
	}

}

package p1;

import com.lmax.disruptor.EventHandler;

public class TradeTransactionVasConsumer implements EventHandler {

	@Override
	public void onEvent(TradeTransaction event, long sequence,
			boolean endOfBatch) throws Exception {
		//do something....
	}
	
}


package p1;

import com.lmax.disruptor.EventHandler;

public class TradeTransactionJMSNotifyHandler implements EventHandler{

	@Override
	public void onEvent(TradeTransaction event, long sequence, boolean endOfBatch) throws Exception {
		
	}

}



3.生产者

package p1;

import java.util.Random;

import com.lmax.disruptor.EventTranslator;

public class TradeTransactionEventTranslator implements EventTranslator{

	private Random random=new Random();
	@Override
	public void translateTo(TradeTransaction event, long sequence) {
		this.generateTradeTransaction(event);
	}
	private TradeTransaction generateTradeTransaction(TradeTransaction trade){
		trade.setPrice(random.nextDouble()*9999);
		return trade;
	}
}

package p1;

import java.util.concurrent.CountDownLatch;

import com.lmax.disruptor.dsl.Disruptor;

public class TradeTransactionPublisher implements Runnable{
	Disruptor disruptor;
	private CountDownLatch latch;
	private static int LOOP=10000000;//模拟一千万次交易的发生
	
	public TradeTransactionPublisher(CountDownLatch latch,Disruptor disruptor){
			this.disruptor = disruptor;
			this.latch = latch;
	}
	@Override
	public void run() {
		TradeTransactionEventTranslator tradeTransloator=new TradeTransactionEventTranslator();
		for(int i=0;i


4.demo类

package p1;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.lmax.disruptor.BusySpinWaitStrategy;
import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.EventHandlerGroup;
import com.lmax.disruptor.dsl.ProducerType;

public class Demo3 {
	public static void main(String[] args) throws Exception{
		long beginTime = System.currentTimeMillis();
		
		int bufferSize = 1024;
		
		ExecutorService executor=Executors.newFixedThreadPool(4);
		
		Disruptor disruptor=new Disruptor(new EventFactory() {
			@Override
			public TradeTransaction newInstance() {
				return new TradeTransaction();
			}
		}, bufferSize, executor, ProducerType.SINGLE, new BusySpinWaitStrategy());
		
		EventHandlerGroup handlerGroup=disruptor.handleEventsWith(new TradeTransactionVasConsumer(),new TradeTransactionInDBHandler());
	
		TradeTransactionJMSNotifyHandler jmsConsumer=new TradeTransactionJMSNotifyHandler();
		//声明在C1,C2完事之后执行JMS消息发送操作 也就是流程走到C3
		handlerGroup.then(jmsConsumer);
		
		disruptor.start();
		CountDownLatch latch = new CountDownLatch(1);
		executor.submit(new TradeTransactionPublisher(latch, disruptor));
		latch.await();//等待生产者完事.
		disruptor.shutdown();
		executor.shutdown();
		
		System.out.println("总耗时:"+(System.currentTimeMillis()-beginTime));
	}



你可能感兴趣的:(disruptor)