Camel 如何在Sping Boot 中使用JpaMessageIdRepository?

pinkon5k  于 2022-11-07  发布在  Apache
关注(0)|答案(2)|浏览(146)

我试图让JpaMessageIdRepository在我的Sping Boot 2.3项目中工作,但是毫无进展。

jpaMessageIdRepository(String persistenceUnit, String processorName) 
jpaMessageIdRepository(EntityManagerFactory entityManagerFactory, String processorName)

我应该使用哪一个?以及如何从我的类中获得entityManagerFactory?似乎在任何地方都找不到一个工作示例:(
这是从我的 Camel 路线不工作:

.idempotentConsumer(
                header("CamelFileName"),
                jpaMessageIdRepository("what", "FileRepo")
        )
        .log("${header.CamelFileName}")
rsl1atfo

rsl1atfo1#

我已经用spring-boot 2.5.4,camel 3.13.0和hib5.4.32.Final成功地运行了它。我不确定这是最好的方法,但是关于这个主题的文档非常少。
www.example.com上没有关于创建sql表的内容https://camel.apache.org/components/3.13.x/jpa-component.html#_spring_boot_auto_configuration但在https://camel.apache.org/components/3.13.x/sql-component.html上找到了一些有用的信息
Maven依赖项

<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-spring-boot-starter</artifactId>
    <version>3.13.0</version>
</dependency>
<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-jpa-starter</artifactId>
    <version>3.13.0</version>
</dependency>

正在创建sql表和序列

CREATE TABLE CAMEL_MESSAGEPROCESSED
(
    id             number(38) primary key not null,
    processor_name VARCHAR(255),
    message_id     VARCHAR(100),
    created_at     TIMESTAMP
);

create sequence hibernate_sequence start with 1 increment by 50;

要包含实体org.apache.camel.processor.idempotent.jpa.MessageProcessed的Jpa实体配置(Spring Bean)

import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Configuration;

@EntityScan(basePackages = {
        "project.base.package.to.do",
        "org.apache.camel.processor.idempotent.jpa"
})
@Configuration
class JpaEntityConfiguration {
}

正在创建JpaMessageIdRepository。应更改处理器名称。EntityManagerFactory从Spring上下文自动连接。

private JpaMessageIdRepository createJpaMessageIdRepository(EntityManagerFactory entityManagerFactory) {
    return JpaMessageIdRepository.jpaMessageIdRepository(entityManagerFactory, "helloSOProcessor");
}

覆盖ID生成策略(仅当您使用名称为hib_sequence以外的序列并且不想创建hib_sequence时)。在META-INF下创建orm.xml

<entity-mappings>
    <entity class="org.apache.camel.processor.idempotent.jpa.MessageProcessed" name="MessageProcessed">
        <attributes>
            <id name="id">
                <generated-value strategy="sequence" generator="sequenceGenerator"/>
            </id>
        </attributes>
    </entity>
</entity-mappings>
rggaifut

rggaifut2#

您可以注入实体管理器工厂

@Autowired
private EntityManagerFactory emf;

而使用它:

.idempotentConsumer(
            header("CamelFileName"),
            jpaMessageIdRepository(emf, "FileRepo")
    )
    .log("${header.CamelFileName}")

相关问题