5. Spring Boot 异步任务与事件驱动(springboot异步不生效)

5. Spring Boot 异步任务与事件驱动(springboot异步不生效)

@Configuration

@EnableAsync

public class AsyncConfig {

@Bean(name = "taskExecutor")

public Executor taskExecutor() {

ThreadPoolTaskExecutor executor = new 6n.oOoo10.MoBI157ThreadPoolTaskExecutor();

executor.setCorePoolSize(5);

executor.setMaxPoolSize(10);

executor.setQueueCapacity(25);

executor.setThreadNamePrefix("Async-");

executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

executor.initialize();

return executor;

}

}

@Service

@Slf4j

public class NotificationService {

private final JavaMailSender mailSender;

private final ApplicationEventPublisher eventPublisher;

public NotificationService(JavaMailSender mailSender, ApplicationEventPublisher eventPublisher) {

this.mailSender = 6ei.oOoo4.MoBI161mailSender;

this.eventPublisher = eventPublisher;

}

@Async("taskExecutor")

public CompletableFuture<Void> sendOrderConfirmationEmail(Order order) {

try {

// 构建邮件内容

MimeMessage message = mailSender.createMimeMessage();

MimeMessageHelper helper = new MimeMessageHelper(message, true);

helper.setTo(order.getUserEmail());

helper.setSubject("订单确认通知");

helper.setText(buildOrderEmailContent(order), true);

mailSender.send(message);

log.info("订单 {} 确认邮件发送成功", order.getId());

// 发布事件

eventPublisher.publishEvent(new EmailSentEvent(this, order.getId(), order.getUserEmail()));

return CompletableFuture. 7q.oOoo5.MoBI162completedFuture(null);

} catch (Exception e) {

log.error("订单 {} 邮件发送失败", order.getId(), e);

return CompletableFuture.failedFuture(e);

}

}

private String buildOrderEmailContent(Order order) {

// 构建邮件内容逻辑

return "";

}

}

// 事件监听

@Component

public class EmailEventListened {

@EventListener

public void handleEmailSentEvent(EmailSentEvent event) {

// 记录邮件发送日志到数据库

log.info("邮件发送事件处理: 订单={}, 邮箱={}", event.getOrderId(), event.getEmail());

}

}

// 自定义事件

public class EmailSentEvent extends ApplicationEvent {

private final Long orderId;

private final String email;

public EmailSentEvent(Object source, Long orderId, String email) {

super(source);

this.orderId = orderId;

this.email = email;

}

// getters

}

特别声明:[5. Spring Boot 异步任务与事件驱动(springboot异步不生效)] 该文观点仅代表作者本人,今日霍州系信息发布平台,霍州网仅提供信息存储空间服务。

猜你喜欢

黑盒VS白盒,谁才是系统集成的主流?(黑盒和白盒怎么结合提升效率)

如果企业需要快速部署系统,且对库存透明度和精细化管理的要求不高,黑盒模式是一个不错的选择。如果企业对库存管理的精细化程度要求较高,且愿意投入更多的时间和资源进行系统实施和维护,白盒模式则更具优势。黑盒模式适用…

黑盒VS白盒,谁才是系统集成的主流?(黑盒和白盒怎么结合提升效率)

手机厂一年就倒闭 摩托前高管:美国人不爱打螺丝(手机厂一年就倒闭怎么办)

8月10日消息,近年来美国上下推动再工业化,实现“美国制造”的政策影响了全球经济,但是让美国人进工厂打螺丝并不容易,之前摩托罗拉在美国建了手机工厂,结果一年之后就不得不关闭了。前摩托罗拉CEO Dennis…

手机厂一年就倒闭 摩托前高管:美国人不爱打螺丝(手机厂一年就倒闭怎么办)

定居泰国、豪车豪宅、高调炫富!这次张国立也难管无法无天的张默(定居泰国好吗)

张国立自然以为,只要给儿子提供充足的资源和爱,他的人生就会一帆风顺,然而他忽略了一个事实:人生的道路最终需要张默自己走,而不可能永远依靠父亲的资源。此次,公众对张国立的同情已基本耗尽,法律也没有给予张默任何宽…

定居泰国、豪车豪宅、高调炫富!这次张国立也难管无法无天的张默(定居泰国好吗)

最高9.5分,HBO这5部台柱剧,值得通宵去看(hbo高分神剧排行)

《企鹅人》不仅仅是一部犯罪剧,更是一部讲述小人物如何在绝望中崛起的史诗之作。剧集将视角聚焦于匹兹堡一家大型医院的急诊室,展现了一群平凡医护人员在生死考验中与时间赛跑的日常。这部剧已经获得续订,将于明年初迎来第…

最高9.5分,HBO这5部台柱剧,值得通宵去看(hbo高分神剧排行)

团体标准丨水洗羽绒羽毛(净绒)行情(2025.8.11)Price of washed down and feather(团体标准体系)

95% 10.22 1941.80 90% 5.98 1076.17 85% 5.37 912.14 80% 5.11 817.60 灰鹅绒1767.00 90% 5.39 970.92 85% 4.…

团体标准丨水洗羽绒羽毛(净绒)行情(2025.8.11)Price of washed down and feather(团体标准体系)