Disruptor3.x 的简单封装



package com.www.pay.business;

/**
 * ------------------------------
 * 事件对象
 * ------------------------------
 * @author wdm  @date 2018年1月12日
 * @version 1.0
 */
public class Event{ 
	
	private EventData  data;

	public Event(){}
    
    public Event(EventData data){
    	this.data=data;
    }

	public EventData getData() {
		return data;
	}

	public void setData(EventData data) {
		this.data = data;
	}
} 

package com.www.pay.business;

import com.cdoframework.cdolib.data.cdo.CDO;

/**
 * ------------------------------
 * 事件数据
 * ------------------------------
 * @author wdm  @date 2018年1月12日
 * @version 1.0
 */
public class EventData {
	
	/**
	 * 这个数据类型根据业务自定义
	 * 可以定义为任意数据类型
	 */
	public CDO data;
	public EventData(CDO data){
		this.data=data;
	}
	
}

package com.www.pay.business;

import org.apache.log4j.Logger;

import com.lmax.disruptor.EventHandler;

/**
 * ------------------------------
 * 交易事件处理
 * ------------------------------
 * @author wdm  @date 2018年1月12日
 * @version 1.0
 */
public class DisruptorEventHandler implements EventHandler<Event> {
	private static Logger logger=Logger.getLogger(DisruptorEventHandler.class);
	
	private String name;
	
	public DisruptorEventHandler(String name){
		this.name=name;
	}
	
    @Override 
    public void onEvent(Event event, long l, boolean b){ 
    	try {
    		System.out.println("收到消息:"+name);
    		System.out.println(name+"----------"+event.getData().data); 
    		Thread.sleep(1);
		} catch (Exception e) {
			logger.error("处理交易数据异常:"+e.getMessage(),e);
		}
    } 
} 

package com.www.pay.business;

public interface IEventPublisher {
	
	public void start();
	
	public void publish(EventData data);
	
}

package com.www.pay.business;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

/**
 * ------------------------------
 * Disruptor发布对象
 * ------------------------------
 * @author wdm  @date 2018年1月12日
 * @version 1.0
 */
public class DisruptorPublisher implements IEventPublisher {
	
	//等待策略
    private static final WaitStrategy YIELDING_WAIT = new YieldingWaitStrategy();
    //事件工厂
    private static final EventFactory<Event> EVENT_FACTORY = new  EventFactory<Event>() { 
        @Override 
        public Event newInstance() { 
            return new Event(); 
        } 
    } ;
    private static final EventTranslatorOneArg<Event, EventData> TRANSLATOR = new EventTranslatorOneArg<Event, EventData>() { 
    	public void translateTo(Event event, long sequence, EventData data) { 
    		event.setData(data);
    	} 
    };
    
	

    private Disruptor<Event> disruptor;
    private EventHandler<? super Event>[] handler;
    private RingBuffer<Event> ringbuffer;    
    private ThreadFactory threadFactory;

    public DisruptorPublisher(int bufferSize, EventHandler<? super Event>[] handler) {
        this.handler = handler;
        threadFactory = Executors.defaultThreadFactory();
        disruptor = new Disruptor<Event>(EVENT_FACTORY , bufferSize ,  threadFactory, ProducerType.SINGLE,YIELDING_WAIT);
    }

    @Override
    public void start() {
        disruptor.handleEventsWith(handler);
        disruptor.start();
        ringbuffer = disruptor.getRingBuffer();
    }

    @Override
    public void publish(EventData data) {
//        long sequence = ringbuffer.next();
//        try {
//            Event evt = ringbuffer.get(sequence);
//            evt.setTrade(data);
//        } finally {
//            ringbuffer.publish(sequence);
//        }
    		ringbuffer.publishEvent(TRANSLATOR, data); 
    }
}

/**
	maven坐标
	<dependency>
	    <groupId>com.lmax</groupId>
	    <artifactId>disruptor</artifactId>
	    <version>3.3.7</version>
	</dependency>
*/
public class TestDisruptor {

    public static void main(String[] args) {
    	EventHandler<Event>[] eventHandlers=new DisruptorEventHandler[]{new DisruptorEventHandler("处理器1"),new DisruptorEventHandler("处理器2")};
    	DisruptorPublisher dp=new DisruptorPublisher(1024, eventHandlers);
    	dp.start();
    	
    	for (int i = 0; true; i++) {
    		CDO cdo=new CDO();
    		cdo.setStringValue("strSN", i+"-"+DataUtil.uuid());
    		EventData data=new EventData(cdo);
    		dp.publish(data);
		}
    	
	}
	
}

说明:

1.在发布事件时是两阶段的:

第一步获取一个事件位置序列号,第二步发布事件(这一步必须放finally中防止业务异常导致RingBuffer中位置错乱)

使用最新版的推荐使用:ringbuffer.publishEvent(TRANSLATOR, data); 这种方式




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值