37.26. 使用基于 JPA 的幂等存储库
在本节中,我们将使用基于 JPA 的幂等存储库,而不是以默认方式为基础的内存。
首先,我们需要 META-INF/persistence.xml 中的 persistence-unit,其中我们需要使用类 org.apache.camel.processor.idempotent.jpa.MessageProcessed 作为模型。
<persistence-unit name="idempotentDb" transaction-type="RESOURCE_LOCAL">
<class>org.apache.camel.processor.idempotent.jpa.MessageProcessed</class>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:derby:target/idempotentTest;create=true"/>
<property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/>
<property name="openjpa.Multithreaded" value="true"/>
</properties>
</persistence-unit>
接下来,我们还可在 spring XML 文件中创建 JPA idempotent 存储库:
<!-- we define our jpa based idempotent repository we want to use in the file consumer -->
<bean id="jpaStore" class="org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository">
<!-- Here we refer to the entityManagerFactory -->
<constructor-arg index="0" ref="entityManagerFactory"/>
<!-- This 2nd parameter is the name (= a category name).
You can have different repositories with different names -->
<constructor-arg index="1" value="FileConsumer"/>
</bean>
然后,我们只需要使用 # 语法选项在文件消费者端点中使用 idempotentRepository 来指代文件消费者端点中的 jpaStore bean :
<route>
<from uri="file://inbox?idempotent=true&idempotentRepository=#jpaStore"/>
<to uri="bean:processInbox"/>
</route>