Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I ensure that one of my Spring ApplicationListeners gets executed last?

Tags:

I have several services that are listening for Spring events to make changes to my underlying data model. These all work by implementing ApplicationListener<Foo>. Once all of the Foo listeners modify the underlying data model, my user interface needs to refresh to reflect the changes (think fireTableDataChanged()).

Is there any way to ensure that a specific listener for Foo is always last? Or is there any way to call a function when all other listeners are done? I'm using annotation based wiring and Java config, if that matters.

like image 664
Luke Avatar asked Jun 05 '12 13:06

Luke


1 Answers

All your beans implementing ApplicationListener should also implement Ordered and provide reasonable order value. The lower the value, the sooner your listener will be invoked:

class FirstListener implements ApplicationListener<Foo>, Ordered {     public int getOrder() {         return 10;     }     //... }  class SecondListener implements ApplicationListener<Foo>, Ordered {     public int getOrder() {         return 20;     }     //... }  class LastListener implements ApplicationListener<Foo>, Ordered {     public int getOrder() {         return LOWEST_PRECEDENCE;     }     //... } 

Moreover you can implement PriorityOrdered to make sure one of your listeners is always invoked first.

like image 80
Tomasz Nurkiewicz Avatar answered Sep 26 '22 14:09

Tomasz Nurkiewicz