diff --git a/src/main/java/com/dnd/runus/application/config/TransactionEventHandler.java b/src/main/java/com/dnd/runus/application/config/TransactionEventHandler.java new file mode 100644 index 00000000..2847f908 --- /dev/null +++ b/src/main/java/com/dnd/runus/application/config/TransactionEventHandler.java @@ -0,0 +1,57 @@ +package com.dnd.runus.application.config; + +import com.dnd.runus.global.event.AfterTransactionEvent; +import com.dnd.runus.global.event.BeforeTransactionEvent; +import org.springframework.core.annotation.Order; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; +import org.springframework.transaction.event.TransactionalEventListener; + +import static org.springframework.transaction.event.TransactionPhase.*; + +@Component +public class TransactionEventHandler { + + private static final int AFTER_COMMIT_ORDER = 1; + private static final int AFTER_ROLLBACK_ORDER = AFTER_COMMIT_ORDER + 1; + private static final int AFTER_COMPLETION_ORDER = AFTER_COMMIT_ORDER + 2; + + /** + * 트랜잭션이 commit되기 전에 실행 + */ + @Async + @TransactionalEventListener(phase = BEFORE_COMMIT) + public void processBeforeCommit(BeforeTransactionEvent event) { + event.invoke(); + } + + /** + * 트랜잭션이 commit된 후에 실행 + */ + @Async + @Order(AFTER_COMMIT_ORDER) + @TransactionalEventListener(phase = AFTER_COMMIT) + public void processAfterCommit(AfterTransactionEvent event) { + event.invoke(); + } + + /** + * 트랜잭션이 rollback된 후에 실행 + */ + @Async + @Order(AFTER_ROLLBACK_ORDER) + @TransactionalEventListener(phase = AFTER_ROLLBACK) + public void processAfterRollback(AfterTransactionEvent event) { + event.onTransactionRollback(); + } + + /** + * 트랜잭션이 commit 또는 rollback된 후에 실행 + */ + @Async + @Order(AFTER_COMPLETION_ORDER) + @TransactionalEventListener(phase = AFTER_COMPLETION) + public void processAfterCompletion(AfterTransactionEvent event) { + event.onComplete(); + } +} diff --git a/src/main/java/com/dnd/runus/global/event/AfterTransactionEvent.java b/src/main/java/com/dnd/runus/global/event/AfterTransactionEvent.java new file mode 100644 index 00000000..906e1ffe --- /dev/null +++ b/src/main/java/com/dnd/runus/global/event/AfterTransactionEvent.java @@ -0,0 +1,7 @@ +package com.dnd.runus.global.event; + +public interface AfterTransactionEvent extends TransactionEvent { + default void onTransactionRollback() {} + + default void onComplete() {} +} diff --git a/src/main/java/com/dnd/runus/global/event/BeforeTransactionEvent.java b/src/main/java/com/dnd/runus/global/event/BeforeTransactionEvent.java new file mode 100644 index 00000000..7dda4c8e --- /dev/null +++ b/src/main/java/com/dnd/runus/global/event/BeforeTransactionEvent.java @@ -0,0 +1,3 @@ +package com.dnd.runus.global.event; + +public interface BeforeTransactionEvent extends TransactionEvent {} diff --git a/src/main/java/com/dnd/runus/global/event/TransactionEvent.java b/src/main/java/com/dnd/runus/global/event/TransactionEvent.java new file mode 100644 index 00000000..91e84eeb --- /dev/null +++ b/src/main/java/com/dnd/runus/global/event/TransactionEvent.java @@ -0,0 +1,5 @@ +package com.dnd.runus.global.event; + +interface TransactionEvent { + void invoke(); +}