Spring learning notes (16) Interesting talk about spring event mechanism [2]: Multi-listener pipeline sequential processing

Source: Internet
Author: User

Spring learning notes (16) Interesting talk about spring event mechanism [2]: Multi-listener pipeline sequential processing

The ApplicationListener we used in the previous article is unordered. Combined with asynchronous scheduling, it can meet most of our application scenarios. But now we have an alternative requirement. Let's simulateJob Scheduling PipelineIt cannot be asynchronous. We must execute different tasks in order to get our final result.
Demand example: Now, if Xiao Bai of Huazhong University of Science and Technology wants to apply for a National Innovation Award for his smart robot work, it must be reviewed by schools, provincial innovative research institutions, and national innovative research institutions layer by layer. We try to implement it through events. The core is to implement the SmartApplicationListener interface in the listener. Example:

1. Configure the event Publisher:
Public class XiaoBai implements ApplicationContextAware {private ApplicationContext applicationContext; // The underlying event publisher public void reportWorks () {// file AuditEvent auditEvent = new AuditEvent (this); applicationContext. publishEvent (auditEvent); // obtain the long-awaited final result System. out. println ("final audit result:" + auditEvent. getStatus () + "--" + auditEvent. getAdvice () ;}@ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {this. applicationContext = applicationContext ;}}

For the above configuration, refer to my previous article "spring learning notes (13) Interesting spring events: implementing business logic decoupling and asynchronous calls to improve user experience".

2. Configure Audit Event sources
Public class AuditEvent extends ApplicationEvent {private Boolean status; // The current reporting status private String work; // The reporting work private String advice; // The current reporting opinion public AuditEvent (Object source) {super (source); status = true; // initial window sill advice = "not reviewed";} // ignore getter and setter}
3. Configure the event listener

The core part of our implementation needs comes, first go to the code

/****************** School Audit listener ****************** /public class SchoolListener implements SmartApplicationListener {@ Override public void onApplicationEvent (ApplicationEvent event) {System. out. println ("Get current reporting status:" + (AuditEvent) event ). getStatus () + "--" + (AuditEvent) event ). getAdvice (); (AuditEvent) event ). setStatus (true); (AuditEvent) event ). setAdvice ("school review opinion: creative, great! ") ;}@ Override public int getOrder () {return 1; // obtain the priority of the school listener (Review)} @ Override // This is one of our listener intelligence, ability to dynamically listen to public boolean supportsEventType (Class
  EventType) {return eventType = AuditEvent. class;} @ Override // This is the second part of our listener intelligence. It can dynamically listen to public boolean supportsSourceType (Class
  SourceType) {return sourceType = XiaoBai. class ;}} /****************** provincial audit listener ****************** /public class ProvinceListener implements SmartApplicationListener {@ Override public void onApplicationEvent (ApplicationEvent event) {if (AuditEvent) event ). getStatus () {// if the upper layer passes the System review. out. println ("Get current reporting status:" + (AuditEvent) event ). getStatus () + "--" + (AuditEvent) event ). getAdvice (); (AuditEvent) e Vent). setStatus (true); (AuditEvent) event). setAdvice ("Provincial review comments: Okay, pass it! ") ;}@ Override public int getOrder () {return 2 ;}@ Override public boolean supportsEventType (Class
  EventType) {return eventType = AuditEvent. class;} @ Override public boolean supportsSourceType (Class
  SourceType) {return sourceType = XiaoBai. class ;}} /****************** National Audit listener ****************** /public class CountryListener implements SmartApplicationListener {@ Override public void onApplicationEvent (ApplicationEvent event) {if (AuditEvent) event ). getStatus () {// if the upper layer passes the System review. out. println ("Get current reporting status:" + (AuditEvent) event ). getStatus () + "--" + (AuditEvent) event ). getAdvice (); (AuditEvent) e Vent). setStatus (true); (AuditEvent) event). setAdvice ("national review opinion: Generally, barely pass it! ") ;}@ Override public int getOrder () {return 3 ;}@ Override public boolean supportsEventType (Class
  EventType) {return eventType = AuditEvent. class;} @ Override public boolean supportsSourceType (Class
  SourceType) {return sourceType = XiaoBai. class ;}}

Here, the configuration of our instance is simplified for convenience of demonstration. Our release source supports the following types: Xiao Bai. Based on our actual needs, we can make Xiao Bai inherit the Student class and then make sourceType a subclass of Student, in this way, students who have inherited Student can submit their applications.
In the listener that implements SmartApplicationListener, we rewrite the GetOrder method to modify the order of different listeners,The smaller the priority, the more called. By configuring different priorities and blocking calls between listeners. We can achieveStreamlined ordered event callingWhich makes sense in actual application scenarios.

5. register the listener in the IOC container

  
   
    
     
      
     
    
   
  
6. Test Method
Public class MainTest {public static void main (String args []) throws InterruptedException {ApplicationContext ac = new ClassPathXmlApplicationContext ("classpath: test/event2/event. xml "); XiaoBai xiaobai = (XiaoBai) ac. getBean ("xiaoBai"); xiaobai. reportWorks (); // }}

Call the test code and we get the running result:
Get the current reporting status: true -- not reviewed
Get the current application status: true -- school review opinion: creative, great!
Obtain the current declaration status: true -- provincial review comments: OK, pass it!
Final review result: true -- national review opinion: Generally, barely pass it!

So Mr. Bai's work finally won the national innovation science and technology award, which made him happy for a while.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.