Class DeadLetterQueue<K>
java.lang.Object
cloud.opencode.base.cache.dlq.DeadLetterQueue<K>
- Type Parameters:
K- key type | 键类型Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Yes - 空值安全: 是
- All Implemented Interfaces:
AutoCloseable
Dead Letter Queue - Tracks failed cache load operations
死信队列 - 跟踪失败的缓存加载操作
Manages keys that failed to load, with support for retry scheduling, exponential backoff, and failure analysis.
管理加载失败的键,支持重试调度、指数退避和故障分析。
Features | 主要功能:
- Failed load tracking - 失败加载跟踪
- Automatic retry with backoff - 带退避的自动重试
- Failure pattern analysis - 故障模式分析
- Manual drain operations - 手动排空操作
- Alerting integration - 告警集成
Usage Examples | 使用示例:
// Create DLQ
DeadLetterQueue<String> dlq = DeadLetterQueue.<String>builder()
.maxRetries(3)
.initialBackoff(Duration.ofSeconds(1))
.maxBackoff(Duration.ofMinutes(5))
.retryLoader(key -> loadFromBackend(key))
.build();
// Add failed key
dlq.add("user:1001", new IOException("Connection refused"));
// Process retries automatically
dlq.startRetryProcessor();
// Or drain manually
List<FailedEntry<String>> failed = dlq.drain(10);
// Get failure analysis
DlqAnalysis analysis = dlq.analyze();
- Since:
- JDK 25, opencode-base-cache V2.0.5
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for DeadLetterQueue DeadLetterQueue 构建器static enumDiscard reason enumeration 丢弃原因枚举static final recordDLQ analysis result DLQ 分析结果static interfaceDLQ event handler DLQ 事件处理器static final recordDLQ statistics DLQ 统计static classFailed entry record 失败条目记录 -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdd a failed key to the DLQ 将失败的键添加到 DLQvoidAdd multiple failed keys 添加多个失败的键analyze()Analyze DLQ contents 分析 DLQ 内容static <K> DeadLetterQueue.Builder<K> builder()Create a builder 创建构建器voidclose()booleanCheck if key is in DLQ 检查键是否在 DLQ 中drain(int maxEntries) Drain up to N entries from the DLQ 从 DLQ 排空最多 N 个条目drainAll()Drain all entries 排空所有条目drainMatching(Predicate<DeadLetterQueue.FailedEntry<K>> predicate) Drain entries matching predicate 排空匹配谓词的条目Get failed entry for key 获取键的失败条目Get all failed keys 获取所有失败的键getStats()Get DLQ statistics 获取 DLQ 统计booleanisEmpty()Check if DLQ is empty 检查 DLQ 是否为空intProcess pending retries 处理待处理的重试booleanRetry a single failed entry 重试单个失败的条目intsize()Get current queue size 获取当前队列大小voidStart automatic retry processor 启动自动重试处理器voidStop automatic retry processor 停止自动重试处理器
-
Method Details
-
builder
Create a builder 创建构建器- Type Parameters:
K- key type | 键类型- Returns:
- builder | 构建器
-
add
-
addAll
-
startRetryProcessor
public void startRetryProcessor()Start automatic retry processor 启动自动重试处理器 -
stopRetryProcessor
public void stopRetryProcessor()Stop automatic retry processor 停止自动重试处理器 -
processRetries
public int processRetries()Process pending retries 处理待处理的重试- Returns:
- number of entries processed | 处理的条目数
-
retry
Retry a single failed entry 重试单个失败的条目- Parameters:
key- the key to retry | 要重试的键- Returns:
- true if recovered, false otherwise | 如果恢复返回 true,否则返回 false
-
drain
Drain up to N entries from the DLQ 从 DLQ 排空最多 N 个条目- Parameters:
maxEntries- maximum entries to drain | 要排空的最大条目数- Returns:
- list of drained entries | 排空的条目列表
-
drainAll
Drain all entries 排空所有条目- Returns:
- list of all entries | 所有条目列表
-
drainMatching
public List<DeadLetterQueue.FailedEntry<K>> drainMatching(Predicate<DeadLetterQueue.FailedEntry<K>> predicate) Drain entries matching predicate 排空匹配谓词的条目- Parameters:
predicate- filter predicate | 过滤谓词- Returns:
- list of matching entries | 匹配条目列表
-
contains
Check if key is in DLQ 检查键是否在 DLQ 中- Parameters:
key- the key | 键- Returns:
- true if in DLQ | 如果在 DLQ 中返回 true
-
get
Get failed entry for key 获取键的失败条目- Parameters:
key- the key | 键- Returns:
- failed entry or null | 失败条目或 null
-
getFailedKeys
-
size
public int size()Get current queue size 获取当前队列大小- Returns:
- queue size | 队列大小
-
isEmpty
public boolean isEmpty()Check if DLQ is empty 检查 DLQ 是否为空- Returns:
- true if empty | 如果为空返回 true
-
analyze
Analyze DLQ contents 分析 DLQ 内容- Returns:
- analysis result | 分析结果
-
getStats
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-