写了个日志异步写入,但是返现监听器里面执行完主线程才会返回
监听器
@Component
public class ApiLogListener {
private static final Logger log = LoggerFactory.getLogger(ApiLogListener.class);
@Async
@Order
@EventListener({ApiLogEvent.class})
public void saveLog(ApiLogEvent event) throws InterruptedException {
Map<String, LogApi> map = (Map<String, LogApi>) event.getSource();
LogApi logApi = map.get("log");
System.out.println(logApi);
}
}
发送是个工具类
@Slf4j
@Component
public class SpringUtil implements ApplicationContextAware {
@Autowired
private static ApplicationContext context;
@Override
public void setApplicationContext(@Nullable ApplicationContext context) throws BeansException {
SpringUtil.context = context;
}
/**
* 发布事件
*
* @param event 事件
*/
public static void publishEvent(ApplicationEvent event) {
if (context == null) {
return;
}
try {
context.publishEvent(event);
} catch (Exception ex) {
log.error(ex.getMessage());
}
}
}
我想的是 publishEvent 完就会继续往下走,但是现在需要在 saveLog 结束了才会继续
1
hexiaowu1993 2021-04-25 15:28:03 +08:00 1
你是不是没有开启异步。
@EnabledAsync 开启下,另外,@Async 默认的异步只有一个线程,你尽量自己指定下线程池 |