0
0
Spring Bootframework~20 mins

Event publishing with ApplicationEventPublisher in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring Event Master
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 using ApplicationEventPublisher?
Consider a Spring Boot application where a custom event is published using ApplicationEventPublisher. What is the expected behavior after publishing the event?
Spring Boot
public class CustomEvent extends ApplicationEvent {
    public CustomEvent(Object source) {
        super(source);
    }
}

@Component
public class EventPublisher {
    private final ApplicationEventPublisher publisher;

    public EventPublisher(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }

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

@Component
public class EventListener {
    @EventListener
    public void handleCustomEvent(CustomEvent event) {
        System.out.println("Event received: " + event.getSource());
    }
}
AThe event triggers a restart of the Spring Boot application context.
BThe event is queued and processed asynchronously by default in a separate thread pool.
CThe event is published synchronously and all matching @EventListener methods are called immediately in the same thread.
DThe event is ignored unless explicitly registered in a configuration file.
Attempts:
2 left
💡 Hint
Think about how Spring handles events by default without extra configuration.
📝 Syntax
intermediate
1:30remaining
Which code snippet correctly publishes a custom event in Spring Boot?
Select the code snippet that correctly publishes a custom event using ApplicationEventPublisher in a Spring Boot component.
Apublisher.publishEvent(new CustomEvent(this));
Bpublisher.publish(new CustomEvent(this));
Cpublisher.fireEvent(new CustomEvent(this));
Dpublisher.sendEvent(new CustomEvent(this));
Attempts:
2 left
💡 Hint
Check the exact method name in ApplicationEventPublisher interface.
🔧 Debug
advanced
2:00remaining
Why does the event listener not receive the published event?
Given the following code, why does the event listener not receive the event? @Component public class MyListener { public void onCustomEvent(CustomEvent event) { System.out.println("Event received"); } } @Component public class MyPublisher { private final ApplicationEventPublisher publisher; public MyPublisher(ApplicationEventPublisher publisher) { this.publisher = publisher; } public void publish() { publisher.publishEvent(new CustomEvent(this)); } }
AThe listener method is missing the @EventListener annotation.
BThe event class CustomEvent is not annotated with @Component.
CThe ApplicationEventPublisher is not injected properly.
DThe event must be published asynchronously to be received.
Attempts:
2 left
💡 Hint
Check how Spring knows which methods listen to events.
🧠 Conceptual
advanced
2:30remaining
What is the effect of publishing an event with a payload object instead of extending ApplicationEvent?
In Spring Framework 4.2 and later, you can publish any object as an event without extending ApplicationEvent. What is the effect of publishing a plain payload object instead of a subclass of ApplicationEvent?
AThe event triggers a runtime exception due to missing ApplicationEvent superclass.
BThe event is published normally and listeners can receive the payload object directly.
CThe event is ignored because it does not extend ApplicationEvent.
DThe event is published but listeners must cast the event to ApplicationEvent manually.
Attempts:
2 left
💡 Hint
Spring Framework supports publishing any object as event since version 4.2.
state_output
expert
2:30remaining
What is the console output after publishing two events synchronously?
Given the following Spring Boot components, what will be printed to the console after calling publisher.publish()? public class CustomEvent extends ApplicationEvent { public CustomEvent(Object source) { super(source); } } @Component public class EventPublisher { private final ApplicationEventPublisher publisher; public EventPublisher(ApplicationEventPublisher publisher) { this.publisher = publisher; } public void publish() { publisher.publishEvent(new CustomEvent("First")); publisher.publishEvent(new CustomEvent("Second")); } } @Component public class EventListener { @EventListener public void onEvent(CustomEvent event) { System.out.println("Received: " + event.getSource()); } }
ANo output because events are asynchronous
B
Received: Second
Received: First
CReceived: First
D
Received: First
Received: Second
Attempts:
2 left
💡 Hint
Events are published synchronously in order by default.