写在最前
实际业务开发过程中,业务逻辑可能非常复杂,核心业务 + N个子业务。如果都放到一块儿去做,代码可能会很长,耦合度不断攀升,维护起来也麻烦,甚至头疼。还有一些业务场景不需要在一次请求中同步完成,比如邮件发送、短信发送等。
MQ 确实可以解决这个问题,但 MQ 重啊,非必要不提升架构复杂度。针对这些问题,我们了解一下 Spring Event
ApplicationEvent - 一般
Spring Event(Application Event
)其实就是一个观察者设计模式,一个 Bean 处理完成任务后希望通知其它 Bean 或者说一个 Bean 想观察监听另一个Bean 的行为。
Spring Event 用来解耦业务真的贼好用!
1.自定义事件
定义事件,继承 ApplicationEvent
的类成为一个事件类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import lombok.Data; import lombok.ToString; import org.springframework.context.ApplicationEvent;
@Data @ToString public class OrderProductEvent extends ApplicationEvent {
private String orderId;
public OrderProductEvent(Object source, String orderId) { super(source); this.orderId = orderId; } }
|
2.定义监听器
监听并处理事件,实现 ApplicationListener
接口或者使用 @EventListener
注解
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
| import com.csp.mingyue.event.events.OrderProductEvent; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component;
@Slf4j @Component public class OrderProductListener implements ApplicationListener<OrderProductEvent> {
@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)); } }
|
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 29 30 31 32 33 34 35 36
| import com.csp.mingyue.event.events.OrderProductEvent; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Service;
@Slf4j @Service @RequiredArgsConstructor public class OrderService {
private final ApplicationContext applicationContext;
public String buyOrder(String orderId) { long start = System.currentTimeMillis();
applicationContext.publishEvent(new OrderProductEvent(this, orderId));
long end = System.currentTimeMillis(); log.info("任务全部完成,总耗时:({})毫秒", end - start); return "购买成功"; } }
|
4.单测执行(同步)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest public class OrderServiceTest { @Autowired private OrderService orderService;
@Test public void buyOrderTest() { orderService.buyOrder("732171109"); } }
|
执行结果如下:
1 2
| 2022-04-24 10:13:17.535 INFO 44272 --- [ main] c.c.m.e.listener.OrderProductListener : 732171109:校验订单商品价格耗时:(2008)毫秒 2022-04-24 10:13:17.536 INFO 44272 --- [ main] c.c.mingyue.event.service.OrderService : 任务全部完成,总耗时:(2009)毫秒
|
@EventListener - 推荐
有些业务场景不需要在一次请求中同步完成,比如邮件发送、短信发送等。
1.定义事件类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import lombok.AllArgsConstructor; import lombok.Data;
@Data @AllArgsConstructor public class MsgEvent {
public String orderId; }
|
2.定义监听器
- 推荐使用
@EventListener
注解
- 支持定义多个监听器【例如多个业务后台都需要监听】,监听同一个事件
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
| import com.csp.mingyue.event.events.MsgEvent; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component;
@Slf4j @Component public class MsgListener {
@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.定义发布者
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
public String buyOrder(String orderId) { long start = System.currentTimeMillis(); applicationContext.publishEvent(new MsgEvent(orderId));
long end = System.currentTimeMillis(); log.info("任务全部完成,总耗时:({})毫秒", end - start); return "购买成功"; }
|
4.单测执行(同步)
1 2 3 4
| @Test public void buyOrderTest() { orderService.buyOrder("732171109"); }
|
执行结果如下:
1 2 3 4 5
| 2022-04-24 10:24:13.905 INFO 54848 --- [ main] c.c.m.e.listener.OrderProductListener : 732171109:校验订单商品价格耗时:(2004)毫秒 2022-04-24 10:24:13.906 INFO 54848 --- [ main] c.c.mingyue.event.listener.MsgListener : 开发发送短信 2022-04-24 10:24:13.907 INFO 54848 --- [ main] c.c.mingyue.event.listener.MsgListener : 开发发送邮件 2022-04-24 10:24:17.908 INFO 54848 --- [ main] c.c.mingyue.event.listener.MsgListener : 732171109:发送短信、邮件耗时:(4002)毫秒 2022-04-24 10:24:17.908 INFO 54848 --- [ main] c.c.mingyue.event.service.OrderService : 任务全部完成,总耗时:(6008)毫秒
|
@Async 异步执行
1. 启动类增加 @EnableAsync
注解
1 2 3 4 5 6 7 8
| @EnableAsync @SpringBootApplication public class MingYueSpringBootEventApplication {
public static void main(String[] args) { SpringApplication.run(MingYueSpringBootEventApplication.class, args); } }
|
2. Listener
类需要开启异步的方法增加 @Async
注解
1 2 3 4 5 6 7 8 9 10 11
| @Async @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. 单测执行(异步)
发送短信的线程显示 task-1
,主线程结束后(总耗时:(2017)毫秒)控制台停止打印了
1 2 3 4
| 2022-04-24 10:30:59.002 INFO 59448 --- [ main] c.c.m.e.listener.OrderProductListener : 732171109:校验订单商品价格耗时:(2009)毫秒 2022-04-24 10:30:59.009 INFO 59448 --- [ main] c.c.mingyue.event.service.OrderService : 任务全部完成,总耗时:(2017)毫秒 2022-04-24 10:30:59.028 INFO 59448 --- [ task-1] c.c.mingyue.event.listener.MsgListener : 开发发送短信 2022-04-24 10:30:59.028 INFO 59448 --- [ task-1] c.c.mingyue.event.listener.MsgListener : 开发发送邮件
|