gpt4 book ai didi

java - 如何在Java配置中正确定义Spring Integration JpaOutboundGateway?

转载 作者:行者123 更新时间:2023-11-30 02:48:57 25 4
gpt4 key购买 nike

我正在尝试用 JavaConfig 替换 Spring Integration XML 配置,但无法正确定义 JpaOutboundGateway。在 XML 中,我有以下 bean 定义:

<int-jpa:outbound-channel-adapter id="saveOffer" entity-manager-factory="entityManagerFactory" channel="channel5"
flush-size="100" entity-class="JobOffer">
<int-jpa:transactional transaction-manager="transactionManager"/>
</int-jpa:outbound-channel-adapter>

而且效果很好。但是当我尝试将其移至 java config bean 时,我不知道如何定义事务管理...我仍然收到与事务相关的错误。

@Bean
@ServiceActivator(inputChannel="channel5")
public JpaOutboundGatewayFactoryBean save() throws Exception {

JpaExecutor executor = new JpaExecutor(entityManager);
executor.setFlushSize(10);
executor.setEntityClass(JobOffer.class);

JpaOutboundGatewayFactoryBean factoryBean = new JpaOutboundGatewayFactoryBean();
factoryBean.setJpaExecutor(executor);
factoryBean.setProducesReply(false);

return factoryBean;
}

我试图在 JpaExecutor 中使用 EntityManager 和 EntityManagerFactory。但在这两种情况下都会引发异常:

2016-09-03 09:20:42.440 ERROR o.s.integration.handler.LoggingHandler  : org.springframework.messaging.MessageHandlingException: error occurred in message handler [org.springframework.integration.jpa.outbound.JpaOutboundGateway@1fc306d3]; nested exception is javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'merge' call
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:139)
at org.springframework.integration.endpoint.PollingConsumer.handleMessage(PollingConsumer.java:129)
at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:272)
at org.springframework.integration.endpoint.AbstractPollingEndpoint.access$000(AbstractPollingEndpoint.java:58)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:190)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:186)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:353)
at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:55)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:51)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:344)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'merge' call
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:282)
at com.sun.proxy.$Proxy121.merge(Unknown Source)
at org.springframework.integration.jpa.core.DefaultJpaOperations.persistOrMerge(DefaultJpaOperations.java:248)
at org.springframework.integration.jpa.core.DefaultJpaOperations.merge(DefaultJpaOperations.java:215)
at org.springframework.integration.jpa.core.JpaExecutor.executeOutboundJpaOperation(JpaExecutor.java:253)
at org.springframework.integration.jpa.outbound.JpaOutboundGateway.handleRequestMessage(JpaOutboundGateway.java:81)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:109)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
... 19 more

最佳答案

这是连接起来比较复杂的端点之一;我们需要在引用手册中添加Java配置的示例;我开了一个JIRA Issue .

您可以随时查看the XML parser查看需要什么。

特别是:

  1. JpaExecutor 应定义为 @Bean
  2. 您需要将事务建议添加到适配器的txAdviceChain
  3. 您可以查看 IntegrationNamespaceUtils.configureTransactionAttributes 以了解将事务建议(事务属性、属性源、TM 等)连接到 TransactionInterceptor 所需的内容。

关于java - 如何在Java配置中正确定义Spring Integration JpaOutboundGateway?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39304371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com