diff --git a/spring-tx/src/main/java/org/springframework/transaction/event/ApplicationListenerMethodTransactionalAdapter.java b/spring-tx/src/main/java/org/springframework/transaction/event/ApplicationListenerMethodTransactionalAdapter.java index a33b07bc63d..3368aa8bf73 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/event/ApplicationListenerMethodTransactionalAdapter.java +++ b/spring-tx/src/main/java/org/springframework/transaction/event/ApplicationListenerMethodTransactionalAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -98,6 +98,11 @@ class ApplicationListenerMethodTransactionalAdapter extends ApplicationListenerM this.phase = phase; } + @Override + public int getOrder() { + return this.listener.getOrder(); + } + @Override public void beforeCommit(boolean readOnly) { if (this.phase == TransactionPhase.BEFORE_COMMIT) { @@ -107,20 +112,15 @@ class ApplicationListenerMethodTransactionalAdapter extends ApplicationListenerM @Override public void afterCompletion(int status) { - if (this.phase == TransactionPhase.AFTER_COMPLETION) { - processEvent(); - } - else if (this.phase == TransactionPhase.AFTER_COMMIT && status == STATUS_COMMITTED) { + if (this.phase == TransactionPhase.AFTER_COMMIT && status == STATUS_COMMITTED) { processEvent(); } else if (this.phase == TransactionPhase.AFTER_ROLLBACK && status == STATUS_ROLLED_BACK) { processEvent(); } - } - - @Override - public int getOrder() { - return this.listener.getOrder(); + else if (this.phase == TransactionPhase.AFTER_COMPLETION) { + processEvent(); + } } protected void processEvent() { diff --git a/spring-tx/src/main/java/org/springframework/transaction/event/TransactionPhase.java b/spring-tx/src/main/java/org/springframework/transaction/event/TransactionPhase.java index 1f928cee437..84e7ea98d67 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/event/TransactionPhase.java +++ b/spring-tx/src/main/java/org/springframework/transaction/event/TransactionPhase.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import org.springframework.transaction.support.TransactionSynchronization; * The phase at which a transactional event listener applies. * * @author Stephane Nicoll + * @author Juergen Hoeller * @since 4.2 * @see TransactionalEventListener */ @@ -33,17 +34,11 @@ public enum TransactionPhase { */ BEFORE_COMMIT, - /** - * Fire the event after the transaction has completed. For - * more fine-grained event, use {@link #AFTER_COMMIT} or - * {@link #AFTER_ROLLBACK} to intercept transaction commit - * or rollback respectively. - * @see TransactionSynchronization#afterCompletion(int) - */ - AFTER_COMPLETION, - /** * Fire the event after the commit has completed successfully. + *

Note: This is a specialization of {@link #AFTER_COMPLETION} and + * therefore executes in the same after-completion sequence of events, + * (and not in {@link TransactionSynchronization#afterCommit()}). * @see TransactionSynchronization#afterCompletion(int) * @see TransactionSynchronization#STATUS_COMMITTED */ @@ -51,9 +46,20 @@ public enum TransactionPhase { /** * Fire the event if the transaction has rolled back. + *

Note: This is a specialization of {@link #AFTER_COMPLETION} and + * therefore executes in the same after-completion sequence of events. * @see TransactionSynchronization#afterCompletion(int) * @see TransactionSynchronization#STATUS_ROLLED_BACK */ - AFTER_ROLLBACK + AFTER_ROLLBACK, + + /** + * Fire the event after the transaction has completed. + *

For more fine-grained events, use {@link #AFTER_COMMIT} or + * {@link #AFTER_ROLLBACK} to intercept transaction commit + * or rollback, respectively. + * @see TransactionSynchronization#afterCompletion(int) + */ + AFTER_COMPLETION } diff --git a/spring-tx/src/main/java/org/springframework/transaction/event/TransactionalEventListener.java b/spring-tx/src/main/java/org/springframework/transaction/event/TransactionalEventListener.java index 64d4fcae5af..8c7d67b04ec 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/event/TransactionalEventListener.java +++ b/spring-tx/src/main/java/org/springframework/transaction/event/TransactionalEventListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,25 +28,27 @@ import org.springframework.core.annotation.AliasFor; /** * An {@link EventListener} that is invoked according to a {@link TransactionPhase}. * - *

If the event is not published within the boundaries of a managed transaction, the event - * is discarded unless the {@link #fallbackExecution} flag is explicitly set. If a + *

If the event is not published within the boundaries of a managed transaction, the + * event is discarded unless the {@link #fallbackExecution} flag is explicitly set. If a * transaction is running, the event is processed according to its {@code TransactionPhase}. * - *

Adding {@link org.springframework.core.annotation.Order @Order} on your annotated method - * allows you to prioritize that listener amongst other listeners running in the same phase. + *

Adding {@link org.springframework.core.annotation.Order @Order} to your annotated + * method allows you to prioritize that listener amongst other listeners running before + * or after transaction completion. * * @author Stephane Nicoll * @author Sam Brannen * @since 4.2 */ -@EventListener @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented +@EventListener public @interface TransactionalEventListener { /** * Phase to bind the handling of an event to. + *

The default phase is {@link TransactionPhase#AFTER_COMMIT}. *

If no transaction is in progress, the event is not processed at * all unless {@link #fallbackExecution} has been enabled explicitly. */ @@ -76,7 +78,7 @@ public @interface TransactionalEventListener { /** * Spring Expression Language (SpEL) attribute used for making the event * handling conditional. - *

Default is {@code ""}, meaning the event is always handled. + *

The default is {@code ""}, meaning the event is always handled. * @see EventListener#condition */ String condition() default "";