Class AsyncEmailReceiver

java.lang.Object
cloud.opencode.base.email.receiver.AsyncEmailReceiver
All Implemented Interfaces:
AutoCloseable

public class AsyncEmailReceiver extends Object implements AutoCloseable
Async Email Receiver Wrapper 异步邮件接收器包装

Wraps any EmailReceiver to provide async operations.

包装任何EmailReceiver以提供异步操作。

Features | 主要功能:

  • Async receive with CompletableFuture - 使用CompletableFuture的异步接收
  • Virtual thread support (JDK 21+) - 虚拟线程支持(JDK 21+)
  • Callback support - 回调支持
  • Builder pattern - 构建器模式

Usage Examples | 使用示例:

// Create async receiver with builder
AsyncEmailReceiver asyncReceiver = AsyncEmailReceiver.builder()
    .config(config)
    .build();

// Async receive
asyncReceiver.receiveAsync(query)
    .thenAccept(emails -> processEmails(emails))
    .exceptionally(e -> { log.error("Failed", e); return null; });

// Async receive with callback
asyncReceiver.receiveAsync(query, (emails, error) -> {
    if (error != null) {
        log.error("Failed", error);
    } else {
        processEmails(emails);
    }
});

Security | 安全性:

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

    • AsyncEmailReceiver

      public AsyncEmailReceiver(cloud.opencode.base.email.internal.EmailReceiver delegate)
      Create async receiver with delegate 使用委托创建异步接收器
      Parameters:
      delegate - the underlying receiver | 底层接收器
  • Method Details

    • builder

      public static AsyncEmailReceiver.Builder builder()
      Create a new builder 创建新的构建器
      Returns:
      the builder | 构建器
    • receiveUnreadAsync

      public CompletableFuture<List<ReceivedEmail>> receiveUnreadAsync()
      Receive unread emails asynchronously 异步接收未读邮件
      Returns:
      future containing received emails | 包含接收邮件的future
    • receiveAsync

      public CompletableFuture<List<ReceivedEmail>> receiveAsync(EmailQuery query)
      Receive emails asynchronously with query 使用查询异步接收邮件
      Parameters:
      query - the email query | 邮件查询
      Returns:
      future containing received emails | 包含接收邮件的future
    • receiveAsync

      Receive emails asynchronously with callback 使用回调异步接收邮件
      Parameters:
      query - the email query | 邮件查询
      callback - callback on completion | 完成时的回调
      Returns:
      future containing received emails | 包含接收邮件的future
    • receiveAllAsync

      public CompletableFuture<List<ReceivedEmail>> receiveAllAsync()
      Receive all emails asynchronously 异步接收所有邮件
      Returns:
      future containing received emails | 包含接收邮件的future
    • receiveByIdAsync

      public CompletableFuture<ReceivedEmail> receiveByIdAsync(String messageId)
      Receive email by ID asynchronously 异步通过ID接收邮件
      Parameters:
      messageId - the message ID | 消息ID
      Returns:
      future containing received email or null | 包含接收邮件或null的future
    • markAsReadAsync

      public CompletableFuture<Void> markAsReadAsync(String messageId)
      Mark email as read asynchronously 异步标记邮件为已读
      Parameters:
      messageId - the message ID | 消息ID
      Returns:
      future that completes when done | 完成时的future
    • markAsUnreadAsync

      public CompletableFuture<Void> markAsUnreadAsync(String messageId)
      Mark email as unread asynchronously 异步标记邮件为未读
      Parameters:
      messageId - the message ID | 消息ID
      Returns:
      future that completes when done | 完成时的future
    • setFlaggedAsync

      public CompletableFuture<Void> setFlaggedAsync(String messageId, boolean flagged)
      Set email flagged status asynchronously 异步设置邮件标记状态
      Parameters:
      messageId - the message ID | 消息ID
      flagged - true to flag, false to unflag | true为标记,false为取消标记
      Returns:
      future that completes when done | 完成时的future
    • deleteAsync

      public CompletableFuture<Void> deleteAsync(String messageId)
      Delete email asynchronously 异步删除邮件
      Parameters:
      messageId - the message ID | 消息ID
      Returns:
      future that completes when done | 完成时的future
    • moveToFolderAsync

      public CompletableFuture<Void> moveToFolderAsync(String messageId, String targetFolder)
      Move email to folder asynchronously 异步移动邮件到文件夹
      Parameters:
      messageId - the message ID | 消息ID
      targetFolder - the target folder | 目标文件夹
      Returns:
      future that completes when done | 完成时的future
    • listFoldersAsync

      public CompletableFuture<List<String>> listFoldersAsync()
      List folders asynchronously 异步列出文件夹
      Returns:
      future containing folder names | 包含文件夹名称的future
    • getMessageCountAsync

      public CompletableFuture<Integer> getMessageCountAsync(String folder)
      Get message count asynchronously 异步获取消息数量
      Parameters:
      folder - the folder name | 文件夹名称
      Returns:
      future containing message count | 包含消息数量的future
    • getUnreadCountAsync

      public CompletableFuture<Integer> getUnreadCountAsync(String folder)
      Get unread count asynchronously 异步获取未读数量
      Parameters:
      folder - the folder name | 文件夹名称
      Returns:
      future containing unread count | 包含未读数量的future
    • connect

      public void connect()
      Connect to mail server 连接到邮件服务器
    • connectAsync

      public CompletableFuture<Void> connectAsync()
      Connect to mail server asynchronously 异步连接到邮件服务器
      Returns:
      future that completes when connected | 连接完成时的future
    • disconnect

      public void disconnect()
      Disconnect from mail server 断开与邮件服务器的连接
    • isConnected

      public boolean isConnected()
      Check if connected 检查是否已连接
      Returns:
      true if connected | 已连接返回true
    • getDelegate

      public cloud.opencode.base.email.internal.EmailReceiver getDelegate()
      Get the underlying receiver 获取底层接收器
      Returns:
      the delegate receiver | 委托接收器
    • close

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