Class AsyncEmailSender

java.lang.Object
cloud.opencode.base.email.sender.AsyncEmailSender
All Implemented Interfaces:
AutoCloseable

public class AsyncEmailSender extends Object implements AutoCloseable
Async Email Sender 异步邮件发送器

Email sender with async support using virtual threads.

使用虚拟线程的异步邮件发送器。

Features | 主要功能:

  • Virtual threads (JDK 21+) - 虚拟线程
  • CompletableFuture support - CompletableFuture支持
  • Built-in retry mechanism - 内置重试机制
  • Configurable thread pool - 可配置线程池
  • Batch sending support - 批量发送支持

Usage Examples | 使用示例:

AsyncEmailSender sender = AsyncEmailSender.builder()
    .sender(new SmtpEmailSender(config))
    .build();

// Single async send
CompletableFuture<Void> future = sender.sendAsync(email);
future.thenRun(() -> log.info("Sent successfully"));

// Batch async send
List<CompletableFuture<Void>> futures = sender.sendAllAsync(emails);
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
Since:
JDK 25, opencode-base-email V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • AsyncEmailSender

      public AsyncEmailSender(cloud.opencode.base.email.internal.EmailSender delegate)
      Create async sender with delegate sender 使用代理发送器创建异步发送器
      Parameters:
      delegate - the delegate sender | 代理发送器
    • AsyncEmailSender

      public AsyncEmailSender(cloud.opencode.base.email.internal.EmailSender delegate, EmailRetryExecutor retryExecutor)
      Create async sender with delegate and retry executor 使用代理发送器和重试执行器创建异步发送器
      Parameters:
      delegate - the delegate sender | 代理发送器
      retryExecutor - the retry executor | 重试执行器
    • AsyncEmailSender

      public AsyncEmailSender(cloud.opencode.base.email.internal.EmailSender delegate, ExecutorService executor, EmailRetryExecutor retryExecutor, boolean ownsExecutor)
      Create async sender with all parameters 使用所有参数创建异步发送器
      Parameters:
      delegate - the delegate sender | 代理发送器
      executor - the executor service | 执行器服务
      retryExecutor - the retry executor | 重试执行器
      ownsExecutor - whether to shutdown executor on close | 关闭时是否关闭执行器
  • Method Details

    • send

      public void send(Email email)
    • sendAsync

      public CompletableFuture<Void> sendAsync(Email email)
      Send email asynchronously 异步发送邮件
      Parameters:
      email - the email to send | 要发送的邮件
      Returns:
      future that completes when sent | 发送完成时的future
    • sendAsync

      public CompletableFuture<Void> sendAsync(Email email, BiConsumer<Email, Throwable> callback)
      Send email asynchronously with callback 使用回调异步发送邮件
      Parameters:
      email - the email to send | 要发送的邮件
      callback - callback on completion (email, exception or null) | 完成时的回调
      Returns:
      future that completes when sent | 发送完成时的future
    • sendAllAsync

      public List<CompletableFuture<Void>> sendAllAsync(List<Email> emails)
      Send multiple emails asynchronously 异步发送多封邮件
      Parameters:
      emails - the emails to send | 要发送的邮件列表
      Returns:
      list of futures | future列表
    • sendAllAndWait

      public CompletableFuture<Void> sendAllAndWait(List<Email> emails)
      Send multiple emails and wait for all to complete 发送多封邮件并等待全部完成
      Parameters:
      emails - the emails to send | 要发送的邮件列表
      Returns:
      future that completes when all sent | 全部发送完成时的future
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • getDelegate

      public cloud.opencode.base.email.internal.EmailSender getDelegate()
      Get delegate sender 获取代理发送器
      Returns:
      the delegate sender | 代理发送器
    • getExecutor

      public ExecutorService getExecutor()
      Get executor service 获取执行器服务
      Returns:
      the executor | 执行器
    • builder

      public static AsyncEmailSender.Builder builder()
      Create a builder 创建构建器
      Returns:
      the builder | 构建器
    • sendWithResult

      default SendResult sendWithResult(Email email)
      Send an email and return the result with message ID 发送邮件并返回包含消息ID的结果
      Parameters:
      email - the email to send | 要发送的邮件
      Returns:
      the send result containing message ID | 包含消息ID的发送结果
      Throws:
      EmailException - if sending fails | 发送失败时抛出