2015-08-03 213 views
1

我正在從春天啓動的關閉端點200響應,而我看到的是,應用程序上下文關閉不如預期,但隨後的JVM進程本身仍然活着,直到永遠。這是關閉端點的預期行爲,還是預計流程本身也會優雅地終止?Spring Boot shutdown端點應該關閉整個JVM進程還是關閉應用程序上下文?

http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html,它說,關閉端點「允許應用程序可以正常關閉(不是默認啓用)」。

+0

你將不得不改變你的問題站在自己的位置。詢問您要鏈接的原始問題的人不會看到這篇文章。儘管這裏應該有一個可行的問題。 –

回答

2

感謝斯特凡,我發現了什麼問題是防止JVM進程擊中/關機端點後終止。在我的一個依賴項中有一個ScheduledExecutor沒有被應用程序上下文關閉,並且它阻止了JVM進程關閉(即使在應用程序上下文關閉後)。我寫了一個簡單的例子來展示如何重現行爲,另一個例子展示瞭如何解決它。

這個例子,當你打/關機端點不會終止JVM進程:

@SpringBootApplication 
public class AppSpringConfiguration { 

    public static void main(String[] args) { 
     SpringApplication.run(AppSpringConfiguration.class); 
    } 

    @Bean 
    public ClassWithExecutor ce() { 
     return new ClassWithExecutor(); 
    } 

    @PostConstruct 
    public void startScheduledTask() { 
     ce().startScheduledTask(); 
    } 

    @RestController 
    public static class BusinessLogicController { 

     @RequestMapping(value = "/hi") 
     public String businessLogic() { 
      return "hi"; 
     } 
    }    

    public static class ClassWithExecutor { 
     ScheduledExecutorService es; 

     ClassWithExecutor() { 
      this.es = Executors.newSingleThreadScheduledExecutor(); 
     } 

     public void startScheduledTask() { 
      es.scheduleAtFixedRate(new Runnable() { 
       @Override 
       public void run() { 
        System.out.println("Printing this every minute"); 
       } 
      }, 0, 3, TimeUnit.SECONDS); 
     }  
    } 
} 

通過添加關機鉤關閉時,應用程序上下文被關閉,JVM進程現在被後終止ScheduledExecutor擊中/關機端點:

@SpringBootApplication 
public class AppSpringConfiguration { 

    public static void main(String[] args) { 
     SpringApplication.run(AppSpringConfiguration.class); 
    } 

    @Bean 
    public ClassWithExecutor ce() { 
     return new ClassWithExecutor(); 
    } 

    @Bean 
    ShutdownAction sa() { 
     return new ShutdownAction(ce()); 
    } 

    @PostConstruct 
    public void startScheduledTask() { 
     ce().startScheduledTask(); 
    } 

    @RestController 
    public static class BusinessLogicController { 

     @RequestMapping(value = "/hi") 
     public String businessLogic() { 
      return "hi"; 
     } 
    } 

    public static class ShutdownAction implements ApplicationListener<ContextClosedEvent> { 
     private ClassWithExecutor classWithExecutor; 

     ShutdownAction(ClassWithExecutor classWithExecutor) { 
      this.classWithExecutor = classWithExecutor; 
     } 

     @Override 
     public void onApplicationEvent(ContextClosedEvent event) { 
      classWithExecutor.shutdown(); 
     } 
    } 

    public static class ClassWithExecutor { 
     ScheduledExecutorService es; 

     ClassWithExecutor() { 
      this.es = Executors.newSingleThreadScheduledExecutor(); 
     } 

     public void startScheduledTask() { 
      es.scheduleAtFixedRate(new Runnable() { 
       @Override 
       public void run() { 
        System.out.println("Printing this every minute"); 
       } 
      }, 0, 3, TimeUnit.SECONDS); 
     } 

     public void shutdown() { 
      es.shutdownNow(); 
     } 
    } 
} 
0

你有一些防止JVM除了你的春天啓動應用程序退出。如果你沒有,並且你有一個演示問題的示例工程,那麼請創建一個問題,我們來看看。

除了使用關閉端點,您還可以使用spring-boot-maven-plugin從1.3開始,startstop目標將用於典型的集成測試場景。

0

如果你有一個安排執行程序運行,您應該指定銷燬方法:

@Bean(destroyMethod = "shutdown")