此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Integration 6.5.1! |
R2DBC 支持
Spring Integration 提供了通道适配器,用于通过 R2DBC 驱动程序使用对数据库的响应式访问来接收和发送消息。
您需要将此依赖项包含在您的项目中:
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-r2dbc</artifactId>
<version>6.3.12-SNAPSHOT</version>
</dependency>
compile "org.springframework.integration:spring-integration-r2dbc:6.3.12-SNAPSHOT"
R2DBC 入站通道适配器
这R2dbcMessageSource
是可轮询的MessageSource
基于R2dbcEntityOperations
并生成带有Flux
或Mono
作为从数据库获取的数据的有效负载,根据expectSingleResult
选择。
查询到SELECT
可以静态提供或基于 SpEL 表达式,该表达式在每个receive()
叫。
这R2dbcMessageSource.SelectCreator
作为评估上下文的根对象存在,以允许使用StatementMapper.SelectSpec
流畅的 API。
默认情况下,此通道适配器将 select 中的记录映射到LinkedCaseInsensitiveMap
实例。
它可以定制,提供payloadType
选项,它下面由EntityRowMapper
基于this.r2dbcEntityOperations.getConverter()
. 这updateSql
是可选的,用于标记数据库中的读取记录以跳过后续轮询。
这UPDATE
作可提供BiFunction<DatabaseClient.GenericExecuteSpec, ?, DatabaseClient.GenericExecuteSpec>
将值绑定到UPDATE
基于SELECT
结果。
此通道适配器的典型配置可能如下所示:
@Bean
@InboundChannelAdapter("fromR2dbcChannel")
public R2dbcMessageSource r2dbcMessageSourceSelectMany() {
R2dbcMessageSource r2dbcMessageSource = new R2dbcMessageSource(this.r2dbcEntityTemplate,
"SELECT * FROM person WHERE name='Name'");
r2dbcMessageSource.setPayloadType(Person.class);
r2dbcMessageSource.setUpdateSql("UPDATE Person SET name='SomeOtherName' WHERE id = :id");
r2dbcMessageSource.setBindFunction(
(DatabaseClient.GenericExecuteSpec bindSpec, Person o) -> bindSpec.bind("id", o.getId()));}
return r2dbcMessageSource;
}
对于 Java DSL,此通道适配器的配置如下所示:
@Bean
IntegrationFlow r2dbcDslFlow(R2dbcEntityTemplate r2dbcEntityTemplate) {
return IntegrationFlow
.from(R2dbc.inboundChannelAdapter(r2dbcEntityTemplate,
(selectCreator) ->
selectCreator.createSelect("person")
.withProjection("*")
.withCriteria(Criteria.where("id").is(1)))
.expectSingleResult(true)
.payloadType(Person.class)
.updateSql("UPDATE Person SET id='2' where id = :id")
.bindFunction((DatabaseClient.GenericExecuteSpec bindSpec, Person o) ->
bindSpec.bind("id", o.getId())),
e -> e.poller(p -> p.fixedDelay(100)))
.handle((p, h) -> p)
.channel(MessageChannels.flux())
.get();
}
R2DBC 出站通道适配器
这R2dbcMessageHandler
是一个ReactiveMessageHandler
实现以执行INSERT
(默认),UPDATE
或DELETE
使用提供的R2dbcEntityOperations
. 这R2dbcMessageHandler.Type
可以静态配置,也可以通过针对请求消息的 SpEL 表达式进行配置。
要执行的查询可以基于tableName
,values
和criteria
表达式选项或(如果tableName
未提供),整个消息有效负载被视为org.springframework.data.relational.core.mapping.Table
实体来执行 SQL。
套餐org.springframework.data.relational.core.query
作为导入注册到 SpEL 评估上下文中,以便直接访问Criteria
流畅的 API,用于UPDATE
和DELETE
查询。
这valuesExpression
用于INSERT
和UPDATE
并且必须评估为Map
对于列值对,以针对请求消息在目标表中执行更改。
此通道适配器的典型配置可能如下所示:
@Bean
@ServiceActivator(inputChannel = "toR2dbcChannel")
public R2dbcMessageHandler r2dbcMessageHandler(R2dbcEntityTemplate r2dbcEntityTemplate) {
R2dbcMessageHandler messageHandler = new R2dbcMessageHandler(r2dbcEntityTemplate)
messageHandler.setValuesExpression(new FunctionExpression<Message<?>>(Message::getPayload));
messageHandler.setQueryType(R2dbcMessageHandler.Type.UPDATE);
messageHandler.setCriteriaExpression(
EXPRESSION_PARSER.parseExpression("T(Criteria).where('id).is(headers.personId)));
return messageHandler;
}
对于 Java DSL,此通道适配器的配置如下所示:
.handleReactive(R2dbc.outboundChannelAdapter(r2dbcEntityTemplate)
.queryType(R2dbcMessageHandler.Type.UPDATE)
.tableNameExpression("payload.class.simpleName")
.criteria((message) -> Criteria.where("id").is(message.getHeaders().get("personId")))
.values("{age:36}"))