02_Disruptor初体验

来源:互联网 发布:南风知我意gl书包网 编辑:程序博客网 时间:2024/06/15 20:00

使用了disruptor,重构了项目里的任务调度模块。记录一下disruptor的初体验。项目里有个模块,使用了juc包里的并发实现生产消费的任务调度系统,目的是起缓存作用并将这个处理过程抽出来,以后好根据各种业务做变更。先贴上原先的代码。


1:任务上下文类。用来保存任务需要的上下文对象ABCD等需要的service,和主要要传递的msg内容。

public class Assignment {
private String msg;
private String A;
private String B;
private String C;
private String D;

}

2:调度器,相当于生产者,包含一个队列,和提供一个讲任务上下文对象塞入队列的方法,并在这里启动定时任务来消费这个队列
public class Schedule {
ConcurrentLinkedQueue<Assignment> assignments = new ConcurrentLinkedQueue<Assignment>();

public void assign(A a, String msg) {

Assignment assignment = new Assignment(msg,a,b,c,d);
assignments.add(assignment);
}


{

maxPar = xxx.

ScheduledExecutorService executor = Executors
.newScheduledThreadPool(maxPar);
for (int i = 0; i < maxPar; i++) {
executor.scheduleWithFixedDelay(new Handler(assignments), 1, 10,
TimeUnit.SECONDS);
}

}

}


3,业务处理器,消费者,讲复杂的处理过程放到这个类。




public class Handler implements Runnable {


@Override
public void run() {

dosm……复杂的业务处理过程

}

}


使用disrupt改造之后。


1:任务上下文对象,直接复用原来的Assignment 

2:调度器,使用disrupt,初始化一个,提供一个生产消息的方法,因为juc的包原先生产过程抽离出来一个类

public class Schedule {
private EventProducer producer;
public void assign(A a, String msg) {
producer.produce(resource, msg);
}

private void schedule() {
Executor executor = Executors.newFixedThreadPool(maxPar);
EventFactory factory = new EventFactory();
int bufferSize = 1024;
Disruptor<SQLEvent> disruptor = new Disruptor<Event>(factory,
bufferSize, executor);
disruptor.handleEventsWith(new EventHandler());
disruptor.start();
RingBuffer<SQLEvent> ringBuffer = disruptor.getRingBuffer();
producer.setQueue(ringBuffer);
}
}

3:接受业务消息端,生产端。

public class EventProducer {

private RingBuffer<Event> ringBuffer;
AtomicInteger taskNums = new AtomicInteger(0);


public void produce(A a, String msg) {
long sequence = ringBuffer.next();
Event assignment = ringBuffer.get(sequence);
assignment.init(msg, a, b, c);
int nowValue = taskNums.incrementAndGet();
resource.write("<INFO>:0:已加入任务队列中,等待执行...,处于任务队列的第 " + nowValue + " 位");
ringBuffer.publish(sequence);
}
}

4:业务处理端,消费端

public class EventHandler implements EventHandler<Event> {
@Override
public void onEvent(SQLEvent assignment, long l, boolean b)
throws Exception {

assignment.xxx().write("<INFO>:1:storm资源分配中...");
try {
semaphore.acquire();
execute(assignment);
} catch (Exception e) {

异常处理
} finally {
semaphore.release();
}

}


/**
* 执行任务
*/
private void execute(Assignment assignment) throws IOException,
InterruptedException {
xxx业务处理
}
}



1 0
原创粉丝点击