Spring Boot Application的正常关机

Gracefull shutdown for Spring boot Application

我正在运行一个spring-boot应用程序,一切都很好,除了我尝试关闭它时。 我在以下代码上出错

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

错误发生在最后一行,Spring Boot在关闭2秒等待之前关闭了level2List,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
沙堡


我做了两件事

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

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

BR
沙堡