Guava Async EventBus

实际例子:

 

package com.eloancn.back.schedule.core.config.event;


import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.UUID;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

@Configuration
public class EventBusConfig {

    private final static Logger logger= LoggerFactory.getLogger(EventBusConfig.class);
    @Bean(name = "eventBus")
    public EventBus getEventBus() {
        return new EventBus();
    }

    @Bean(name = "asyncEventBus")
    public AsyncEventBus getAsyncEventBus() {
        ThreadPoolExecutor executor=new ThreadPoolExecutor(10,150,100,
                TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(100),
                new CustomThreadFactory(),
                new CustomRejectedExecutionHandler());
        return new AsyncEventBus(executor);
    }

    private class CustomThreadFactory implements ThreadFactory {
        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setName(UUID.randomUUID().toString());
            t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
            return t;
        }
    }

    private class CustomRejectedExecutionHandler implements RejectedExecutionHandler {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            logger.info("线程池缓冲队列超出范围抛弃");
        }
    }
    private class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler{
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            logger.error("线程池异常捕获打印:{}",ExceptionUtils.getFullStackTrace(e));
        }
    }
}
package com.eloancn.back.schedule.core.async.producer;

import com.eloancn.back.schedule.core.async.event.AsyncRecordOperatorEvent;
import com.eloancn.back.schedule.core.async.event.RecordOperatorEvent;
import com.eloancn.back.schedule.core.model.ScheduleRecord;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AsyncRecordOperatorProducer {

    @Autowired
    AsyncEventBus asyncEventBus;

    public void producer(ScheduleRecord scheduleRecord){
        asyncEventBus.post(new AsyncRecordOperatorEvent(scheduleRecord));
    }

}
package com.eloancn.back.schedule.core.async.listener;

import com.eloancn.back.schedule.core.async.event.AsyncRecordOperatorEvent;
import com.eloancn.back.schedule.core.async.event.RecordOperatorEvent;
import com.eloancn.back.schedule.core.model.ScheduleRecord;
import com.eloancn.back.schedule.core.schedule.CoreSchedule;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
public class RecordOperatorAsyncListener {

    private Logger logger = LoggerFactory.getLogger(RecordOperatorAsyncListener.class) ;

    @Autowired
    AsyncEventBus asyncEventBus;

    @Autowired
    private CoreSchedule coreSchedule;

    @PostConstruct
    public void init(){
        asyncEventBus.register(this);
    }


    @Subscribe
    @AllowConcurrentEvents
    public void action(AsyncRecordOperatorEvent event) {
         try {
             ScheduleRecord scheduleRecord=event.getScheduleRecord();
             logger.info("async listener: {}",scheduleRecord.getId());
             coreSchedule.doSchedule(event.getScheduleRecord());
         }catch (Exception e){
             logger.error("异步消息响应异常:"+ExceptionUtils.getFullStackTrace(e));
         }
    }

}
package com.eloancn.back.schedule.core.async.event;

import com.eloancn.back.schedule.core.model.ScheduleRecord;

public class AsyncRecordOperatorEvent {

    private ScheduleRecord scheduleRecord;

    public AsyncRecordOperatorEvent() {

    }
    public AsyncRecordOperatorEvent(ScheduleRecord scheduleRecord) {
        this.scheduleRecord = scheduleRecord;
    }

    public ScheduleRecord getScheduleRecord() {
        return scheduleRecord;
    }

    public void setScheduleRecord(ScheduleRecord scheduleRecord) {
        this.scheduleRecord = scheduleRecord;
    }
}

 

转载于:https://my.oschina.net/payzheng/blog/1545805

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值