Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Maven users will need to add the following dependency to their pom.xml for this component:

Code Block
xml
xml

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jpa</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

...

From Camel 2.13 onwards you can use @PreConsumed which will be invoked on your entity bean before it has been processed (before routing).

URI format

Code Block

jpa:entityClassName[?options]

...

You can append query options to the URI in the following format, ?option=value&option=value&...

Options

Wiki Markup
{div:class=confluenceTableSmall}
|| Name || Default Value || Description ||
| {{entityType}} | _entityClassName_ | Overrides the _entityClassName_ from the URI. |
| {{persistenceUnit}} | {{camel}} | The JPA persistence unit used by default. | 
| {{consumeDelete}} | {{true}} | *JPA consumer only:* If {{true}}, the entity is deleted after it is consumed; if {{false}}, the entity is not deleted. |
| {{consumeLockEntity}} | {{true}} | *JPA consumer only:* Specifies whether or not to set an exclusive lock on each entity bean while processing the results from polling. |
| {{flushOnSend}} | {{true}} | *JPA producer only:* Flushes the [EntityManager|http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html] after the entity bean has been persisted. |
| {{maximumResults}} | {{-1}} |  *JPA consumer only:* Set the maximum number of results to retrieve on the [Query|http://java.sun.com/javaee/5/docs/api/javax/persistence/Query.html]. |
| {{transactionManager}} | {{null}} | This option is [Registry] based which requires the {{#}} notation so that the given {{transactionManager}} being specified can be looked up properly, e.g. {{transactionManager=#myTransactionManager}}. It specifies the transaction manager to use. If none provided, Camel will use a {{JpaTransactionManager}} by default. Can be used to set a JTA transaction manager (for integration with an EJB container). |
| {{consumer.delay}} | {{500}} | *JPA consumer only:* Delay in milliseconds between each poll. |
| {{consumer.initialDelay}} | {{1000}} | *JPA consumer only:* Milliseconds before polling starts. |
| {{consumer.useFixedDelay}} | {{false}} | *JPA consumer only:* Set to {{true}} to use fixed delay between polls, otherwise fixed rate is used. See [ScheduledExecutorService|http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html] in JDK for details. |
| {{maxMessagesPerPoll}} | {{0}} | *JPA consumer only:* An integer value to define the maximum number of messages to gather per poll. By default, no maximum is set. Can be used to avoid polling many thousands of messages when starting up the server. Set a value of 0 or negative to disable. |
| {{consumer.query}} | | *JPA consumer only:* To use a custom query when consuming data. |
| {{consumer.namedQuery}} | | *JPA consumer only:* To use a named query when consuming data. |
| {{consumer.nativeQuery}} | | *JPA consumer only:* To use a custom native query when consuming data. You may want to use the option {{consumer.resultClass}} also when using native queries. |
| {{consumer.parameters}} |  | *Camel 2.12: JPA consumer only:* This option is [Registry] based which requires the {{#}} notation. This key/value mapping is used for building the query parameters. It's is expected to be of the generic type {{java.util.Map<String, Object>}} where the keys are the named parameters of a given JPA query and the values are their corresponding effective values you want to select for. |
| {{consumer.resultClass}} | | *Camel 2.7: JPA consumer only:* Defines the type of the returned payload (we will call {{entityManager.createNativeQuery(nativeQuery, resultClass)}} instead of {{entityManager.createNativeQuery(nativeQuery)}}). Without this option, we will return an object array. Only has an affect when using in conjunction with native query when consuming data. |
| {{consumer.transacted}} | {{false}} | *Camel 2.7.5/2.8.3/2.9: JPA consumer only:* Whether to run the consumer in transacted mode, by which all messages will either commit or rollback, when the entire batch has been processed. The default behavior (false) is to commit all the previously successfully processed messages, and only rollback the last failed message. |
| {{consumer.lockModeType}} | {{WRITE}} | *Camel 2.11.2/2.12:* To configure the lock mode on the consumer. The possible values is defined in the enum {{javax.persistence.LockModeType}}. The default value is changed to {{PESSIMISTIC_WRITE}} since *Camel 2.13*.|
| {{consumer.SkipLockedEntity}} | {{false}} | *Camel 2.13:* To configure whether to use NOWAIT on lock and silently skip the entity. |
| {{usePersist}} | {{false}} | *Camel 2.5: JPA producer only:* Indicates to use {{entityManager.persist(entity)}} instead of {{entityManager.merge(entity)}}. Note: {{entityManager.persist(entity)}} doesn't work for detached entities (where the EntityManager has to execute an UPDATE instead of an INSERT query)! |
{div}

Message Headers

Camel adds the following message headers to the exchange:

...

For example, you can instantiate a JPA component that references the myEMFactory entity manager factory, as follows:

Code Block
xml
xml

<bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
   <property name="entityManagerFactory" ref="myEMFactory"/>
</bean>

...

For example, you can instantiate a JPA component that references the myTransactionManager transaction manager, as follows:

Code Block
xml
xml

<bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
   <property name="entityManagerFactory" ref="myEMFactory"/>
   <property name="transactionManager" ref="myTransactionManager"/>
</bean>

...

For consuming only selected entities, you can use the consumer.namedQuery URI query option. First, you have to define the named query in the JPA Entity class:

Code Block

@Entity
@NamedQuery(name = "step1", query = "select x from MultiSteps x where x.step = 1")
public class MultiSteps {
   ...
}

After that you can define a consumer uri like this one:

Code Block

from("jpa://org.apache.camel.examples.MultiSteps?consumer.namedQuery=step1")
.to("bean:myBusinessLogic");

...

For consuming only selected entities, you can use the consumer.query URI query option. You only have to define the query option:

Code Block

from("jpa://org.apache.camel.examples.MultiSteps?consumer.query=select o from org.apache.camel.examples.MultiSteps o where o.step = 1")
.to("bean:myBusinessLogic");

...

For consuming only selected entities, you can use the consumer.nativeQuery URI query option. You only have to define the native query option:

Code Block

from("jpa://org.apache.camel.examples.MultiSteps?consumer.nativeQuery=select * from MultiSteps where step = 1")
.to("bean:myBusinessLogic");

...

And finally we can create our JPA idempotent repository in the spring XML file as well:

Code Block
xml
xml

<camelContext xmlns="http://camel.apache.org/schema/spring">	
    <route id="JpaMessageIdRepositoryTest">
        <from uri="direct:start" />
        <idempotentConsumer messageIdRepositoryRef="jpaStore">
            <header>messageId</camel:header>
            <to uri="mock:result" />
        </idempotentConsumer>
    </route>
</camelContext>
Info
titleWhen running this Camel component tests inside your IDE

In case you run the tests of this component directly inside your IDE (and not necessarily through Maven itself) then you could spot exceptions like:

Code Block
java
java

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)

The problem here is that the source has been compiled/recompiled through your IDE and not through Maven itself which would enhance the byte-code at build time. To overcome this you would need to enable dynamic byte-code enhancement of OpenJPA. As an example assuming the current OpenJPA version being used in Camel itself is 2.2.1, then as running the tests inside your favorite IDE you would need to pass the following argument to the JVM:

Code Block
 
-javaagent:<path_to_your_local_m2_cache>/org/apache/openjpa/openjpa/2.2.1/openjpa-2.2.1.jar

Then it will all become green again (smile)

Include Page
Endpoint See Also
Endpoint See Also