Inbound Channel Adapter
入站通道适配器用于使用 JPA QL 执行数据库选择查询并返回结果。消息有效负载可以是单个实体或实体的`List`。以下 XML 配置一个`inbound-channel-adapter`:
An inbound channel adapter is used to execute a select query over the database using JPA QL and return the result.
The message payload is either a single entity or a List
of entities.
The following XML configures an inbound-channel-adapter
:
<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 | 在 `query`属性中执行 JPA QL 后,`inbound-channel-adapter`放置消息(包含有效内容)的通道。 |
2 | The channel over which the inbound-channel-adapter puts the messages (with the payload) after executing the JPA QL in the query attribute. |
3 | 用于执行必需 JPA 操作的 `EntityManager`实例。 |
4 | The EntityManager instance used to perform the required JPA operations. |
5 | 一个属性,表示组件在应用程序上下文启动时是否应该自动启动。该值默认为 true . |
6 | Attribute signaling whether the component should automatically start when the application context starts.
The value defaults to true . |
7 | 其结果作为消息有效内容发送的 JPA QL |
8 | The JPA QL whose result are sent out as the payload of the message |
9 | 此属性表示 JPQL 查询在结果中给出一个实体还是一个实体的 List 。如果值设置为 true ,则将单个实体作为消息的有效内容发送。然而,如果在将此值设置为 true`后返回多个结果,则会抛出 `MessagingException 。该值默认为 false . |
10 | This attribute tells whether the JPQL query gives a single entity in the result or a List of entities.
If the value is set to true , the single entity is sent as the payload of the message.
If, however, multiple results are returned after setting this to true , a MessagingException is thrown.
The value defaults to false . |
11 | 此非零、非负整数值告诉适配器在执行选择操作时不要选择超过给定数量的行。默认情况下,如果未设置此属性,查询将选择所有可能的记录。此属性与 `max-results-expression`互斥。可选。 |
12 | This non-zero, non-negative integer value tells the adapter not to select more than the given number of rows on execution of the select operation.
By default, if this attribute is not set, all possible records are selected by the query.
This attribute is mutually exclusive with max-results-expression .
Optional. |
13 | 一个在结果集中查找最大结果数量的表达式。与 `max-results`互斥。可选。 |
14 | An expression that is evaluated to find the maximum number of results in a result set.
Mutually exclusive with max-results .
Optional. |
15 | 如果您希望在执行查询后删除接收到的行,请将该值设置为 true 。您必须确保组件作为事务的一部分进行操作。否则,您可能会遇到诸如: `java.lang.IllegalArgumentException: Removing a detached instance …`之类的异常。 |
16 | Set this value to true if you want to delete the rows received after execution of the query.
You must ensure that the component operates as part of a transaction.
Otherwise, you may encounter an exception such as: java.lang.IllegalArgumentException: Removing a detached instance … |
17 | 如果您想在删除接收到的实体后立即刷新持久化上下文并且不想依赖 EntityManager`的 `flushMode`时,请将该值设置为 `true 。该值默认为 false . |
18 | Set this value to true if you want to flush the persistence context immediately after deleting received entities and if you do not want to rely on the flushMode of the EntityManager .
The value defaults to false . |
Configuration Parameter Reference
以下清单显示了可以为`inbound-channel-adapter`设置的所有值:
The following listing shows all the values that can be set for an inbound-channel-adapter
:
<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 | This lifecycle attribute signals whether this component should automatically start when the application context starts.
This attribute defaults to true .
Optional. |
3 | 适配器使用执行所需的 JPA 操作产生的有效内容向其发送一条消息的通道。 |
4 | The channel to which the adapter sends a message with the payload from performing the desired JPA operation. |
5 | 一个布尔标志,表示在适配器轮询到所选记录后是否将其删除。默认情况下,值为 false (即不删除记录)。您必须确保组件作为事务的一部分进行操作。否则,您可能会遇到诸如: `java.lang.IllegalArgumentException: Removing a detached instance …​`之类的异常。可选。 |
6 | A boolean flag that indicates whether to delete the selected records after they have been polled by the adapter.
By default, the value is false (that is, the records are not deleted).
You must ensure that the component operates as part of a transaction.
Otherwise, you may encounter an exception, such as: java.lang.IllegalArgumentException: Removing a detached instance … .
Optional. |
7 | 指示记录可批量删除还是必须一次删除一条记录的布尔标志。默认情况下,值为 false (即,记录可以批量删除)。可选。 |
8 | A boolean flag that indicates whether the records can be deleted in bulk or must be deleted one record at a time.
By default, the value is false (that is, the records can be bulk-deleted).
Optional. |
9 | 要从数据库中查询的实体类的完全限定名。适配器会根据实体类名自动构建一个 JPA 查询。可选。 |
10 | The fully qualified name of the entity class to be queried from the database. The adapter automatically builds a JPA Query based on the entity class name. Optional. |
11 | 用于执行 JPA 操作的 jakarta.persistence.EntityManager 实例。可选。 |
12 | An instance of jakarta.persistence.EntityManager used to perform the JPA operations.
Optional. |
13 | 用于获取执行 JPA 操作的 jakarta.persistence.EntityManager 实例的 jakarta.persistence.EntityManagerFactory 实例。可选。 |
14 | An instance of jakarta.persistence.EntityManagerFactory used to obtain an instance of jakarta.persistence.EntityManager that performs the JPA operations.
Optional. |
15 | 指示选择操作预计会返回单个结果还是 List 结果的布尔标志。如果将此标志设置为 true ,则所选的单个实体将作为消息的有效负载发送。如果返回多个实体,则会引发异常。如果为 false ,则将 List 实体作为消息的有效负载发送。该值默认为 false 。可选。 |
16 | A boolean flag indicating whether the select operation is expected to return a single result or a List of results.
If this flag is set to true , the single entity selected is sent as the payload of the message.
If multiple entities are returned, an exception is thrown.
If false , the List of entities is sent as the payload of the message.
The value defaults to false .
Optional. |
17 | 用于执行 JPA 操作的 org.springframework.integration.jpa.core.JpaOperations 实现。我们建议不要提供自己的实现,而使用默认的 org.springframework.integration.jpa.core.DefaultJpaOperations 实现。您可以使用任何 entity-manager 、entity-manager-factory 或 jpa-operations 属性。可选。 |
18 | An implementation of org.springframework.integration.jpa.core.JpaOperations used to perform the JPA operations.
We recommend not providing an implementation of your own but using the default org.springframework.integration.jpa.core.DefaultJpaOperations implementation.
You can use any of the entity-manager , entity-manager-factory , or jpa-operations attributes.
Optional. |
19 | 此适配器执行的 JPA QL。可选。 |
20 | The JPA QL to be executed by this adapter. Optional. |
21 | 此适配器需要执行的命名查询。可选。 |
22 | The named query that needs to be executed by this adapter. Optional. |
23 | 此适配器执行的原生查询。您可以使用任何 jpa-query 、named-query 、entity-class 或 native-query 属性。可选。 |
24 | The native query executed by this adapter.
You can use any of the jpa-query , named-query , entity-class , or native-query attributes.
Optional. |
25 | 用于解析查询中参数值的 o.s.i.jpa.support.parametersource.ParameterSource 实现。如果 entity-class 属性具有一定值,则此实现将被忽略。可选。 |
26 | An implementation of o.s.i.jpa.support.parametersource.ParameterSource used to resolve the values of the parameters in the query.
Ignored if the entity-class attribute has a value.
Optional. |
27 | 向通道发送消息时等待的最长时间(以毫秒为单位)。可选。 |
28 | Maximum amount of time (in milliseconds) to wait when sending a message to the channel. Optional. |
Configuring with Java Configuration
以下 Spring Boot 应用程序展示了如何使用 Java 配置入站适配器的示例:
The following Spring Boot application shows an example of how to configure the inbound adapter with 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());
}
}
Configuring with the Java DSL
以下 Spring Boot 应用程序展示了如何使用 Java DSL 配置入站适配器的示例:
The following Spring Boot application shows an example of how to configure the inbound adapter with the 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();
}
}