2.2.7 トランザクションの伝搬
トランザクションの伝搬をするアプリケーションの開発方法について説明します。
トランザクションの伝搬は、Orchestrator、Entity-Serviceのアプリケーションで実装してください。
トランザクションの伝搬は、Entity-serviceの呼び出しに使用するクライアントによって設定方法が異なります。使用するクライアントに対応する参照先を参照してください。
|
項番 |
トランザクションの伝搬に使用するクライアント |
参照先 |
|---|---|---|
|
1 |
Spring FrameworkのRestTemplate |
|
|
2 |
HMP-ADIFのISCコマンド(ImperativeかつREST通信) |
(1) Spring FrameworkのRestTemplateを使用する場合
TxPropagationRestTemplateInterceptorクラスをorg.springframework.web.client.RestTemplateクラスのsetInterceptors()メソッド、またはorg.springframework.boot.web.client.RestTemplateBuilderクラスのinterceptors()メソッドで登録して使用します。
(a) ライブラリの組み込み
TxPropagationRestTemplateInterceptorクラスを登録するには、hmppcto-propagation-rest-templateライブラリをアプリケーションに組み込む必要があります。Gradleで組み込む記載例を次に示します。
dependencies {
implementation 'jp.co.Hitachi.soft.hmppcto:hmppcto-propagation-rest-template:<version>'
implementation 'jp.co.Hitachi.soft.hmppcto:hmppcto-common:<version>'
}(b) コーディング例
org.springframework.web.client.RestTemplateクラスのsetInterceptors()メソッドを使用して登録する例を説明します。アプリケーションが使用するほかのインタセプタとの登録順番に制約はありません。
TxPropagationRestTemplateInterceptorクラスのインスタンスは@AutowiredアノテーションでDIして取得してください。DIしたインスタンスを使用するメソッドのアクセス制御修飾子は、private以外にしてください。privateとした場合は、HMP-PCTOの機能は正しく動作しません。
@Autowired TxPropagationRestTemplateInterceptor txIc; RestTemplate restTemplate = new RestTemplate(); // RestTemplateのインスタンス作成 List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); // インタセプタのリストを作成 interceptors.add(txIc); // HMP-PCTOのインタセプタをリストに追加 restTemplate.setInterceptors(interceptors); // RestTemplateにインタセプタを登録
(c) エラーハンドリング
TxPropagationRestTemplateInterceptorクラスでは、アプリケーションでエラーハンドリングが必要なエラーが発生することはありません。
RestTemplateを使用した通信で例外が発生した場合に、例外のCausedに次のようにHMP-PCTOのTxPropagationRestTemplateInterceptorクラスで例外が発生したことを示すスタックトレースが出力されます。これは、例外が発生した直接の原因を示すものではありません。直接の原因は、例外のCausedの前に出力された内容を参照してください。
(2) HMP-ADIFのISCコマンド(ImperativeかつREST通信)を使用する場合
TxPropagationIscImperativePluginクラスをアプリケーションに組み込んで使用します。TxPropagationIscImperativePluginクラスは、HMP-ADIFのorg.globallogic.microservice.modules.isc.imperative.IscImperativeCommandExecutionPluginインタフェースを実装しています。アプリケーションに組み込むとアプリケーション起動時にHMP-ADIFが自動的に登録します。
(a) ライブラリの組み込み
TxPropagationIscImperativePluginクラスを登録するには、hmppcto-propagation-isc-imperativeライブラリをアプリケーションに組み込む必要があります。Gradleで組み込む記載例を次に示します。
dependencies {
implementation 'jp.co.Hitachi.soft.hmppcto:hmppcto-propagation-isc-imperative:<version>'
implementation 'jp.co.Hitachi.soft.hmppcto:hmppcto-common:<version>'
}(b) コーディング例
アプリケーション起動時にHMP-ADIFが自動的に登録するため、コーディングは不要です。
(c) エラーハンドリング
TxPropagationIscImperativePluginクラスでは、アプリケーションでエラーハンドリングが必要なエラーが発生することはありません。