Cause Redisson Redis Exception And Interrupted Exception Reason

Cause Redisson Redis Exception And Interrupted Exception Reason

這篇介紹Cause Redisson Redis Exception And Interrupted Exception Reason。

Exception內容

[2023/11/21 10:23:43.356][xxx.java:186][ERROR][ForkJoinPool-2-worker-1]
java.lang.InterruptedException
org.redisson.client.RedisException: java.lang.InterruptedException
at org.redisson.command.CommandAsyncService.get(CommandAsyncService.java:114)
~[redisson-3.23.2.jar:3.23.2]
Caused by: java.lang.InterruptedException
at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:386)
~[?:?]
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2073)
~[?:?]
at org.redisson.command.CommandAsyncService.get(CommandAsyncService.java:110)
~[redisson-3.23.2.jar:3.23.2]
… 14 more

程式碼片段

new ForkJoinPool(1).execute(() -> {
try {
TimeUnit.MILLISECONDS.sleep(10000);
} catch (InterruptedException e) {
LogUtils.system.error(e.getMessage(), e);
}
SubscriberType.xxx.publish(“test”);
});

以上publish時常會出現錯誤,出現機率不一定。原本以為是Redisson Bug~
查到最後才發現new ForkJoinPool(1)寫法有可能執行緒中斷而讓執行緒內的程式碼片段異常。

改善後寫法

ForkJoinPool.commonPool().execute(() -> {
try {
TimeUnit.MILLISECONDS.sleep(10000);
} catch (InterruptedException e) {
LogUtils.system.error(e.getMessage(), e);
}
SubscriberType.xxx.publish(“test”);
});

使用ForkJoinPool.commonPool()確保執行緒內任務不中斷!