先直接上代码:
package com.liuxd.eventBus;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.SubscriberExceptionContext;
import com.google.common.eventbus.SubscriberExceptionHandler;
import javax.annotation.Resource;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by Liuxd on 2018/8/16.
*/
public class Producer {
private int maxPoolSize = 150;
private AsyncEventBus sendMessEventBus;
@Resource
Consumer consumer = new Consumer();
private ThreadPoolExecutor threadPoolExecutor;
public static void main(String[] args) {
Producer producer = new Producer();
producer.init();
System.out.println(producer.getCorePoolSize());
for (int i = 0; i < 1000; i++) {
System.out.println("当前剩余任务数:"+producer.getTaskCount());
producer.post("消息体" + i);
}
}
// @PostConstruct
public void init() {
threadPoolExecutor = newThreadPool();
sendMessEventBus = new AsyncEventBus(threadPoolExecutor, new SubscriberExceptionHandler() {
public void handleException(Throwable exception, SubscriberExceptionContext context) {
exception.printStackTrace();
System.out.println(exception.getMessage());
System.out.println(exception.toString());
}
});
sendMessEventBus.register(consumer);
}
public void post(String message) {
sendMessEventBus.post(message);
}
private ThreadPoolExecutor newThreadPool() {
return new ThreadPoolExecutor(maxPoolSize, maxPoolSize,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(200), new DefaultThreadFactory(), new RejectedExecutionHandler() {
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
//TODO
}
});
}
public void setCorePoolSize(int size) {
threadPoolExecutor.setCorePoolSize(size);
threadPoolExecutor.setMaximumPoolSize(size);
}
public int getCorePoolSize() {
return threadPoolExecutor.getCorePoolSize();
}
public int getTaskCount() {
return threadPoolExecutor.getQueue().size();
}
static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "evnetbusPmpool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
}
消费者---订阅者
package com.liuxd.eventBus;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import java.util.Random;
/**
* 事件
*/
public class Consumer {
/**
* 处理事件触发
*/
@Subscribe
@AllowConcurrentEvents
public void processor(String message) {
myPrint(message);
}
private void myPrint(String message) {
System.out.println("打印:"+message+"_当前线程id为:"+Thread.currentThread().getId());
}
public static void main(String[] args) {
Consumer consumer = new Consumer();
}
}