1. Custom Application Event
package ; import ; import ; import ; /** * @className MyEvent * @author weixiansheng * @date 2023/9/28 * @version V1.0 **/ @Setter @Getter public class MyEvent extends ApplicationEvent { private String data; public MyEvent(Object source, String data) { super(source); = data; } }
2. Custom listening
package ; import ; import .slf4j.Slf4j; import ; import ; /** * Simple monitoring * * @author weixiang * @version V1.0 * @className MySimpleListener * @date 2023/9/28 **/ @Component @Slf4j public class MySimpleListener { /** * @param event * @methodName: handleDemoEvent * @return: void * @author: weixiansheng * @date: 2023/9/28 **/ @EventListener public void handleDemoEvent(MyEvent event) { ("Posteddatafor:{}", ()); } }
3. Test
package ; import ; import ; import .slf4j.Slf4j; import ; import ; import ; /** * @author weixiansheng * @version V1.0 * @className MySimpleListenerTest * @date 2023/9/28 **/ @SpringBootTest @Slf4j class MySimpleListenerTest { /** * Post a message * * @methodName: publishEvent * @return: void * @author: weixiang * @date: 2023/9/28 **/ @Test public void publishEvent() throws InterruptedException { ("publishEvent start"); (new MyEvent(this, "test")); ("publishEvent end"); (1); } }
Print log
[INFO ] 2023-09-28 10:19:15.312 [main] - publishEvent start
[INFO] 2023-09-28 10:19:16.344 [main] - The published data is: test
[INFO ] 2023-09-28 10:19:16.347 [main] - publishEvent end
4. Source code
share: share warehouse -
5. Others
5.1 Sequential execution
Use Notes
- @Order The smaller the value of the order, the higher the priority.
- If the order is not marked with a number, the default minimum priority is because its default value is the maximum value of int.
Example
/** * Normal monitoring * * @param event * @methodName: handleDemoEvent * @return: void * @author: weixiang * @date: 2023/9/28 **/ @EventListener @Order(2) public void handleEvent(MyEvent event) throws InterruptedException { (1); ("handleEvent data:{}", ()); } /** * Conditional monitoring * * @param event * @methodName: handleConditionEvent * @return: void * @author: weixiang * @date: 2023/9/28 **/ @EventListener(condition = "#=='Zhang San'")@Order(1) public void handleConditionEvent(MyEvent event) { ("handleConditionEvent data:{}", ()); }
Print log
[INFO ] 2023-09-28 10:40:22.206 [main] - handleConditionEvent data: Zhang San
[INFO ] 2023-09-28 10:40:23.216 [main] - handleEvent data: Zhang San
5.2 Asynchronous support
The Spring event mechanism is synchronously blocked by default. If the ApplicationEventPublisher publishes an event, it will keep blocking and waiting for the listener to respond. In the case of multiple listeners, the previous ones that have not been executed after the next ones will be blocked. The publisher and the subscriber belong to the same transaction. If the subscriber fails to execute, the publisher transaction will roll back.
Asynchronous threading can be implemented using the thread pool annotation @Async provided by Spring. Asynchronously does not affect the publisher's transactions.
Example
/** * Normal monitoring * * @param event * @methodName: handleDemoEvent * @return: void * @author: weixiang * @date: 2023/9/28 **/ @EventListener public void handleEvent(MyEvent event) throws InterruptedException { (1); ("handleEvent data:{}", ()); } /** * Conditional monitoring * * @param event * @methodName: handleConditionEvent * @return: void * @author: weixiang * @date: 2023/9/28 **/ @Async @EventListener(condition = "#=='Zhang San'")public void handleConditionEvent(MyEvent event) { ("handleConditionEvent data:{}", ()); }
Print log
[INFO ] 2023-09-28 10:49:40.246 [thread-pool-1] - handleConditionEvent data: Zhang San
[INFO ] 2023-09-28 10:49:41.255 [main] - handleEvent data: Zhang San
One is thread thread-pool-1 and the other is thread main.
6. Summary
-
Event pattern concept
- event:The trigger of an event, such as user registration, is an event.
- Event release:The object that describes what happened, such as the event in which Zhang San successfully registered.
- Event listening: When listening to an event, do some processing, such as increasing user points after placing an order.
-
The processing of event listeners in Spring is synchronous
- The logs are all the same thread.
- In the order of execution, after executing the listener's business, the subsequent logic will continue to be executed downward. As shown in the above example: publishEvent end.
This is the end of this article about spring @EventListener events and listening. For more related spring @EventListener events and listening content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!