送信ゲートウェイ

送信ゲートウェイは、送信アダプターと受信アダプターの組み合わせに似ています。そのロールは、メッセージを処理し、それを使用して SQL クエリを実行し、応答チャネルに送信することで結果で応答することです。デフォルトでは、次の例に示すように、メッセージペイロードとヘッダーはクエリへの入力パラメーターとして使用できます。

<int-jdbc:outbound-gateway
    update="insert into mythings (id, status, name) values (:headers[id], 0, :payload[thing])"
    request-channel="input" reply-channel="output" data-source="dataSource" />

The result of the preceding example is to insert a record into the mythings table and return a message that indicates the number of rows affected (the payload is a map: {UPDATED=1}) to the output channel.

更新クエリが自動生成キーの挿入である場合、keys-generated="true" を前述の例に追加することにより、生成されたキーを応答メッセージに追加できます(一部のデータベースプラットフォームではサポートされていないため、これはデフォルトではありません)。次の例は、変更された構成を示しています。

<int-jdbc:outbound-gateway
    update="insert into mythings (status, name) values (0, :payload[thing])"
    request-channel="input" reply-channel="output" data-source="dataSource"
    keys-generated="true"/>

次の例に示すように、更新カウントまたは生成されたキーの代わりに、選択クエリを提供して、結果(受信アダプターなど)から応答メッセージを実行および生成することもできます。

<int-jdbc:outbound-gateway
    update="insert into foos (id, status, name) values (:headers[id], 0, :payload[foo])"
    query="select * from foos where id=:headers[$id]"
    request-channel="input" reply-channel="output" data-source="dataSource"/>

Since Spring Integration 2.2, the update SQL query has been no longer mandatory. You can now provide only a select query, by using either the query attribute or the query element. This is extremely useful if you need to actively retrieve data by using, for example, a generic gateway or a payload enricher. The reply message is then generated from the result (similar to how the inbound adapter works) and passed to the reply channel. The following example show to use the query attribute:

<int-jdbc:outbound-gateway
    query="select * from foos where id=:headers[id]"
    request-channel="input"
    reply-channel="output"
    data-source="dataSource"/>

デフォルトでは、SELECT クエリのコンポーネントは、カーソルから 1(最初の)行のみを返します。この動作は、max-rows オプションを使用して調整できます。SELECT からすべての行を返す必要がある場合は、max-rows="0" の指定を検討してください。

As with the channel adapters, you can also provide SqlParameterSourceFactory instances for request and reply. The default is the same as for the outbound adapter, so the request message is available as the root of an expression. If keys-generated="true", the root of the expression is the generated keys (a map if there is only one or a list of maps if multivalued).

送信ゲートウェイには、DataSource または JdbcTemplate への参照が必要です。また、受信メッセージのクエリへのバインドを制御するために、SqlParameterSourceFactory を挿入することもできます。

バージョン 4.2 以降、request-prepared-statement-setter 属性は request-sql-parameter-source-factory の代替として <int-jdbc:outbound-gateway> で使用可能です。MessagePreparedStatementSetter Bean 参照を指定できます。これにより、実行前に、より洗練された PreparedStatement 準備が実装されます。

Starting with the version 6.0, the JdbcOutboundGateway returns an empty list result as is instead of converting it to null as it was before with the meaning "no reply". This caused an extra configuration in applications where handling of empty lists is a part of downstream logic. See スプリッタ廃棄チャネル for a possible empty list handling option.

MessagePreparedStatementSetter の詳細については、送信チャネルアダプターを参照してください。