Challenge - 5 Problems
Event-Driven Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when an event is published in Spring Boot?
In Spring Boot's event-driven architecture, when an event is published using
ApplicationEventPublisher, what is the immediate behavior?Spring Boot
public class MyEvent extends ApplicationEvent { public MyEvent(Object source) { super(source); } } @Component public class MyEventListener { @EventListener public void handleMyEvent(MyEvent event) { System.out.println("Event received: " + event.getSource()); } } // Somewhere in a service applicationEventPublisher.publishEvent(new MyEvent(this));
Attempts:
2 left
💡 Hint
Think about how Spring's default event publishing works in terms of thread execution.
✗ Incorrect
By default, Spring publishes events synchronously in the same thread that calls publishEvent. All listeners are invoked before the method returns.
📝 Syntax
intermediate2:00remaining
Identify the correct way to declare an event listener method in Spring Boot
Which of the following method declarations correctly listens to a custom event
OrderCreatedEvent in Spring Boot?Spring Boot
public class OrderCreatedEvent extends ApplicationEvent { public OrderCreatedEvent(Object source) { super(source); } }
Attempts:
2 left
💡 Hint
Look for the correct annotation placement and method signature.
✗ Incorrect
The correct way is to annotate the method with @EventListener and accept the event type as a parameter. Option A matches this pattern.
🔧 Debug
advanced2:00remaining
Why does this Spring Boot event listener not get called?
Given this listener class, why is the event handler method never invoked when the event is published?
Spring Boot
@Component public class PaymentListener { public void handlePaymentEvent(PaymentEvent event) { System.out.println("Payment processed"); } } // Event published elsewhere applicationEventPublisher.publishEvent(new PaymentEvent(this));
Attempts:
2 left
💡 Hint
Check if the listener method is properly marked to receive events.
✗ Incorrect
Spring only calls methods annotated with @EventListener (or implementing ApplicationListener). Without the annotation, the method is ignored.
❓ state_output
advanced2:00remaining
What is the output order when multiple listeners handle the same event?
Consider two listeners for the same event
UserRegisteredEvent with different @Order annotations. What is the order of their execution?Spring Boot
@Component public class ListenerA { @EventListener @Order(2) public void onUserRegistered(UserRegisteredEvent event) { System.out.println("Listener A"); } } @Component public class ListenerB { @EventListener @Order(1) public void onUserRegistered(UserRegisteredEvent event) { System.out.println("Listener B"); } } // Event published applicationEventPublisher.publishEvent(new UserRegisteredEvent(this));
Attempts:
2 left
💡 Hint
Lower @Order value means higher priority.
✗ Incorrect
Spring calls listeners in ascending order of their @Order value. So ListenerB (order 1) runs before ListenerA (order 2).
🧠 Conceptual
expert2:00remaining
Which statement best describes the advantage of event-driven architecture in Spring Boot?
Select the option that best explains why using event-driven architecture benefits a Spring Boot application.
Attempts:
2 left
💡 Hint
Think about how events help components work independently.
✗ Incorrect
Event-driven architecture allows components to react to events without tight coupling, making the system more modular and easier to scale.