Outbound Gateway
出站网关类似于出站和入站适配器的组合:其作用是处理消息并使用它来执行 SQL 查询,然后通过将其发送到答复通道来将结果作为答复返回。默认情况下,消息有效负载和标头可用作查询的输入参数,如下例所示:
The outbound gateway is like a combination of the outbound and inbound adapters: Its role is to handle a message and use it to execute a SQL query and then respond with the result by sending it to a reply channel. By default, the message payload and headers are available as input parameters to the query, as the following example shows:
<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" />
前一个示例的结果是将记录插入到 mythings
表中并返回一个消息,指示受到影响的行数(有效负载是一个映射:{UPDATED=1}
)到输出通道。
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"
添加到前面的示例中来使用生成键填充答复消息(这不是默认值,因为它不受某些数据库平台支持)。以下示例显示了更改后的配置:
If the update query is an insert with auto-generated keys, you can populate the reply message with the generated keys by adding keys-generated="true"
to the preceding example (this is not the default because it is not supported by some database platforms).
The following example shows the changed configuration:
<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"/>
除了更新计数或生成的键之外,你还可以提供一个选择查询来执行并从结果中生成答复消息(例如入站适配器),如下例所示:
Instead of the update count or the generated keys, you can also provide a select query to execute and generate a reply message from the result (such as the inbound adapter), as the following example shows:
<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"/>
自 Spring Integration 2.2 起,更新 SQL 查询不再是强制性的。你现在可以使用 query
属性或 query
元素仅提供一个选择查询。如果你需要使用通用网关或有效负载增强器等来主动检索数据,这非常有用。然后从结果生成答复消息(类似于入站适配器的工作方式)并将其传递到答复通道。以下示例显示了如何使用 query
属性:
Since Spring Integration 2.2, the update SQL query is 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
查询的组件仅从游标中返回一行(第一行)。你可以使用 max-rows
选项调整此行为。如果你需要从 SELECT 返回所有行,请考虑指定 max-rows="0"
。
By default, the component for the SELECT
query returns only one (the first) row from the cursor.
You can adjust this behavior with the max-rows
option.
If you need to return all the rows from the SELECT, consider specifying max-rows="0"
.
与通道适配器一样,你还可以为请求和答复提供 SqlParameterSourceFactory
实例。默认值与出站适配器相同,因此请求消息可作为表达式的根。如果 keys-generated="true"
,表达式的根是生成键(如果只有一个,则为映射;如果多值,则为映射列表)。以下示例显示了更改后的配置:
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 multi-valued).
出站网关需要对 DataSource
或 JdbcTemplate
的引用。它还可以注入一个 SqlParameterSourceFactory
来控制传入消息与查询的绑定。
The outbound gateway requires a reference to either a DataSource
or a JdbcTemplate
.
It can also have a SqlParameterSourceFactory
injected to control the binding of the incoming message to the query.
从 4.2 版本开始,request-prepared-statement-setter
属性在 <int-jdbc:outbound-gateway>
中可用,作为 request-sql-parameter-source-factory
的替代项。它允许您指定 MessagePreparedStatementSetter
bean 引用,它在执行之前实现了更复杂的 PreparedStatement
准备。
Starting with the version 4.2, the request-prepared-statement-setter
attribute is available on the <int-jdbc:outbound-gateway>
as an alternative to request-sql-parameter-source-factory
.
It lets you specify a MessagePreparedStatementSetter
bean reference, which implements more sophisticated PreparedStatement
preparation before its execution.
从 6.0 版本开始,{JdbcOutboundGateway
} 返回一个空的列表结果原样,而不是将其转化为 {null
},就像以前一样,含义为“无答复”。这导致了在处理空列表是下游逻辑的一部分的应用程序中的额外的配置。有关可能的空列表处理选择,请参见 {Splitter Discard Channel}。
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 Splitter Discard Channel for possible empty list handling option.
有关 MessagePreparedStatementSetter
的更多信息,请参阅 Outbound Channel Adapter。
See Outbound Channel Adapter for more information about MessagePreparedStatementSetter
.