在实际业务开发中,有时候复杂性的业务之间需要解耦,常用的方法:同步、异步、MQ。但 MQ 重啊,非必要不提升架构复杂度。
针对同步和异步使用方式:1.定时器 2.Spring Event.
Spring Event:
观察者设计模式,一个 Bean 处理完成任务后希望通知其它 Bean 或者说一个 Bean监听另一个Bean 的行为。
配置异步线程池
package com.lean.event.demo.config;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;@Configuration
public class AsynConfig implements AsyncConfigurer {private static final Logger log= LoggerFactory.getLogger(AsynConfig.class);@Bean("taskAsyncPool")public Executor orderTaskAsyncPool() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(10); // 核心线程数executor.setMaxPoolSize(20); // 最大线程数executor.setQueueCapacity(1000); // 队列大小executor.setKeepAliveSeconds(300); // 线程最大空闲时间executor.setThreadNamePrefix("async-Executor-"); // 指定用于新创建的线程名称的前缀。executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略(一共四种,此处省略)executor.initialize();return executor;}@Overridepublic Executor getAsyncExecutor() {return orderTaskAsyncPool();}// 异常处理器@Overridepublic AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {return (ex, method, params) -> log.error(String.format("执行异步任务'%s'", method), ex);}
}
1.同步
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.context.ApplicationEvent;
/*** 自定义事件 继承ApplicationEvent*/
@Getter
@Setter
@ToString
public class MessageEvent extends ApplicationEvent {//传输的数据对象private String messageId;public MessageEvent(Object source, String messageId) {super(source);this.messageId = messageId;}
}
package com.lean.event.demo.listener;import com.lean.event.demo.events.MessageEvent;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/*** 同步* 监听器:* 实现方式:* 1.实现ApplicationListener* 2.@EventListener*/
@Slf4j
@Component
public class MessageListener implements ApplicationListener<MessageEvent> {@SneakyThrows@Overridepublic void onApplicationEvent(MessageEvent event) {String messageId = event.getMessageId();long start = System.currentTimeMillis();long end = System.currentTimeMillis();log.info("{}:耗时:({})毫秒", messageId, (end - start));}
}
2.异步
| 自定义事件
import lombok.AllArgsConstructor;
import lombok.Data;@Data
@AllArgsConstructor
public class MsgEvent {/*** 该类型事件携带的信息*/public String messageId;
}
import com.lean.event.demo.events.MsgEvent;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class MsgListener {//异步注解 需要开启@EnableAsync异步@Async(value = "taskAsyncPool")@SneakyThrows@EventListener(MsgEvent.class)public void sendMsg(MsgEvent event) {String messageId = event.getMessageId();long start = System.currentTimeMillis();log.info("开发发送短信");log.info("开发发送邮件");long end = System.currentTimeMillis();log.info("{}:发送短信、邮件耗时:({})毫秒", messageId, (end - start));}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;@EnableAsync
@SpringBootApplication
public class SpringLeanEventApplication {public static void main(String[] args) {SpringApplication.run(SpringLeanEventApplication.class, args);}}
3.测试
package com.lean.event.demo.service;import com.lean.event.demo.events.MsgEvent;
import com.lean.event.demo.events.MessageEvent;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Slf4j
@Service
@Data
public class MessageService {/** 注入ApplicationContext用来发布事件 */private final ApplicationContext applicationContext;public String testMessage(String messageId) {long start = System.currentTimeMillis();// (同步处理)applicationContext.publishEvent(new MessageEvent(this, messageId));// (异步处理)applicationContext.publishEvent(new MsgEvent(messageId));long end = System.currentTimeMillis();log.info("任务全部完成,总耗时:({})毫秒", end - start);return "购买成功";}
}
package com.lean.event.demo;import com.lean.event.demo.service.MessageService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class MessageServiceTest {@Autowiredprivate MessageService messageService;@Testpublic void buyOrderTest() {messageService.testMessage("123456");}
}
ApplicationEvent:
类的注释是,被应用事件继承的类,抽象的不能初始化;
EventObject: 父类
事件携带一个 Objecgt 对象,可以被发布。
事件监听者,监听到这个事件后,触发自定义逻辑 ( 操作 Object 对象);
注意:
事件发布后,都会被监听ApplicationEvent的监听对象所监听到。
代码