如需使用最新稳定版本,请使用 Spring Integration 7.0.4spring-doc.cadn.net.cn

入站通道适配器

入站通道适配器用于使用 JPA QL 执行数据库查询并返回结果。 消息负载要么是单个实体,要么是一组 List 个实体。 以下 XML 配置了一个 inbound-channel-adapterspring-doc.cadn.net.cn

<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 执行后,inbound-channel-adapter 将消息(包含负载)放入的通道,该通道由 query 属性指定。
2 The EntityManager 实例用于执行所需的 JPA 操作。
3 属性,用于指示组件是否应在应用程序上下文启动时自动启动。 该值的默认值为 true
4 发送为消息负载的 JPA QL
5 此属性指示 JPQL 查询是否在结果中返回单个实体或 List 个实体。 如果值设置为 true,则单个实体将作为消息的有效负载发送。 然而,如果将此设置为 true 后返回多个结果,则会抛出 MessagingException。 该值的默认值为 false
6 此非零、非负整数值指示适配器在执行选择操作时不要选择超过给定数量的行。 默认情况下,如果未设置此属性,查询将选择所有可能的记录。 此属性与 max-results-expression 互斥。 可选。
7 用于在结果集中查找最大结果数量的表达式。 与 max-results 互斥。 可选。
8 如果您希望在查询执行后删除接收到的行,请将此值设置为true。 您必须确保该组件作为事务的一部分运行。 否则,您可能会遇到类似以下的异常:java.lang.IllegalArgumentException: Removing a detached instance …​
9 如果您希望在删除接收到的实体后立即刷新持久化上下文,并且不希望依赖flushModeEntityManager,请将此值设置为true。 该值的默认值为false

配置参数参考

以下列表显示了可以为 inbound-channel-adapter 设置的所有值:spring-doc.cadn.net.cn

<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 一个用于执行 JPA 操作的 jakarta.persistence.EntityManager 实例。 可选。
7 一个用于获取执行 JPA 操作的 jakarta.persistence.EntityManager 实例的 jakarta.persistence.EntityManagerFactory 实例。 可选。
8 一个布尔标志,指示该选择操作是否预期返回单个结果或List个结果。 如果此标志设置为true,则所选的单个实体将作为消息的有效载荷发送。 如果返回多个实体,则将抛出异常。 如果为false,则List个实体将作为消息的有效载荷发送。 该值默认为false。 可选。
9 org.springframework.integration.jpa.core.JpaOperations 的实现,用于执行 JPA 操作。 我们建议不要提供您自己的实现,而是使用默认的 org.springframework.integration.jpa.core.DefaultJpaOperations 实现。 您可以使用 entity-managerentity-manager-factoryjpa-operations 中的任意一个属性。 可选的。
10 此适配器要执行的 JPA QL。 可选。
11 此适配器需要执行的命名查询。 可选。
12 此适配器执行的本地查询。 您可以使用任意 jpa-querynamed-queryentity-classnative-query 属性。 可选。
13 一个用于解析查询中参数值的o.s.i.jpa.support.parametersource.ParameterSource实现。 如果entity-class属性具有值,则会被忽略。 可选。
14 发送消息到通道时等待的最大时间(毫秒)。 可选。

使用 Java 配置进行配置

以下 Spring Boot 应用程序展示了如何使用 Java 配置入站适配器的示例:spring-doc.cadn.net.cn

@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 配置入站适配器:spring-doc.cadn.net.cn

@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();
    }

}