Class AsyncEmailSender
java.lang.Object
cloud.opencode.base.email.sender.AsyncEmailSender
- All Implemented Interfaces:
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for AsyncEmailSender AsyncEmailSender构建器 -
Constructor Summary
ConstructorsConstructorDescriptionAsyncEmailSender(cloud.opencode.base.email.internal.EmailSender delegate) Create async sender with delegate sender 使用代理发送器创建异步发送器AsyncEmailSender(cloud.opencode.base.email.internal.EmailSender delegate, EmailRetryExecutor retryExecutor) Create async sender with delegate and retry executor 使用代理发送器和重试执行器创建异步发送器AsyncEmailSender(cloud.opencode.base.email.internal.EmailSender delegate, ExecutorService executor, EmailRetryExecutor retryExecutor, boolean ownsExecutor) Create async sender with all parameters 使用所有参数创建异步发送器 -
Method Summary
Modifier and TypeMethodDescriptionstatic AsyncEmailSender.Builderbuilder()Create a builder 创建构建器voidclose()cloud.opencode.base.email.internal.EmailSenderGet delegate sender 获取代理发送器Get executor service 获取执行器服务voidsendAllAndWait(List<Email> emails) Send multiple emails and wait for all to complete 发送多封邮件并等待全部完成sendAllAsync(List<Email> emails) Send multiple emails asynchronously 异步发送多封邮件Send email asynchronously 异步发送邮件sendAsync(Email email, BiConsumer<Email, Throwable> callback) Send email asynchronously with callback 使用回调异步发送邮件default SendResultsendWithResult(Email email) Send an email and return the result with message ID 发送邮件并返回包含消息ID的结果
-
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
-
sendAsync
Send email asynchronously 异步发送邮件- Parameters:
email- the email to send | 要发送的邮件- Returns:
- future that completes when sent | 发送完成时的future
-
sendAsync
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
Send multiple emails asynchronously 异步发送多封邮件- Parameters:
emails- the emails to send | 要发送的邮件列表- Returns:
- list of futures | future列表
-
sendAllAndWait
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:
closein interfaceAutoCloseable
-
getDelegate
public cloud.opencode.base.email.internal.EmailSender getDelegate()Get delegate sender 获取代理发送器- Returns:
- the delegate sender | 代理发送器
-
getExecutor
-
builder
-
sendWithResult
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 | 发送失败时抛出
-