This project illustrates how Spring Boot allows developers to consume and produce synchronous events by default.
To publish events, Spring Boot needs to create an event publisher for sending the event messages to listeners by injecting the ApplicationEventPublisher as well as using the publishEvent()
API. Spring developers also need to create custom events that should extend ApplicationEvent
. The custom events in Spring are synchronous by default so Spring developers need to create an ApplicationEventMulticaster
in the configuration for handling asynchronous events or add an @Async
annotation to the listener.
You can run your Spring boot application:
./mvnw spring-boot:run
Then, you should see the published custom event:
Publishing custom event......
Received Spring Event: A test event message
The application can be packaged using:
./mvnw package
It produces the chapter-5-spring-eventbus-0.0.1-SNAPSHOT.jar
file in the target/
directory.
The application is now runnable using java -jar target/chapter-5-spring-eventbus-0.0.1-SNAPSHOT.jar
.
For further reference, please consider the following sections:
- Official Apache Maven documentation
- Spring Boot Maven Plugin Reference Guide
- Create an OCI image
- Spring Web
The following guides illustrate how to use some features concretely: