BAEL-1596 Spring Events (#5605)

* Annotation-driven event listener + generic events

* update examples + add tests

* add article link

* add more tests for Spring Events
This commit is contained in:
Paul
2018-11-09 16:35:44 +03:00
committed by Eugen
parent 064113f939
commit 8e6fff4fc8
12 changed files with 214 additions and 5 deletions
@@ -0,0 +1,46 @@
package org.baeldung.springevents.synchronous;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;
@Component
public class AnnotationDrivenEventListener {
// for tests
private boolean hitContextStartedHandler = false;
private boolean hitSuccessfulEventHandler = false;
private boolean hitCustomEventHandler = false;
@EventListener
public void handleContextStart(final ContextStartedEvent cse) {
System.out.println("Handling context started event.");
hitContextStartedHandler = true;
}
@EventListener(condition = "#event.success")
public void handleSuccessful(final GenericSpringEvent<String> event) {
System.out.println("Handling generic event (conditional): " + event.getWhat());
hitSuccessfulEventHandler = true;
}
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
public void handleCustom(final CustomSpringEvent event) {
System.out.println("Handling event inside a transaction BEFORE COMMIT.");
hitCustomEventHandler = true;
}
boolean isHitContextStartedHandler() {
return hitContextStartedHandler;
}
boolean isHitSuccessfulEventHandler() {
return hitSuccessfulEventHandler;
}
boolean isHitCustomEventHandler() {
return hitCustomEventHandler;
}
}
@@ -7,9 +7,16 @@ import org.springframework.stereotype.Component;
@Component
public class ContextRefreshedListener implements ApplicationListener<ContextRefreshedEvent> {
// for tests
private boolean hitContextRefreshedHandler = false;
@Override
public void onApplicationEvent(final ContextRefreshedEvent cse) {
System.out.println("Handling context re-freshed event. ");
hitContextRefreshedHandler = true;
}
boolean isHitContextRefreshedHandler() {
return hitContextRefreshedHandler;
}
}
@@ -16,4 +16,16 @@ public class CustomSpringEventPublisher {
applicationEventPublisher.publishEvent(customSpringEvent);
}
public void publishGenericEvent(final String message, boolean success) {
System.out.println("Publishing generic event.");
final GenericSpringEvent<String> genericSpringEvent = new GenericStringSpringEvent(message, success);
applicationEventPublisher.publishEvent(genericSpringEvent);
}
public void publishGenericAppEvent(final String message) {
System.out.println("Publishing generic event.");
final GenericSpringAppEvent<String> genericSpringEvent = new GenericStringSpringAppEvent(this, message);
applicationEventPublisher.publishEvent(genericSpringEvent);
}
}
@@ -0,0 +1,18 @@
package org.baeldung.springevents.synchronous;
import org.springframework.context.ApplicationEvent;
public class GenericSpringAppEvent<T> extends ApplicationEvent {
private final T what;
public GenericSpringAppEvent(final Object source, final T what) {
super(source);
this.what = what;
}
public T getWhat() {
return what;
}
}
@@ -0,0 +1,21 @@
package org.baeldung.springevents.synchronous;
public class GenericSpringEvent<T> {
private final T what;
protected final boolean success;
public GenericSpringEvent(final T what, final boolean success) {
this.what = what;
this.success = success;
}
public T getWhat() {
return what;
}
public boolean isSuccess() {
return success;
}
}
@@ -0,0 +1,22 @@
package org.baeldung.springevents.synchronous;
import org.springframework.context.ApplicationListener;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
@Component
public class GenericSpringEventListener implements ApplicationListener<GenericSpringAppEvent<String>> {
// for testing
private boolean hitEventHandler = false;
@Override
public void onApplicationEvent(@NonNull final GenericSpringAppEvent<String> event) {
System.out.println("Received spring generic event - " + event.getWhat());
hitEventHandler = true;
}
boolean isHitEventHandler() {
return hitEventHandler;
}
}
@@ -0,0 +1,9 @@
package org.baeldung.springevents.synchronous;
class GenericStringSpringAppEvent extends GenericSpringAppEvent<String> {
GenericStringSpringAppEvent(final Object source, final String what) {
super(source, what);
}
}
@@ -0,0 +1,9 @@
package org.baeldung.springevents.synchronous;
public class GenericStringSpringEvent extends GenericSpringEvent<String> {
GenericStringSpringEvent(final String what, final boolean success) {
super(what, success);
}
}