入站通道介面卡
入站通道介面卡用於使用 JPA QL 在資料庫上執行選擇查詢並返回結果。訊息負載可以是單個實體或實體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在執行query屬性中的 JPA QL 後將訊息(帶有效負載)放入的通道。 |
| 2 | 用於執行所需 JPA 操作的EntityManager例項。 |
| 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 | 如果您想在刪除收到的實體後立即重新整理持久化上下文,並且不想依賴EntityManager的flushMode,請將此值設定為true。該值預設為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 | 用於執行 JPA 操作的jakarta.persistence.EntityManager例項。可選。 |
| 7 | 用於獲取執行 JPA 操作的jakarta.persistence.EntityManager例項的jakarta.persistence.EntityManagerFactory例項。可選。 |
| 8 | 一個布林標誌,指示選擇操作是預期返回單個結果還是結果List。如果此標誌設定為true,則選定的單個實體作為訊息的有效負載傳送。如果返回多個實體,則丟擲異常。如果為false,則實體List作為訊息的有效負載傳送。該值預設為false。可選。 |
| 9 | 用於執行 JPA 操作的org.springframework.integration.jpa.core.JpaOperations實現。我們建議不要提供您自己的實現,而是使用預設的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-class屬性有值,則忽略。可選。 |
| 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();
}
}