181.16. JPA 기반 Idempotent 리포지토리 사용

EIP 패턴 의 Idempotent Consumer는 중복된 메시지를 필터링하는 데 사용됩니다. JPA 기반 멱등 리포지토리가 제공됩니다.

JPA 기반 멱등 리포지토리를 사용하려면 다음을 수행합니다.

절차

  1. persistence.xml 파일에 persistence-unit 을 설정합니다.
  2. org. apache.cam.processor.idempotent.jpaMessageIdRepository에서 사용하는 org.springframework.jpa.jpa Template 을 설정합니다.
  3. 매크로 형식 지정 매크로 구성: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20
  4. 멱등 리포지토리 구성: org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository:
  5. 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 구성 요소 테스트를 실행하는 경우

이 구성 요소의 테스트 를 직접 IDE 내에서 실행하고 Maven을 통과하지 않으면 다음과 같은 예외를 확인할 수 있습니다.

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이 아닌 Maven을 통해 빌드 시 바이트 코드를 개선 할 수 있다는 점입니다. 이 문제를 해결하려면 OpenJ PA의 동적 바이트 코드 개선 기능 을 활성화해야 합니다. 예를 들어 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