CallbackService可用Runnable替代慷嗜,代碼如下:
```java
public class TransactionCommitHandler {
private ExecutorService executor;
private boolean thread;
public TransactionCommitHandler executor(ExecutorService executor) {
this.executor = executor;
return this;
}
public TransactionCommitHandler setThread(boolean thread) {
this.thread = thread;
return this;
}
public boolean supportExecutor(){
return executor != null;
}
public boolean supportThread() {
return thread;
}
/**
* 確保當前事務(wù)提交后 執(zhí)行runnable
*
* @param runnable
*/
public void handle(Runnable runnable) {
// 保證當前線程有活躍的事務(wù)才會執(zhí)行
if (TransactionSynchronizationManager.isActualTransactionActive()) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
if (supportExecutor()) {
// 通過executor執(zhí)行
executor.execute(runnable);
} else if (supportThread()) {
// 通過new thread方式執(zhí)行
new Thread(runnable).start();
} else {
// 通過當前主線程執(zhí)行
runnable.run();
}
}
});
}
}
}
```
SpringBoot事務(wù)提交后執(zhí)行后續(xù)操作SpringBoot事務(wù)提交后執(zhí)行后續(xù)操作 業(yè)務(wù)場景 業(yè)務(wù)需求上經(jīng)常會有一些邊緣操作队询,比如主流程操作A:用戶報名課程操作入庫,邊緣操作B:發(fā)送郵件或短信通知艺骂。 業(yè)務(wù)要求 操作...