1. 概要

Spring Bootアプリケーションのライフサイクルを管理することは、本番環境に対応したシステムにとって非常に重要です。 Springコンテナは、 ApplicationContext。を使用して、すべてのBeanの作成、初期化、および破棄を処理します。

この記事の強調は、ライフサイクルの破壊段階です。 具体的には、Spring Bootアプリケーションをシャットダウンするさまざまな方法を見ていきます。

Spring Bootを使用してプロジェクトを設定する方法の詳細については、 Spring Boot Starter の記事を確認するか、 Spring BootConfigurationを参照してください。

2. シャットダウンエンドポイント

デフォルトでは、 /shutdownを除くすべてのエンドポイントがSpringBootApplicationで有効になっています。 これは当然、アクチュエータエンドポイントの一部です。

これらを設定するためのMavenの依存関係は次のとおりです。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

また、セキュリティサポートも設定する場合は、次のものが必要です。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

最後に、application.propertiesファイルでシャットダウンエンドポイントを有効にします。

management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true
endpoints.shutdown.enabled=true

使用するアクチュエータエンドポイントも公開する必要があることに注意してください。 上記の例では、 /shutdownエンドポイントを含むすべてのアクチュエータエンドポイントを公開しました。

Spring Bootアプリケーションをシャットダウンするには、次のようなPOSTメソッドを呼び出すだけです

curl -X POST localhost:port/actuator/shutdown

この呼び出しでは、ポートはアクチュエータポートを表します。

3. アプリケーションコンテキストを閉じる

アプリケーションコンテキストを使用して、 close()メソッドを直接呼び出すこともできます。

コンテキストを作成して閉じる例から始めましょう。

ConfigurableApplicationContext ctx = new 
  SpringApplicationBuilder(Application.class).web(WebApplicationType.NONE).run();
System.out.println("Spring Boot application started");
ctx.getBean(TerminateBean.class);
ctx.close();

これにより、すべてのBeanが破棄され、ロックが解除されてから、Beanファクトリが閉じられます。 アプリケーションのシャットダウンを確認するために、@PreDestroyアノテーションを付けたSpringの標準ライフサイクルコールバックを使用します。

public class TerminateBean {

    @PreDestroy
    public void onDestroy() throws Exception {
        System.out.println("Spring Container is destroyed!");
    }
}

このタイプのBeanも追加する必要があります。

@Configuration
public class ShutdownConfig {

    @Bean
    public TerminateBean getTerminateBean() {
        return new TerminateBean();
    }
}

この例を実行した後の出力は次のとおりです。

Spring Boot application started
Closing AnnotationConfigApplicationContext@39b43d60
DefaultLifecycleProcessor - Stopping beans in phase 0
Unregistering JMX-exposed beans on shutdown
Spring Container is destroyed!

ここで覚えておくべき重要なこと:アプリケーションコンテキストを閉じている間、親コンテキストは個別のライフサイクルのために影響を受けません。

3.1. 現在のアプリケーションコンテキストを閉じる

上記の例では、子アプリケーションコンテキストを作成し、 close()メソッドを使用して破棄しました。

現在のコンテキストを閉じたい場合、1つの解決策は、アクチュエータ /shutdownエンドポイントを呼び出すことです。

ただし、独自のカスタムエンドポイントを作成することもできます。

@RestController
public class ShutdownController implements ApplicationContextAware {
    
    private ApplicationContext context;
    
    @PostMapping("/shutdownContext")
    public void shutdownContext() {
        ((ConfigurableApplicationContext) context).close();
    }

    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        this.context = ctx;
        
    }
}

ここでは、 ApplicationContextAware インターフェイスを実装し、setterメソッドをオーバーライドして現在のアプリケーションコンテキストを取得するコントローラーを追加しました。 次に、マッピングメソッドでは、単に close()メソッドを呼び出しています。

次に、新しいエンドポイントを呼び出して、現在のコンテキストをシャットダウンできます。

curl -X POST localhost:port/shutdownContext

もちろん、実際のアプリケーションにこのようなエンドポイントを追加する場合は、それも保護する必要があります。

4. SpringApplicationを終了します

SpringApplication は、 shutdown フックをJVMに登録して、アプリケーションが適切に終了することを確認します。

Beanは、 ExitCodeGenerator インターフェースを実装して、特定のエラーコードを返す場合があります。

ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class)
  .web(WebApplicationType.NONE).run();

int exitCode = SpringApplication.exit(ctx, new ExitCodeGenerator() {
@Override
public int getExitCode() {
        // return the error code
        return 0;
    }
});

System.exit(exitCode);

Java 8ラムダのアプリケーションと同じコード:

SpringApplication.exit(ctx, () -> 0);

System.exit(exitCode)を呼び出した後、プログラムは0の戻りコードで終了します

Process finished with exit code 0

5. アプリプロセスを強制終了します

最後に、bashスクリプトを使用して、アプリケーションの外部からSpringBootアプリケーションをシャットダウンすることもできます。 このオプションの最初のステップは、アプリケーションコンテキストにPIDをファイルに書き込ませることです。

SpringApplicationBuilder app = new SpringApplicationBuilder(Application.class)
  .web(WebApplicationType.NONE);
app.build().addListeners(new ApplicationPidFileWriter("./bin/shutdown.pid"));
app.run();

次に、次の内容のshutdown.batファイルを作成します。

kill $(cat ./bin/shutdown.pid)

shutdown.bat を実行すると、 shutdown.pid ファイルからプロセスIDが抽出され、killコマンドを使用してブートアプリケーションが終了します。

6. 結論

この簡単な記事では、実行中のSpring Bootアプリケーションをシャットダウンするために使用できるいくつかの簡単な方法について説明しました。

適切な方法を選択するのは開発者次第ですが、 これらの方法はすべて、設計上および意図的に使用する必要があります。

たとえば、 .exit()は、エラーコードを別の環境(JVMなど)に渡す必要がある場合に適しています。 アプリケーションPIDを使用すると、bashスクリプトを使用してアプリケーションを起動または再起動できるため、柔軟性が向上します。

最後に、 / shutdown は、HTTPを介して外部でアプリケーションを終了できるようにするためのものです。 他のすべての場合、 .close()は完全に機能します。

いつものように、この記事の完全なコードはGitHubプロジェクトで入手できます。