https://github.com/ghusta/spring-boot-demo-event-publishing
Demo for Spring Boot Application Events
https://github.com/ghusta/spring-boot-demo-event-publishing
event spring-boot
Last synced: about 2 months ago
JSON representation
Demo for Spring Boot Application Events
- Host: GitHub
- URL: https://github.com/ghusta/spring-boot-demo-event-publishing
- Owner: ghusta
- Created: 2023-03-15T21:00:07.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-02T18:53:48.000Z (about 3 years ago)
- Last Synced: 2025-02-24T13:48:41.045Z (over 1 year ago)
- Topics: event, spring-boot
- Language: Java
- Homepage:
- Size: 75.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.adoc
Awesome Lists containing this project
README
= Demo for Spring Boot Application Events
:author: Guillaume HUSTA
:toc:
== Overview
Demo for testing publish/subscribe events with Spring Boot.
Some subscribers are *synchronous*, others are *asynchronous* like email sending.
=== Publisher
One publisher publishes events.
All these events will be subclasses of `ObservationEvent`.
=== Subscribers
There are 2 kinds of subscribers : synchronous and asynchronous.
==== Synchronous
* `ObservationEventCyanLoggerListener` prints events on console with color _cyan_
* `ObservationEventGreenLoggerListener` prints events on console with color _green_
* `ObservationEventRedLoggerListener` prints events on console with color _red_
==== Asynchronous
* `AsyncObservationEventLoggerListener` prints events on console asynchronously
* `SendMailObservationListener` sends emails asynchronously, if SMTP server is available
== How to test it
=== Ensure a SMTP server is accessible
Either configure `src/main/resources/application.properties` with your own SMTP server (see https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#appendix.application-properties.mail[Spring Boot mail configuration]), or launch a fake SMTP server.
You can do the latter with the provided link:docker-compose.yml[docker-compose.yml] file, launching a SMTP server by default on localhost, port 2525 :
[source,bash]
----
docker compose up -d
----
The sent emails will then be accessible in the local folder `sent-emails`.
=== Launch Spring Boot app
With Maven :
[source,bash]
----
mvn spring-boot:run
----
=== Execution log example
====
[%hardbreaks]
2023-03-16T10:22:19.486+01:00 INFO 6240 --- [ main] c.e.d.DemoEventPublishingApplication : Starting DemoEventPublishingApplication using Java 17.0.6 with PID 6240 (\Java\demo-event-publishing\target\classes started by g in \Java\demo-event-publishing)
2023-03-16T10:22:19.489+01:00 INFO 6240 --- [ main] c.e.d.DemoEventPublishingApplication : No active profile set, falling back to 1 default profile: "default"
2023-03-16T10:22:19.868+01:00 INFO 6240 --- [ main] c.e.d.DemoEventPublishingApplication : Started DemoEventPublishingApplication in 0.619 seconds (process running for 0.825)
2023-03-16T10:22:19.870+01:00 INFO 6240 --- [ main] c.e.d.DemoEventPublishingApplication : Running the app...
2023-03-16T10:22:19.870+01:00 INFO 6240 --- [ main] c.e.d.service.MyService : Entering addObservation()...
ASYNC -- It's a ObservationAddedEvent !
[blue]#It's a ObservationAddedEvent... Content is : Nothing interesting 😴#
[green]#It's a ObservationAddedEvent... Content is : Nothing interesting 😴#
[red]#It's a ObservationAddedEvent... Content is : Nothing interesting 😴#
2023-03-16T10:22:19.885+01:00 INFO 6240 --- [ main] c.e.d.service.MyService : Entering publishObservation()...
ASYNC -- It's a ObservationPublishedEvent !
[blue]#It's a ObservationPublishedEvent !#
[green]#It's a ObservationPublishedEvent !#
[red]#It's a ObservationPublishedEvent !#
2023-03-16T10:22:19.886+01:00 INFO 6240 --- [ main] c.e.d.service.MyService : Entering removeObservation()...
2023-03-16T10:22:19.886+01:00 DEBUG 6240 --- [ task-4] c.e.d.l.SendMailObservationListener : Sending a mail async... (Observation published) (in task-4)
2023-03-16T10:22:19.885+01:00 DEBUG 6240 --- [ task-2] c.e.d.l.SendMailObservationListener : Sending a mail async... (Observation added) (in task-2)
[blue]#It's a ObservationRemovedEvent !#
[green]#It's a ObservationRemovedEvent !#
[red]#It's a ObservationRemovedEvent !#
2023-03-16T10:22:19.887+01:00 DEBUG 6240 --- [ task-6] c.e.d.l.SendMailObservationListener : Event not managed (com.example.demoeventpublishing.event.ObservationRemovedEvent)
2023-03-16T10:22:20.014+01:00 DEBUG 6240 --- [ task-4] c.e.d.l.SendMailObservationListener : Mail sent (in task-4)
2023-03-16T10:22:20.015+01:00 DEBUG 6240 --- [ task-2] c.e.d.l.SendMailObservationListener : Mail sent (in task-2)
====
== References
* Spring Boot :
** https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.spring-application.application-events-and-listeners[Application Events and Listeners]
** https://docs.spring.io/spring-boot/docs/current/reference/html/io.html#io.email[Sending Email]
** https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.task-execution-and-scheduling[Task Execution and Scheduling], for asynchronous task execution (`@EnableAsync`)
* Articles :
** https://reflectoring.io/spring-boot-application-events-explained/[Spring Boot Application Events Explained]
** https://howtodoinjava.com/spring-core/how-to-publish-and-listen-application-events-in-spring/[Spring Application Events]
* Issues / Mentions :
** ⭐ https://github.com/spring-projects/spring-framework/issues/27777[Mention explicitly that @EventListener should be put on public methods] => *INVALID*
*** Related : https://github.com/spring-projects/spring-framework/pull/26785