Spring Boot集成EventBus(Guava方式)

依赖

1
2
3
4
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Configuration
public class AsyncEventBusConfig {

@Bean
@Scope("singleton")
public AsyncEventBus asyncEventBus() {

final ThreadPoolTaskExecutor executor = executor();
return new AsyncEventBus("Merchant-AsyncEventBus", executor);
}

@Bean
public ThreadPoolTaskExecutor executor() {

ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(20);
return executor;
}
}

生产者

1
2
3
4
5
6
7
@Autowired
private AsyncEventBus asyncEventBus;

public void myFunc(MyEvent event) {
...
asyncEventBus.post(MyEvent.builder().id(1).name("Joe.Ye").build());
}

消费者

1
2
3
4
5
6
7
8
9
10
11
12
@Autowired
private AsyncEventBus asyncEventBus;

@Subscribe
public void listenMyEvent(MyEvent event) {

}

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

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :