Fixes and added examples for transactional...
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package io.reflectoring.eventsdemo;
|
||||
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AsyncListener {
|
||||
|
||||
@Async
|
||||
@EventListener
|
||||
public void handleAsyncEvent(String event) {
|
||||
System.out.println(String.format("Async event recevied: %s", event));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,15 +2,16 @@ package io.reflectoring.eventsdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import io.reflectoring.eventsdemo.listeners.SpringBuiltInEventsListener;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAsync
|
||||
public class EventsDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication springApplication = new SpringApplication(EventsDemoApplication.class);
|
||||
springApplication.addListeners(new SpringBuiltInEventsListener());
|
||||
// uncomment below line if you are not using spring.factories
|
||||
// springApplication.addListeners(new SpringBuiltInEventsListener());
|
||||
springApplication.run(args);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package io.reflectoring.eventsdemo;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
class Publisher {
|
||||
|
||||
@Autowired
|
||||
private ApplicationEventPublisher publisher;
|
||||
|
||||
void publishEvent() {
|
||||
// Async Event
|
||||
publisher.publishEvent("I'm Async");
|
||||
|
||||
|
||||
// @EventListener Annotated and ApplicationListener
|
||||
publisher.publishEvent(new UserCreatedEvent(this, "Lucario"));
|
||||
publisher.publishEvent(new UserRemovedEvent("Lucario"));
|
||||
|
||||
// Conditional Listener
|
||||
publisher.publishEvent(new UserCreatedEvent(this, "reflectoring"));
|
||||
publisher.publishEvent(new UserRemovedEvent("reflectoring"));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package io.reflectoring.eventsdemo;
|
||||
|
||||
public class ReturnedEvent {
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.reflectoring.eventsdemo.listeners;
|
||||
package io.reflectoring.eventsdemo;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
@@ -7,9 +7,7 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
import io.reflectoring.eventsdemo.publisher.Publisher;
|
||||
|
||||
public class SpringBuiltInEventsListener implements ApplicationListener<SpringApplicationEvent>, ApplicationContextAware{
|
||||
class SpringBuiltInEventsListener implements ApplicationListener<SpringApplicationEvent>, ApplicationContextAware{
|
||||
|
||||
ApplicationContext applicationContext;
|
||||
|
||||
@@ -28,7 +26,7 @@ public class SpringBuiltInEventsListener implements ApplicationListener<SpringAp
|
||||
|
||||
private void initPublisher(SpringApplicationEvent event) {
|
||||
if(event instanceof ApplicationReadyEvent) {
|
||||
this.applicationContext.getBean(Publisher.class).publishEvent("Lucario");
|
||||
this.applicationContext.getBean(Publisher.class).publishEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package io.reflectoring.eventsdemo;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
class UserCreatedEvent extends ApplicationEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private String name;
|
||||
|
||||
UserCreatedEvent(Object source, String name) {
|
||||
super(source);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package io.reflectoring.eventsdemo;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
class UserCreatedListener implements ApplicationListener<UserCreatedEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(UserCreatedEvent event) {
|
||||
System.out.println(String.format("User created: %s", event.getName()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package io.reflectoring.eventsdemo;
|
||||
|
||||
class UserRemovedEvent {
|
||||
|
||||
public String name;
|
||||
|
||||
UserRemovedEvent(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package io.reflectoring.eventsdemo;
|
||||
|
||||
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 UserRemovedListener {
|
||||
|
||||
@EventListener
|
||||
public ReturnedEvent handleUserRemovedEvent(UserRemovedEvent event) {
|
||||
System.out.println(String.format("User removed (@EventListerner): %s", event.getName()));
|
||||
// Spring will send ReturnedEvent as a new event
|
||||
return new ReturnedEvent();
|
||||
}
|
||||
|
||||
// Listener to receive the event returned by Spring
|
||||
@EventListener
|
||||
public void handleReturnedEvent(ReturnedEvent event) {
|
||||
System.out.println("ReturnedEvent received.");
|
||||
}
|
||||
|
||||
@EventListener(condition = "#event.name eq 'reflectoring'")
|
||||
public void handleConditionalListener(UserRemovedEvent event) {
|
||||
System.out.println(String.format("User removed (Conditional): %s", event.getName()));
|
||||
}
|
||||
|
||||
@TransactionalEventListener(condition = "#event.name eq 'reflectoring'", phase=TransactionPhase.AFTER_COMPLETION)
|
||||
public void handleAfterUserRemoved(UserRemovedEvent event) {
|
||||
System.out.println(String.format("User removed (@TransactionalEventListener): %s", event.getName()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package io.reflectoring.eventsdemo.events;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
public class UserCreatedEvent extends ApplicationEvent {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private String name;
|
||||
|
||||
public UserCreatedEvent(String name) {
|
||||
super(name);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package io.reflectoring.eventsdemo.listeners;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import io.reflectoring.eventsdemo.events.UserCreatedEvent;
|
||||
|
||||
@Component
|
||||
public class UserCreatedListener implements ApplicationListener<UserCreatedEvent> {
|
||||
|
||||
@EventListener
|
||||
public void handleUserCreatedEvent(UserCreatedEvent event) {
|
||||
System.out.println("User created event triggered with annotated listener. Created user: " + event.getName());
|
||||
}
|
||||
|
||||
@EventListener
|
||||
public void handleUserCreatedEvent2(UserCreatedEvent event) {
|
||||
System.out.println("Another listener listening to user created event: " + event.getName());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(UserCreatedEvent event) {
|
||||
System.out.println("User created event triggered with ApplicationListener implemented listener. Created user: " + event.getName());
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package io.reflectoring.eventsdemo.publisher;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import io.reflectoring.eventsdemo.events.UserCreatedEvent;
|
||||
|
||||
@Component
|
||||
public class Publisher {
|
||||
|
||||
@Autowired
|
||||
private ApplicationEventPublisher publisher;
|
||||
|
||||
public void publishEvent(final String name) {
|
||||
publisher.publishEvent(new UserCreatedEvent(name));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.springframework.context.ApplicationListener=io.reflectoring.eventsdemo.SpringBuiltInEventsListener
|
||||
@@ -17,8 +17,8 @@ import org.springframework.boot.test.mock.mockito.SpyBean;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import io.reflectoring.eventsdemo.events.UserCreatedEvent;
|
||||
import io.reflectoring.eventsdemo.listeners.UserCreatedListener;
|
||||
import io.reflectoring.eventsdemo.UserCreatedEvent;
|
||||
import io.reflectoring.eventsdemo.UserListener;
|
||||
import junit.framework.Assert;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@@ -29,7 +29,7 @@ class EventsDemoApplicationTests {
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
|
||||
@SpyBean
|
||||
UserCreatedListener userCreatedListener;
|
||||
UserListener userCreatedListener;
|
||||
|
||||
@Captor
|
||||
protected ArgumentCaptor<UserCreatedEvent> publishEventCaptor;
|
||||
|
||||
Reference in New Issue
Block a user