172.15. JPA ベースのべき等リポジトリーの使用
EIP パターン のべき等コンシューマーは、重複メッセージをフィルターするために使用されます。JPA ベースのべき等リポジトリーが提供されます。
JPA ベースのべき等リポジトリーを使用します。
手順
-
persistence.xml ファイルで
persistence-unit
を設定します。 -
org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository
によって使用されるorg.springframework.orm.jpa.JpaTemplate
を設定します。 - error format マクロの設定: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20
-
べき等リポジトリーを設定します(
org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository
)。 - Spring XML ファイルに JPA べき等リポジトリーを作成します。
<camelContext xmlns="http://camel.apache.org/schema/spring"> <route id="JpaMessageIdRepositoryTest"> <from uri="direct:start" /> <idempotentConsumer messageIdRepositoryRef="jpaStore"> <header>messageId</header> <to uri="mock:result" /> </idempotentConsumer> </route> </camelContext>
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route id="JpaMessageIdRepositoryTest">
<from uri="direct:start" />
<idempotentConsumer messageIdRepositoryRef="jpaStore">
<header>messageId</header>
<to uri="mock:result" />
</idempotentConsumer>
</route>
</camelContext>
IDE 内で Camel コンポーネントテストを実行する場合
Maven ではなく、IDE 内で このコンポーネントのテスト を直接実行する場合は、以下のような例外が表示されます。
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is <openjpa-2.2.1-r422266:1396819 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: This configuration disallows runtime optimization, but the following listed types were not enhanced at build time or at class load time with a javaagent: "org.apache.camel.examples.SendEmail". at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:427) at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371) at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:127) at org.apache.camel.processor.jpa.JpaRouteTest.cleanupRepository(JpaRouteTest.java:96) at org.apache.camel.processor.jpa.JpaRouteTest.createCamelContext(JpaRouteTest.java:67) at org.apache.camel.test.junit4.CamelTestSupport.doSetUp(CamelTestSupport.java:238) at org.apache.camel.test.junit4.CamelTestSupport.setUp(CamelTestSupport.java:208)
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is
<openjpa-2.2.1-r422266:1396819 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: This configuration disallows runtime optimization,
but the following listed types were not enhanced at build time or at class load time with a javaagent: "org.apache.camel.examples.SendEmail".
at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:427)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:127)
at org.apache.camel.processor.jpa.JpaRouteTest.cleanupRepository(JpaRouteTest.java:96)
at org.apache.camel.processor.jpa.JpaRouteTest.createCamelContext(JpaRouteTest.java:67)
at org.apache.camel.test.junit4.CamelTestSupport.doSetUp(CamelTestSupport.java:238)
at org.apache.camel.test.junit4.CamelTestSupport.setUp(CamelTestSupport.java:208)
この問題は、ソースが IDE 経由でコンパイルまたは再コンパイルされ、Maven ではなく IDE でコンパイルされているため、ビルド時にバイトコードを強化 します。これに対応するには、OpenJPA の動的バイトコード拡張 を有効にする必要があります。たとえば、Camel で現在使用されている OpenJPA のバージョンが 2.2.1 で、IDE 内でテストを実行するには、以下の引数を JVM に渡す必要があります。
-javaagent:<path_to_your_local_m2_cache>/org/apache/openjpa/openjpa/2.2.1/openjpa-2.2.1.jar
-javaagent:<path_to_your_local_m2_cache>/org/apache/openjpa/openjpa/2.2.1/openjpa-2.2.1.jar