最新の安定バージョンについては、Spring Integration 7.1.0 を使用してください!

JDBC コンポーネント用の Java DSL

バージョン 7.0 では、JDBC モジュールにチャネルアダプター用の Java DSL API が導入されました。Java DSL の中心となるクラス(通常は開始点)は org.springframework.integration.jdbc.dsl.Jdbc ファクトリです。これは、対象のチャネルアダプターまたはゲートウェイの設定を開始するための、説明の必要がないメソッドを提供します。すぐに使用可能なチャネルアダプター用の標準的な IntegrationComponentSpec 実装は次のとおりです。

  • JdbcInboundChannelAdapterSpec extends MessageSourceSpec<JdbcInboundChannelAdapterSpec, JdbcPollingChannelAdapter>

  • JdbcOutboundChannelAdapterSpec extends MessageHandlerSpec<JdbcOutboundChannelAdapterSpec, JdbcMessageHandler>

  • JdbcOutboundGatewaySpec extends MessageHandlerSpec<JdbcOutboundGatewaySpec, JdbcOutboundGateway>

  • JdbcStoredProcInboundChannelAdapterSpec extends MessageSourceSpec<JdbcStoredProcInboundChannelAdapterSpec, StoredProcPollingChannelAdapter>

  • JdbcStoredProcOutboundChannelAdapterSpec extends MessageHandlerSpec<JdbcStoredProcOutboundChannelAdapterSpec, StoredProcMessageHandler>

  • JdbcStoredProcOutboundGatewaySpec extends MessageHandlerSpec<JdbcStoredProcOutboundGatewaySpec, StoredProcOutboundGateway>

さらに、StoredProcExecutor の作成と構成のために、便利なビルダーのようなコンポーネントである StoredProcExecutorSpec も提供されています。

Jdbc ファクトリを IntegrationFlow の構成に使用する方法の例をいくつか示します。

@Bean
public DataSource h2DataSource() {
    return new EmbeddedDatabaseBuilder()
            .setType(EmbeddedDatabaseType.H2)
            .addScripts("classpath:dsl-h2.sql", "classpath:h2-stored-procedures.sql")
            .build();
}

@Bean
public IntegrationFlow outboundFlow(DataSource h2DataSource) {
    return flow -> flow
            .handle(Jdbc.outboundAdapter(h2DataSource,
                            "insert into outbound (id, status, name) values (1, 0, ?)")
                    .preparedStatementSetter((ps, requestMessage) ->
                            ps.setObject(1, requestMessage.getPayload()))
                    .usePayloadAsParameterSource(false)
                    .keysGenerated(false));
}

@Bean
public IntegrationFlow storedProcInboundFlow(DataSource h2DataSource) {
    return IntegrationFlow.from(Jdbc.storedProcInboundAdapter(h2DataSource)
                            .expectSingleResult(true)
                            .configurerStoredProcExecutor(configurer -> configurer
                                    .ignoreColumnMetaData(true)
                                    .isFunction(false)
                                    .storedProcedureName("GET_PRIME_NUMBERS")
                                    .procedureParameter(new ProcedureParameter("beginRange", 1, null))
                                    .procedureParameter(new ProcedureParameter("endRange", 10, null))
                                    .sqlParameter(new SqlParameter("beginRange", Types.INTEGER))
                                    .sqlParameter(new SqlParameter("endRange", Types.INTEGER))
                                    .returningResultSetRowMapper("out", new PrimeMapper())
                            ),
                    e -> e.poller(p -> p.trigger(new OnlyOnceTrigger())))
            .channel(c -> c.queue("storedProcInboundPollerChannel"))
            .get();
}

この Java DSL API は、Kotlin および Groovy DSL でそのまま使用できます。