入站通道适配器
入站通道适配器用于使用 JPA QL 对数据库执行 select 查询并返回结果。
消息有效负载可以是单个实体,也可以是List的实体。
以下 XML 配置了一个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 | 该通道 inbound-channel-adapter将消息(连同有效负载)放在执行 JPA QL 之后的query属性。 | 
| 2 | 这 EntityManager实例。 | 
| 3 | Attribute 指示组件是否应在应用程序上下文启动时自动启动。
该值默认为 true. | 
| 4 | 其结果作为消息的有效负载发送的 JPA QL | 
| 5 | 此属性指示 JPQL 查询是在结果中给出单个实体,还是给出 List的实体。
如果该值设置为true,则单个实体将作为消息的有效负载发送。
但是,如果在将此项设置为true一个MessagingException被抛出。
该值默认为false. | 
| 6 | 此非零、非负整数值指示适配器在执行 select作时选择不超过给定行数的行数。
默认情况下,如果未设置此属性,则查询将选择所有可能的记录。
此属性与 max-results-expression.
自选。 | 
| 7 | 一个表达式,用于查找结果集中的最大结果数。
互斥与 max-results.
自选。 | 
| 8 | 将此值设置为 true如果要删除执行查询后收到的行。
您必须确保该组件作为事务的一部分运行。
否则,您可能会遇到如下异常:java.lang.IllegalArgumentException: Removing a detached instance … | 
| 9 | 将此值设置为 true如果要在删除收到的实体后立即刷新持久性上下文,并且不想依赖flushMode的EntityManager.
该值默认为false. | 
配置参数参考
下面的清单显示了可以为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 | 适配器向其发送一条消息的通道,其中包含执行所需 JPA作的有效负载。 | 
| 3 | 一个布尔标志,指示是否在适配器轮询所选记录后删除这些记录。
默认情况下,值为 false(即,记录不会被删除)。
您必须确保该组件作为事务的一部分运行。
否则,您可能会遇到异常,例如:java.lang.IllegalArgumentException: Removing a detached instance ….
自选。 | 
| 4 | 一个布尔标志,指示记录是可以批量删除还是必须一次删除一条记录。
默认情况下,值为 false(即,可以批量删除记录)。
自选。 | 
| 5 | 要从数据库中查询的实体类的完全限定名称。 适配器根据实体类名自动构建 JPA 查询。 自选。 | 
| 6 | 一个 jakarta.persistence.EntityManager用于执行 JPA作。
自选。 | 
| 7 | 一个 jakarta.persistence.EntityManagerFactory用于获取jakarta.persistence.EntityManager执行 JPA作。
自选。 | 
| 8 | 一个布尔标志,指示 select作是应返回单个结果还是 List的结果。
如果此标志设置为true,则所选的单个实体将作为消息的有效负载发送。
如果返回多个实体,则会引发异常。
如果false这Listof entities 作为消息的有效负载发送。
该值默认为false.
自选。 | 
| 9 | 的实现 org.springframework.integration.jpa.core.JpaOperations用于执行 JPA作。
我们建议不要提供您自己的实现,而是使用默认的org.springframework.integration.jpa.core.DefaultJpaOperations实现。
您可以使用任何entity-manager,entity-manager-factory或jpa-operations属性。
自选。 | 
| 10 | 要由此适配器执行的 JPA QL。 自选。 | 
| 11 | 需要由此适配器执行的命名查询。 自选。 | 
| 12 | 此适配器执行的本机查询。
您可以使用任何 jpa-query,named-query,entity-class或native-query属性。
自选。 | 
| 13 | 的实现 o.s.i.jpa.support.parametersource.ParameterSource用于解析查询中参数的值。
如果entity-classattribute 具有值。
自选。 | 
| 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();
    }
}