java - ThreadPoolTaskExecutor keep running, how to control shutdown? -
i writing spring boot app threadpooltaskexecutor. here application class:
import info.gandraweb.apns.service.messageservice; import info.gandraweb.apns.settings.appsettings; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.aop.interceptor.asyncuncaughtexceptionhandler; import org.springframework.aop.interceptor.simpleasyncuncaughtexceptionhandler; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.enableautoconfiguration; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.context.applicationcontext; import org.springframework.scheduling.annotation.asyncconfigurer; import org.springframework.scheduling.annotation.enableasync; import org.springframework.scheduling.annotation.enablescheduling; import org.springframework.scheduling.concurrent.threadpooltaskexecutor; import java.util.concurrent.executor; @springbootapplication @enablescheduling @enableautoconfiguration @enableasync public class application implements asyncconfigurer{ private static final logger logger = loggerfactory.getlogger(application.class); @autowired private appsettings appsettings; public static void main(string[] args) { applicationcontext applicationcontext = springapplication.run(application.class, args); appsettings appsettings = applicationcontext.getbean(appsettings.class); logger.info(appsettings.tostring()); test(applicationcontext); } @override public executor getasyncexecutor() { threadpooltaskexecutor taskexecutor = new threadpooltaskexecutor(); taskexecutor.setthreadnameprefix("myexecutor-"); taskexecutor.setcorepoolsize(10); taskexecutor.setmaxpoolsize(50); taskexecutor.setqueuecapacity(1000); taskexecutor.setwaitfortaskstocompleteonshutdown(true); taskexecutor.initialize(); return taskexecutor; } @override public asyncuncaughtexceptionhandler getasyncuncaughtexceptionhandler() { return new simpleasyncuncaughtexceptionhandler(); } private static void test(applicationcontext applicationcontext) { messageservice messageservice = applicationcontext.getbean(messageservice.class); messageservice.processnewmessages(); } }**
inside test method calling
messageservice.processnewmessages()
which, in turn, if exists task in db call async method on bean process it. when there no tasks processing app finishes.
if there tasks in db processing after execution app keep running , never finish .... seems shutdown() method on threadpooltaskexecutor never called?
then have implemented applicationlistener interface , logged going on inside onapplicationevent(contextclosedevent)
so when there no task processing app finish , contextclosedevent triggered. if there tasks in db processing after execution contextclosedevent not triggered???
when remove asyncconfigurer app contextclosedevent trigered , app shutdown. in such case have lost posibility configure threadpooltaskexecutor.
so, how can configure threadpooltaskexecutor , manage gracefully shutdown executor, or trigger contextclosedevent ... whatever preferred way gracefully shutdown it?
Comments
Post a Comment