Implementation of springboot Event Monitoring
- 2021-07-18 08:05:46
- OfStack
Defining Events
@Getter
public class TestEvent extends ApplicationEvent {
private String msg;
public TestEvent(Object source, String msg) {
super(source);
this.msg = msg;
}
}
Define event listening (annotation mode)
@Component
public class TestListen {
@EventListener
public void testListen(TestEvent event) {
System.out.println(event.getMsg());
}
}
Note: @ Component annotation
Publish Events
@Autowired
private ApplicationContext publiser;
@GetMapping("test-listen")
public void testListen() {
for (int i = 0; i < 10; i++) {
System.out.println("i = " + i);
}
publiser.publishEvent(new TestEvent(this, " Test event listening "));
for (int j = 0; j < 10; j++) {
System.out.println("j = " + j);
}
}
Execution order when testing:
i cycle Print "event = [Test Event Listening]" j cycle
Asynchronous listening
Monitor with @ Async annotation
@Component
public class TestListen {
@EventListener
@Async
public void testListen(TestEvent event) {
for (int i = 0; i < 10; i++) {
System.out.println("event = [" + event.getMsg() + "]");
}
}
}
Execution order when testing:
i cycle j cycle Print "event = [Test Event Listening]"
Code: async
springboot listens for events in four ways:
1. Manually add listeners to ApplicationContext
2. Load the listener into the spring container
3. Configure the listener in application. properties
4. Event listening through @ EventListener annotations
When it comes to event listening, let's talk about the implementation of custom events and custom listener classes:
Let's talk about the concrete implementation of the next four kinds of event monitoring
Mode 1.
Create the MyListener1 class first
public class MyListener1 implements ApplicationListener<MyEvent>
{
Logger logger = Logger.getLogger(MyListener1.class);
public void onApplicationEvent(MyEvent event)
{
logger.info(String.format("%s Listening to the event source: %s.", MyListener1.class.getName(), event.getSource()));
}
}
Then get the ConfigurableApplicationContext context in the springboot application startup class and load the listening
@SpringBootApplication
public class LisenterApplication
{
public static void main(String[] args)
{
ConfigurableApplicationContext context = SpringApplication.run(LisenterApplication.class, args);
// Load monitoring
context.addApplicationListener(new MyListener1());
}
}
Mode 2.
Create an MyListener2 class and load it into an spring container using the @ Component annotation
@Component
public class MyListener2 implements ApplicationListener<MyEvent>
{
Logger logger = Logger.getLogger(MyListener2.class);
public void onApplicationEvent(MyEvent event)
{
logger.info(String.format("%s Listening to the event source: %s.", MyListener2.class.getName(), event.getSource()));
}
}
Mode 3.
Create the MyListener3 class first
public class MyListener3 implements ApplicationListener<MyEvent>
{
Logger logger = Logger.getLogger(MyListener3.class);
public void onApplicationEvent(MyEvent event)
{
logger.info(String.format("%s Listening to the event source: %s.", MyListener3.class.getName(), event.getSource()));
}
}
Then configure listening in application. properties
context.listener.classes=com.listener.MyListener3
Mode 4.
Create the MyListener4 class, which does not need to implement the ApplicationListener interface, and uses @ EventListener to decorate the specific method
@Component
public class MyListener4
{
Logger logger = Logger.getLogger(MyListener4.class);
@EventListener
public void listener(MyEvent event)
{
logger.info(String.format("%s Listening to the event source: %s.", MyListener4.class.getName(), event.getSource()));
}
}
The custom event code is as follows:
@Component
public class TestListen {
@EventListener
public void testListen(TestEvent event) {
System.out.println(event.getMsg());
}
}
0
Test (add logic to publish events in the startup class):
@Component
public class TestListen {
@EventListener
public void testListen(TestEvent event) {
System.out.println(event.getMsg());
}
}
1
After startup, the log prints as follows:
2018-06-15 10:51:20. 198 INFO 4628--[main] com. listener. MyListener3: com. listener. MyListener3 Listening to Event Source: Test Event..
2018-06-15 10:51:20. 198 INFO 4628--[main] com. listener. MyListener4: com. listener. MyListener4 Listening to Event Source: Test Event..
2018-06-15 10:51:20. 199 INFO 4628--[main] com. listener. MyListener2: com. listener. MyListener2 Listening to Event Source: Test Event..
2018-06-15 10:51: 20. 199 INFO 4628--[main] com. listener. MyListener1: com. listener. MyListener1 Supervisor
Hear event source: Test event..
It can be seen from the log printing that the monitoring of SpringBoot 4 events is orderly
Complete code path: https://github.com/ingorewho/springboot-develope/tree/master/springboot-listener