Bird
Raised Fist0
Spring Bootframework~20 mins

Event-driven architecture pattern in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Event-Driven Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2: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));
AThe event is queued and listeners are called only when the application context is refreshed.
BListeners are called asynchronously in separate threads after publishEvent returns.
CAll listeners for MyEvent are called synchronously in the same thread before publishEvent returns.
DListeners are called only if explicitly polled by the publisher after publishing.
Attempts:
2 left
💡 Hint
Think about how Spring's default event publishing works in terms of thread execution.
📝 Syntax
intermediate
2: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);
    }
}
A
@EventListener
public void onOrderCreated(OrderCreatedEvent event) { /* handle event */ }
B
@EventListener(OrderCreatedEvent.class)
public void onOrderCreated() { /* handle event */ }
Cpublic void onOrderCreated(@EventListener OrderCreatedEvent event) { /* handle event */ }
D
@EventListener
public void onOrderCreated(Object event) { /* handle event */ }
Attempts:
2 left
💡 Hint
Look for the correct annotation placement and method signature.
🔧 Debug
advanced
2: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));
AThe method is missing the @EventListener annotation, so Spring does not recognize it as an event handler.
BThe event publisher must call handlePaymentEvent directly for the listener to work.
CThe event class PaymentEvent must extend RuntimeException to be handled.
DThe class is not annotated with @Service, so it cannot listen to events.
Attempts:
2 left
💡 Hint
Check if the listener method is properly marked to receive events.
❓ state_output
advanced
2: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));
AOnly Listener A prints because it has higher order.
BListener B prints first, then Listener A.
CListener A prints first, then Listener B.
DListeners print in random order.
Attempts:
2 left
💡 Hint
Lower @Order value means higher priority.
🧠 Conceptual
expert
2: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.
AIt eliminates the need for dependency injection by using events instead.
BIt forces all components to run synchronously, ensuring strict execution order.
CIt requires all events to be stored in a database before processing, improving reliability.
DIt decouples components by allowing them to communicate through events, improving modularity and scalability.
Attempts:
2 left
💡 Hint
Think about how events help components work independently.

Practice

(1/5)
1. What is the main purpose of the event-driven architecture pattern in Spring Boot?
easy
A. To store data in a relational database
B. To allow different parts of the application to communicate by sending and receiving events
C. To create user interfaces with HTML and CSS
D. To handle HTTP requests directly without any middleware

Solution

  1. Step 1: Understand event-driven architecture basics

    This pattern lets components send and listen to events to communicate asynchronously.
  2. Step 2: Identify Spring Boot usage

    Spring Boot uses Java classes and annotations to implement this pattern for flexible communication.
  3. Final Answer:

    To allow different parts of the application to communicate by sending and receiving events -> Option B
  4. Quick Check:

    Event-driven communication = To allow different parts of the application to communicate by sending and receiving events [OK]
Hint: Event-driven means parts talk by sending/listening events [OK]
Common Mistakes:
  • Confusing event-driven with database storage
  • Thinking it handles UI design
  • Assuming it processes HTTP requests directly
2. Which annotation is used in Spring Boot to mark a method as an event listener?
easy
A. @EventHandler
B. @Listener
C. @HandleEvent
D. @EventListener

Solution

  1. Step 1: Recall Spring Boot event listener annotation

    Spring Boot uses @EventListener to mark methods that listen for events.
  2. Step 2: Check other options

    @EventHandler, @Listener, and @HandleEvent are not valid Spring Boot annotations for event listening.
  3. Final Answer:

    @EventListener -> Option D
  4. Quick Check:

    @EventListener marks event methods [OK]
Hint: Remember: @EventListener listens to events in Spring Boot [OK]
Common Mistakes:
  • Using @EventHandler which is not a Spring Boot annotation
  • Confusing with @Listener or @HandleEvent which don't exist
  • Misspelling the annotation name
3. Given this Spring Boot event publishing code snippet, what will be printed?
public class MyEvent extends ApplicationEvent {
    public MyEvent(Object source) { super(source); }
}

@Component
public class Publisher {
    @Autowired
    private ApplicationEventPublisher publisher;

    public void publish() {
        publisher.publishEvent(new MyEvent(this));
    }
}

@Component
public class Listener {
    @EventListener
    public void onMyEvent(MyEvent event) {
        System.out.println("Event received");
    }
}
medium
A. Event received
B. No output
C. Compilation error
D. Runtime exception

Solution

  1. Step 1: Understand event publishing

    The Publisher publishes a MyEvent instance using ApplicationEventPublisher.
  2. Step 2: Understand event listening

    The Listener class has a method annotated with @EventListener that listens for MyEvent and prints "Event received".
  3. Final Answer:

    Event received -> Option A
  4. Quick Check:

    Publishing triggers listener print = Event received [OK]
Hint: Publishing event triggers listener print statement [OK]
Common Mistakes:
  • Thinking no output because listener is missing
  • Assuming compilation error due to event class
  • Expecting runtime exception without error
4. What is wrong with this Spring Boot event listener code?
@Component
public class MyListener {
    @EventListener
    public void handleEvent(String event) {
        System.out.println("Event: " + event);
    }
}
medium
A. EventListener methods cannot accept String as event parameter
B. Missing @Component annotation
C. Method must return a value
D. EventListener annotation is misspelled

Solution

  1. Step 1: Check event parameter type

    Spring events can be any object, including String, but using String as event parameter is discouraged because it may cause ambiguity and unintended triggers.
  2. Step 2: Understand best practice

    Using custom event classes extending ApplicationEvent or plain objects is preferred for clarity and to avoid conflicts.
  3. Final Answer:

    EventListener methods cannot accept String as event parameter -> Option A
  4. Quick Check:

    String event param is invalid for event listener [OK]
Hint: EventListener needs custom event class, not String [OK]
Common Mistakes:
  • Thinking @Component is missing
  • Believing method must return a value
  • Assuming annotation is misspelled
5. You want to build a Spring Boot app where multiple components react to a user registration event asynchronously. Which approach best fits the event-driven architecture pattern?
hard
A. Send HTTP requests manually between components
B. Call all components directly from the registration method without events
C. Create a UserRegisteredEvent class, publish it with ApplicationEventPublisher, and have multiple @EventListener methods handle it
D. Use a database trigger to notify components

Solution

  1. Step 1: Identify event-driven pattern usage

    Creating a custom event class and publishing it allows decoupled communication between components.
  2. Step 2: Understand asynchronous handling

    Multiple @EventListener methods can react independently and asynchronously to the event.
  3. Step 3: Evaluate other options

    Direct calls, database triggers, or manual HTTP requests do not follow Spring Boot event-driven best practices.
  4. Final Answer:

    Create a UserRegisteredEvent class, publish it with ApplicationEventPublisher, and have multiple @EventListener methods handle it -> Option C
  5. Quick Check:

    Custom event + publisher + listeners = Create a UserRegisteredEvent class, publish it with ApplicationEventPublisher, and have multiple @EventListener methods handle it [OK]
Hint: Use custom event class and @EventListener for async reactions [OK]
Common Mistakes:
  • Calling components directly, losing decoupling
  • Relying on database triggers for app events
  • Manually sending HTTP requests between components
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard