182.16. JPA ベースのべき等リポジトリーの使用
EIP パターン の Idempotent Consumer は、重複するメッセージを除外するために使用されます。JPA ベースのべき等リポジトリーが提供されます。
JPA ベースのべき等リポジトリーを使用するには。
手順
-
persistence.xml ファイルで
persistence-unit
を設定します。 -
org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository
で使用されるorg.springframework.orm.jpa.JpaTemplate
を設定します。 - エラー形式のマクロを設定します: 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>
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)
ここでの問題は、ソースが 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