java - Bean Lifecycle Management Spring Boot -
i trying deploy spring boot application external tomcat instance, , running questions regarding how best manage instantiation of things.
as presently structured have along lines of
public class myclass extends springbootservletinitializer{ @bean public threadpool pool(){ return new threadpool(); } @bean public backgroundthread setupinbox() { backgroundthread inbox = new backgroundthread(pool()); inbox.start(); return inbox; } @override protected springapplicationbuilder configure(springapplicationbuilder application) { return application.sources(myclass.class); } public static void main(string[] args) throws exception { springapplication.run(myclass.class, args); } }
where backgroundthread thread listening amqp type messaging queue new jobs. know spring offers rabbitmq ways this, not using rabbit doesn't help.
the entire purpose of *.war file being deployed expose functionality wire via messaging, question best way instantiate, start , destroy backgroundthread through lifecycle of spring? xml configuration?
the jsr-250 @postconstruct , @predestroy annotations considered best practice receiving lifecycle callbacks in modern spring application. using these annotations means beans not coupled spring specific interfaces.
for details see section 7.9.8, “@postconstruct , @predestroy”
those annotation meant put on init , cleanup methods:
@postconstruct public void initafterstartup() { ... } @predestroy public void cleanupbeforeexit() { ... }
also useful :
each springapplication register shutdown hook jvm ensure applicationcontext closed gracefully on exit. standard spring lifecycle callbacks (such disposablebean interface, or @predestroy annotation) can used.
in addition, beans may implement org.springframework.boot.exitcodegenerator interface if wish return specific exit code when application ends.
Comments
Post a Comment