|
对于最新稳定版本,请使用 Spring Integration 7.0.0! |
入站通道适配器
入站通道适配器用于使用 JPA QL 对数据库执行选择查询并返回结果。
消息有效载荷要么是一个实体,要么是列表存在。
以下 XML 配置为入站信道适配器:
<int-jpa:inbound-channel-adapter channel="inboundChannelAdapterOne" (1)
entity-manager="em" (2)
auto-startup="true" (3)
query="select s from Student s" (4)
expect-single-result="true" (5)
max-results="" (6)
max-results-expression="" (7)
delete-after-poll="true" (8)
flush-after-delete="true"> (9)
<int:poller fixed-rate="2000" >
<int:transactional propagation="REQUIRED" transaction-manager="transactionManager"/>
</int:poller>
</int-jpa:inbound-channel-adapter>
| 1 | 所覆盖的通道入站信道适配器在执行JPA QL后,将消息(带载荷)放入查询属性。 |
| 2 | 这实体管理器实例用于执行所需的JPA作。 |
| 3 | 属性表示组件是否应在应用上下文启动时自动启动。该值默认为true. |
| 4 | JPA QL 的结果作为消息的有效载荷发送 |
| 5 | 该属性告诉JPQL查询是否给出结果中的单个实体,还是列表实体的集合。如果 值被设置为true,单一实体作为消息的有效载荷发送。但如果将此设置为 后返回多个结果true一个消息异常掷出。值默认为false. |
| 6 | 这个非零、非负的整数值告诉适配器在执行选择作时不要选择超过给定的行数。默认情况下,如果未设置该属性,查询会选择所有可能的记录。该属性与 是互斥的最大结果表达式. 自选。 |
| 7 | 一个用来求解结果集中最大结果数的表达式。互斥最大成绩. 自选。 |
| 8 | 将此值设为true如果你想删除查询执行后收到的行,你必须确保该组件作为事务的一部分运行。否则,你可能会遇到以下异常:java.lang.IllegalArgumentException:移除分离实例 ... |
| 9 | 将此值设为true如果你想在删除已接收实体后立即冲洗持久性上下文,并且不想依赖冲洗模式关于实体管理器. 该值默认为false. |
配置参数参考
以下列表展示了所有可以设置的值入站信道适配器:
<int-jpa:inbound-channel-adapter
auto-startup="true" (1)
channel="" (2)
delete-after-poll="false" (3)
delete-per-row="false" (4)
entity-class="" (5)
entity-manager="" (6)
entity-manager-factory="" (7)
expect-single-result="false" (8)
id=""
jpa-operations="" (9)
jpa-query="" (10)
named-query="" (11)
native-query="" (12)
parameter-source="" (13)
send-timeout=""> (14)
<int:poller ref="myPoller"/>
</int-jpa:inbound-channel-adapter>
| 1 | 该生命周期属性表示该组件在应用上下文启动时是否应自动启动。该属性默认为true. 自选。 |
| 2 | 适配器发送包含有效载荷消息的通道,该通道是执行所需JPA作后产生的。 |
| 3 | 一个布尔标志,指示适配器轮询后是否删除所选记录。默认值为false(即记录不会被删除)。你必须确保组件作为事务的一部分运行。否则,你可能会遇到异常,例如:java.lang.IllegalArgumentException:移除分离实例 .... 自选。 |
| 4 | 一个布尔标志,指示记录是否可以批量删除,还是必须逐条删除。默认值为false(也就是说,记录可以批量删除。) 自选。 |
| 5 | 从数据库中查询的实体类别的完全限定名称。适配器会自动基于实体类名称构建JPA查询。 自选。 |
| 6 | 一个实例jakarta.persistence.EntityManager用于执行JPA作。 自选。 |
| 7 | 一个实例jakarta.persistence.EntityManagerFactory用于获得jakarta.persistence.EntityManager执行JPA作。 自选。 |
| 8 | 一个布尔标志,表示选择作是否预期返回单个结果,还是列表结果。如果该标志设置为true,被选中的单个实体作为消息的有效载荷发送。如果返回多个实体,则抛出异常。 如果false这列表作为消息的有效载荷发送。该值默认为false. 自选。 |
| 9 | 一个实现org.springframework.integration.jpa.core.JpaOperations用于执行 JPA作。我们建议不要提供自己的实现,而是使用默认的org.springframework.integration.jpa.core.DefaultJpaOperations实现。 你可以使用任何一种实体管理器,实体管理工厂或JPA作属性。 自选。 |
| 10 | JPA QL 将由该适配器执行。 自选。 |
| 11 | 该适配器需要执行的命名查询。 自选。 |
| 12 | 该适配器执行的本地查询。您可以使用任何JPA-查询,命名查询,实体类或native-query属性。 自选。 |
| 13 | 一个实现o.s.i.jpa.support.parametersource.ParameterSource用于解析查询中参数的值。如果实体类属性有一个值。 自选。 |
| 14 | 向信道发送消息时等待的最大时间(以毫秒计)。 自选。 |
使用 Java 配置配置
以下 Spring Boot 应用程序展示了如何用 Java 配置入站适配器的示例:
@SpringBootApplication
@EntityScan(basePackageClasses = StudentDomain.class)
public class JpaJavaApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(JpaJavaApplication.class)
.web(false)
.run(args);
}
@Autowired
private EntityManagerFactory entityManagerFactory;
@Bean
public JpaExecutor jpaExecutor() {
JpaExecutor executor = new JpaExecutor(this.entityManagerFactory);
jpaExecutor.setJpaQuery("from Student");
return executor;
}
@Bean
@InboundChannelAdapter(channel = "jpaInputChannel",
poller = @Poller(fixedDelay = "${poller.interval}"))
public MessageSource<?> jpaInbound() {
return new JpaPollingChannelAdapter(jpaExecutor());
}
@Bean
@ServiceActivator(inputChannel = "jpaInputChannel")
public MessageHandler handler() {
return message -> System.out.println(message.getPayload());
}
}
使用 Java DSL 配置
以下 Spring Boot 应用程序展示了如何用 Java DSL 配置入站适配器的示例:
@SpringBootApplication
@EntityScan(basePackageClasses = StudentDomain.class)
public class JpaJavaApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(JpaJavaApplication.class)
.web(false)
.run(args);
}
@Autowired
private EntityManagerFactory entityManagerFactory;
@Bean
public IntegrationFlow pollingAdapterFlow() {
return IntegrationFlow
.from(Jpa.inboundAdapter(this.entityManagerFactory)
.entityClass(StudentDomain.class)
.maxResults(1)
.expectSingleResult(true),
e -> e.poller(p -> p.trigger(new OnlyOnceTrigger())))
.channel(c -> c.queue("pollingResults"))
.get();
}
}