实际例子:
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;
}
}