一、前言

实际业务开发过程中,业务逻辑可能非常复杂,核心业务 + N个子业务。如果都放到一块儿去做,代码可能会很长,耦合度不断攀升,维护起来也麻烦,甚至头疼。还有一些业务场景不需要在一次请求中同步完成,比如邮件发送、短信发送等。MQ 确实可以解决这个问题,但MQ重啊,非必要不提升架构复杂度。针对这些问题,我们了解一下 Spring Event。

二、Spring Event 同步使用

Spring Event(Application Event)其实就是一个观察者设计模式,一个 Bean 处理完成任务后希望通知其它 Bean 或者说一个 Bean 想观察监听另一个Bean 的行为。

2.1 自定义事件

定义事件,继承 ApplicationEvent 的类成为一个事件类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Setter
@Getter
@ToString
public class OrderProductEvent extends ApplicationEvent {

/** 该类型事件携带的信息 */
private String orderId;

public OrderProductEvent(Object source, String orderId) {
super(source);
this.orderId = orderId;
}

}

2.2 定义监听器

监听并处理事件,实现 ApplicationListener 接口或者使用 @EventListener 注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Slf4j
@Component
public class OrderProductListener implements ApplicationListener<OrderProductEvent> {

/** 使用 onApplicationEvent 方法对消息进行接收处理 */
@SneakyThrows
@Override
public void onApplicationEvent(OrderProductEvent event) {
String orderId = event.getOrderId();
long start = System.currentTimeMillis();
Thread.sleep(2000);
long end = System.currentTimeMillis();
log.info("{}:校验订单商品价格耗时:({})毫秒", orderId, (end - start));
}

}

2.3 定义发布者

发布事件,通过 ApplicationEventPublisher 发布事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderService {

/** 注入ApplicationContext用来发布事件 */
private final ApplicationContext applicationContext;

/**
* 下单
*
* @param orderId 订单ID
*/
public String buyOrder(String orderId) {
long start = System.currentTimeMillis();
// 1.查询订单详情

// 2.检验订单价格 (同步处理)
applicationContext.publishEvent(new OrderProductEvent(this, orderId));

// 3.短信通知(异步处理)

long end = System.currentTimeMillis();
log.info("任务全部完成,总耗时:({})毫秒", end - start);
return "购买成功";
}

}

2.4 单测执行

1
2
3
4
5
6
7
8
9
10
11
12
@SpringBootTest
class EventApplicationTests {

@Autowired
private OrderService orderService;

@Test
void buyOrder() {
orderService.buyOrder("982222");
}

}

执行结果如下:

三、Spring Event 异步使用

有些业务场景不需要在一次请求中同步完成,比如邮件发送、短信发送等。

3.1 自定义事件

1
2
3
4
5
6
7
8
@Data
@AllArgsConstructor
public class MsgEvent {

/** 该类型事件携带的信息 */
public String orderId;

}

3.2 定义监听器

推荐使用 @EventListener 注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Slf4j
@Component
public class MsgListener {

@SneakyThrows
@EventListener(MsgEvent.class)
public void sendMsg(MsgEvent event) {
String orderId = event.getOrderId();
long start = System.currentTimeMillis();
log.info("开发发送短信");
log.info("开发发送邮件");
Thread.sleep(4000);
long end = System.currentTimeMillis();
log.info("{}:发送短信、邮件耗时:({})毫秒", orderId, (end - start));
}

}

3.3 定义发布者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderService {

/** 注入ApplicationContext用来发布事件 */
private final ApplicationContext applicationContext;

/**
* 下单
*
* @param orderId 订单ID
*/
public String buyOrderAndMsg(String orderId) {
long start = System.currentTimeMillis();
// 1.查询订单详情

// 2.检验订单价格 (同步处理)
applicationContext.publishEvent(new OrderProductEvent(this, orderId));

// 3.短信通知(异步处理)
applicationContext.publishEvent(new MsgEvent(orderId));

long end = System.currentTimeMillis();
log.info("任务全部完成,总耗时:({})毫秒", end - start);
return "购买成功";
}

}

3.4 单测执行(同步)

1
2
3
4
5
6
7
8
9
10
11
12
@SpringBootTest
class EventApplicationTests {

@Autowired
private OrderService orderService;

@Test
void buyOrderAndMsg() {
orderService.buyOrderAndMsg("982222");
}

}

执行结果如下:

3.5 开启异步

启动类增加 @EnableAsync 注解

1
2
3
4
5
6
7
8
9
@EnableAsync
@SpringBootApplication
public class EventApplication {

public static void main(String[] args) {
SpringApplication.run(EventApplication.class, args);
}

}

Listener 类需要开启异步的方法增加 @Async 注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Async
@Slf4j
@Component
public class MsgListener {

@SneakyThrows
@EventListener(MsgEvent.class)
public void sendMsg(MsgEvent event) {
String orderId = event.getOrderId();
long start = System.currentTimeMillis();
log.info("开发发送短信");
log.info("开发发送邮件");
Thread.sleep(4000);
long end = System.currentTimeMillis();
log.info("{}:发送短信、邮件耗时:({})毫秒", orderId, (end - start));
}

}

3.6 单测执行(异步)

发送短信的线程显示 task-1,主线程结束后(总耗时:(2019)毫秒)控制台停止打印了