mirror of
https://gitee.com/52itstyle/spring-boot-seckill.git
synced 2025-12-30 10:22:26 +00:00
:sparkles:JDK8
This commit is contained in:
@@ -12,16 +12,12 @@ import com.lmax.disruptor.dsl.Disruptor;
|
||||
*/
|
||||
public class DisruptorUtil {
|
||||
|
||||
static Disruptor<SeckillEvent> disruptor = null;
|
||||
static Disruptor<SeckillEvent> disruptor;
|
||||
static{
|
||||
SeckillEventFactory factory = new SeckillEventFactory();
|
||||
int ringBufferSize = 1024;
|
||||
ThreadFactory threadFactory = new ThreadFactory() {
|
||||
public Thread newThread(Runnable runnable) {
|
||||
return new Thread(runnable);
|
||||
}
|
||||
};
|
||||
disruptor = new Disruptor<SeckillEvent>(factory, ringBufferSize, threadFactory);
|
||||
ThreadFactory threadFactory = runnable -> new Thread(runnable);
|
||||
disruptor = new Disruptor<>(factory, ringBufferSize, threadFactory);
|
||||
disruptor.handleEventsWith(new SeckillEventConsumer());
|
||||
disruptor.start();
|
||||
}
|
||||
|
||||
@@ -12,7 +12,8 @@ public class SeckillEventConsumer implements EventHandler<SeckillEvent> {
|
||||
|
||||
private ISeckillService seckillService = (ISeckillService) SpringUtil.getBean("seckillService");
|
||||
|
||||
public void onEvent(SeckillEvent seckillEvent, long seq, boolean bool) throws Exception {
|
||||
@Override
|
||||
public void onEvent(SeckillEvent seckillEvent, long seq, boolean bool) {
|
||||
seckillService.startSeckil(seckillEvent.getSeckillId(), seckillEvent.getUserId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,8 @@ import com.lmax.disruptor.EventFactory;
|
||||
*/
|
||||
public class SeckillEventFactory implements EventFactory<SeckillEvent> {
|
||||
|
||||
public SeckillEvent newInstance() {
|
||||
@Override
|
||||
public SeckillEvent newInstance() {
|
||||
return new SeckillEvent();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,7 @@ public class SeckillEventMain {
|
||||
public static void producerWithTranslator(){
|
||||
SeckillEventFactory factory = new SeckillEventFactory();
|
||||
int ringBufferSize = 1024;
|
||||
ThreadFactory threadFactory = new ThreadFactory() {
|
||||
public Thread newThread(Runnable runnable) {
|
||||
return new Thread(runnable);
|
||||
}
|
||||
};
|
||||
ThreadFactory threadFactory = runnable -> new Thread(runnable);
|
||||
//创建disruptor
|
||||
Disruptor<SeckillEvent> disruptor = new Disruptor<SeckillEvent>(factory, ringBufferSize, threadFactory);
|
||||
//连接消费事件方法
|
||||
|
||||
@@ -9,12 +9,10 @@ import com.lmax.disruptor.RingBuffer;
|
||||
*/
|
||||
public class SeckillEventProducer {
|
||||
|
||||
private final static EventTranslatorVararg<SeckillEvent> translator = new EventTranslatorVararg<SeckillEvent>() {
|
||||
public void translateTo(SeckillEvent seckillEvent, long seq, Object... objs) {
|
||||
seckillEvent.setSeckillId((Long) objs[0]);
|
||||
seckillEvent.setUserId((Long) objs[1]);
|
||||
}
|
||||
};
|
||||
private final static EventTranslatorVararg<SeckillEvent> translator = (seckillEvent, seq, objs) -> {
|
||||
seckillEvent.setSeckillId((Long) objs[0]);
|
||||
seckillEvent.setUserId((Long) objs[1]);
|
||||
};
|
||||
|
||||
private final RingBuffer<SeckillEvent> ringBuffer;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user