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异步不生效)] 该文观点仅代表作者本人,今日霍州系信息发布平台,霍州网仅提供信息存储空间服务。

猜你喜欢

中国移动累计有效专利达1.8万项,连续三年全球第一(中国移动流量累积吗)

这些专利广泛覆盖4G、5G、大数据等领域,其中86%为国家高价值专利。在1.8万项有效专利中,有36项获中国专利奖。在5G领域就有2000余项授权专利,支撑我国建成全球最早、规模最大、技术最先进的5G独立组网…

中国移动累计有效专利达1.8万项,连续三年全球第一(中国移动流量累积吗)

冲上热搜!用微信收付款的要当心,这个功能一定要开启!(上微博热搜对有什么影响)

第一步:进入“我-钱包👛-服务”,选择“客服中心”。 可以看到有两种解锁方式,一个是“指纹解锁”,另一个是“手势密码解锁”,根据自己的使用习惯选择一种解锁方式,验证身份并设置好解锁方式后,安全锁功能就完成开…

冲上热搜!用微信收付款的要当心,这个功能一定要开启!(上微博热搜对有什么影响)

攀枝花共享陪护床投放(攀枝花疗养院怎么收费)

对于病人家属来说,这种服务提供了一种相对便捷、规范的陪护休息解决方案,在一定程度上缓解了陪护过程中的实际困难。 正如我们看到的,立式共享陪护床通过其特定的设计和运营模式,试图在有限的空间和资源条件下,为有需要…

攀枝花共享陪护床投放(攀枝花疗养院怎么收费)

探索大疆Osmo Pocket3的秘密拓展工具(大疆osmoaction)

从单机拍摄到多功能『直播间』,这些神奇配件是如何将大疆Osmo Pocket3打造成全方位拍摄神器的?本文深入解读口袋相机📷️背后的强大扩展体系,帮助您解锁更多创作可能。

探索大疆Osmo Pocket3的秘密拓展工具(大疆osmoaction)

女子打车不给过路费司机送回起点 30元高速费引发争执(女子打车不给过户怎么办)

一名女乘客在打车时拒绝支付30元的高速费,导致与司机发生争执。司机提出自掏腰包将她送回上车点,女子最终选择报警。随着数字支付的普及,人们已经习惯了平台包办一切,点击支付后便万事大吉

女子打车不给过路费司机送回起点 30元高速费引发争执(女子打车不给过户怎么办)