Gracefull关闭Spring启动应用程序

时间:2015-05-30 08:36:40

标签: spring spring-boot application-shutdown

我正在运行一个spring-boot应用程序,一切都很好,除非我试图关闭它。我在下面的代码中遇到错误

    while (true) {
        try {
            if(level2List == null)
                break;
            CDR cdr = level2List.poll(2, TimeUnit.SECONDS);

在最后一行产生错误,spring boot在2秒等待完成之前关闭level2List

2015-05-29 17:32:15.758  INFO 27390 --- [       Thread-1] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@358ee631: startup date [Fri May 29 17:31:17 GMT 2015]; root of context hierarchy
2015-05-29 17:32:15.765  INFO 27390 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2015-05-29 17:32:15.766  INFO 27390 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans
2015-05-29 17:32:15.780  INFO 27390 --- [       Thread-1] o.s.c.ehcache.EhCacheManagerFactoryBean  : Shutting down EhCache CacheManager
2015-05-29 17:32:15.804  INFO 27390 --- [       Thread-1] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'level2ES'
2015-05-29 17:32:15.815  INFO 27390 --- [       Thread-1] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'level1ES'
2015-05-29 17:32:15.823  INFO 27390 --- [       Thread-1] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2015-05-29 17:32:15.825  INFO 27390 --- [       Thread-1] com.jolbox.bonecp.BoneCP                 : Shutting down connection pool...
2015-05-29 17:32:15.833  INFO 27390 --- [       Thread-1] com.jolbox.bonecp.BoneCP                 : Connection pool has been shutdown.
2015-05-29 17:32:15.848 ERROR 27390 --- [pool-2-thread-1] c.t.t.c.process.CDRDataBase     : Error

java.lang.InterruptedException: null
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:2014)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2088)
        at java.util.concurrent.LinkedBlockingQueue.poll(LinkedBlockingQueue.java:467)
        at com.teltacworldwide.tekram.cdrserver.process.CDRDataBaseNative.run(CDRDataBaseNative.java:79)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:483)
        at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
        at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)

有没有办法订购关机程序或确保我在关闭之前没有使用level2List?

BR Shahbour

1 个答案:

答案 0 :(得分:0)

我做了两件事

添加

@Bean(destroyMethod = "shutdown")
public ThreadPoolTaskExecutor level1ES() {

    } catch (InterruptedException e) {
        log.warn("Closing application while CDR still in queue");           }

BR Shahbour