You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2016/05/02 08:36:13 UTC

camel git commit: Added camel-jpa docs to Gitbook

Repository: camel
Updated Branches:
  refs/heads/master ec54b23f3 -> 65aea6a54


Added camel-jpa docs to Gitbook


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/65aea6a5
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/65aea6a5
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/65aea6a5

Branch: refs/heads/master
Commit: 65aea6a54b8dd4a2711a361ee0685b4c763bb045
Parents: ec54b23
Author: Andrea Cosentino <an...@gmail.com>
Authored: Mon May 2 08:35:39 2016 +0200
Committer: Andrea Cosentino <an...@gmail.com>
Committed: Mon May 2 08:35:57 2016 +0200

----------------------------------------------------------------------
 components/camel-jpa/src/main/docs/jpa.adoc | 381 +++++++++++++++++++++++
 docs/user-manual/en/SUMMARY.md              |   1 +
 2 files changed, 382 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/65aea6a5/components/camel-jpa/src/main/docs/jpa.adoc
----------------------------------------------------------------------
diff --git a/components/camel-jpa/src/main/docs/jpa.adoc b/components/camel-jpa/src/main/docs/jpa.adoc
new file mode 100644
index 0000000..937f461
--- /dev/null
+++ b/components/camel-jpa/src/main/docs/jpa.adoc
@@ -0,0 +1,381 @@
+[[JPA-JPAComponent]]
+JPA Component
+~~~~~~~~~~~~~
+
+The *jpa* component enables you to store and retrieve Java objects from
+persistent storage using EJB 3's Java Persistence Architecture (JPA),
+which is a standard interface layer that wraps Object/Relational Mapping
+(ORM) products such as OpenJPA, Hibernate, TopLink, and so on.
+
+Maven users will need to add the following dependency to their `pom.xml`
+for this component:
+
+[source,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>
+------------------------------------------------------------
+
+[[JPA-Sendingtotheendpoint]]
+Sending to the endpoint
+^^^^^^^^^^^^^^^^^^^^^^^
+
+You can store a Java entity bean in a database by sending it to a JPA
+producer endpoint. The body of the _In_ message is assumed to be an
+entity bean (that is, a POJO with an
+http://java.sun.com/javaee/5/docs/api/javax/persistence/Entity.html[@Entity]
+annotation on it) or a collection or array of entity beans.
+
+If the body is a List of entities, make sure to use
+*entityType=java.util.ArrayList* as a configuration passed to the
+producer endpoint.
+
+If the body does not contain one of the previous listed types, put a
+link:message-translator.html[Message Translator] in front of the
+endpoint to perform the necessary conversion first.
+
+[[JPA-Consumingfromtheendpoint]]
+Consuming from the endpoint
+^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Consuming messages from a JPA consumer endpoint removes (or updates)
+entity beans in the database. This allows you to use a database table as
+a logical queue: consumers take messages from the queue and then
+delete/update them to logically remove them from the queue.
+
+If you do not wish to delete the entity bean when it has been processed
+(and when routing is done), you can specify `consumeDelete=false` on the
+URI. This will result in the entity being processed each poll.
+
+If you would rather perform some update on the entity to mark it as
+processed (such as to exclude it from a future query) then you can
+annotate a method with
+http://camel.apache.org/maven/current/camel-jpa/apidocs/org/apache/camel/component/jpa/Consumed.html[@Consumed]
+which will be invoked on your entity bean when the entity bean when it
+has been processed (and when routing is done).
+
+From *Camel 2.13* onwards you can use
+http://camel.apache.org/maven/current/camel-jpa/apidocs/org/apache/camel/component/jpa/PreConsumed.html[@PreConsumed]
+which will be invoked on your entity bean before it has been processed
+(before routing).
+
+If you are consuming a lot (100K+) of rows and experience OutOfMemory
+problems you should set the maximumResults to sensible value.
+
+[[JPA-URIformat]]
+URI format
+^^^^^^^^^^
+
+[source,java]
+-----------------------------
+jpa:entityClassName[?options]
+-----------------------------
+
+For sending to the endpoint, the _entityClassName_ is optional. If
+specified, it helps the link:type-converter.html[Type Converter] to
+ensure the body is of the correct type.
+
+For consuming, the _entityClassName_ is mandatory.
+
+You can append query options to the URI in the following format,
+`?option=value&option=value&...`
+
+[[JPA-Options]]
+Options
+^^^^^^^
+
+
+// component options: START
+The JPA component supports 4 options which are listed below.
+
+
+
+[width="100%",cols="2s,1m,8",options="header"]
+|=======================================================================
+| Name | Java Type | Description
+| entityManagerFactory | EntityManagerFactory | To use the EntityManagerFactory. This is strongly recommended to configure.
+| transactionManager | PlatformTransactionManager | To use the PlatformTransactionManager for managing transactions.
+| joinTransaction | boolean | The camel-jpa component will join transaction by default. You can use this option to turn this off for example if you use LOCAL_RESOURCE and join transaction doesn't work with your JPA provider. This option can also be set globally on the JpaComponent instead of having to set it on all endpoints.
+| sharedEntityManager | boolean | Whether to use Spring's SharedEntityManager for the consumer/producer. Note in most cases joinTransaction should be set to false as this is not an EXTENDED EntityManager.
+|=======================================================================
+// component options: END
+
+
+
+// endpoint options: START
+The JPA component supports 42 endpoint options which are listed below:
+
+[width="100%",cols="2s,1,1m,1m,5",options="header"]
+|=======================================================================
+| Name | Group | Default | Java Type | Description
+| entityType | common |  | Class<?> | *Required* The JPA annotated class to use as entity.
+| joinTransaction | common | true | boolean | The camel-jpa component will join transaction by default. You can use this option to turn this off for example if you use LOCAL_RESOURCE and join transaction doesn't work with your JPA provider. This option can also be set globally on the JpaComponent instead of having to set it on all endpoints.
+| persistenceUnit | common | camel | String | *Required* The JPA persistence unit used by default.
+| sharedEntityManager | common | false | boolean | Whether to use Spring's SharedEntityManager for the consumer/producer. Note in most cases joinTransaction should be set to false as this is not an EXTENDED EntityManager.
+| bridgeErrorHandler | consumer | false | boolean | Allows for bridging the consumer to the Camel routing Error Handler which mean any exceptions occurred while the consumer is trying to pickup incoming messages or the likes will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions that will be logged at WARN/ERROR level and ignored.
+| consumeDelete | consumer | true | boolean | If true the entity is deleted after it is consumed; if false the entity is not deleted.
+| consumeLockEntity | consumer | true | boolean | Specifies whether or not to set an exclusive lock on each entity bean while processing the results from polling.
+| deleteHandler | consumer |  | Object> | To use a custom DeleteHandler to delete the row after the consumer is done processing the exchange
+| lockModeType | consumer | PESSIMISTIC_WRITE | LockModeType | To configure the lock mode on the consumer.
+| maximumResults | consumer | -1 | int | Set the maximum number of results to retrieve on the Query.
+| maxMessagesPerPoll | consumer |  | int | 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.
+| namedQuery | consumer |  | String | To use a named query when consuming data.
+| nativeQuery | consumer |  | String | To use a custom native query when consuming data. You may want to use the option consumer.resultClass also when using native queries.
+| parameters | consumer |  | Map | This key/value mapping is used for building the query parameters. It's is expected to be of the generic type java.util.Map 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.
+| preDeleteHandler | consumer |  | Object> | To use a custom Pre-DeleteHandler to delete the row after the consumer has read the entity.
+| query | consumer |  | String | To use a custom query when consuming data.
+| resultClass | consumer |  | Class<?> | 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.
+| sendEmptyMessageWhenIdle | consumer | false | boolean | If the polling consumer did not poll any files you can enable this option to send an empty message (no body) instead.
+| skipLockedEntity | consumer | false | boolean | To configure whether to use NOWAIT on lock and silently skip the entity.
+| transacted | consumer | false | boolean | 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.
+| exceptionHandler | consumer (advanced) |  | ExceptionHandler | To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this options is not in use. By default the consumer will deal with exceptions that will be logged at WARN/ERROR level and ignored.
+| pollStrategy | consumer (advanced) |  | PollingConsumerPollStrategy | A pluggable org.apache.camel.PollingConsumerPollingStrategy allowing you to provide your custom implementation to control error handling usually occurred during the poll operation before an Exchange have been created and being routed in Camel.
+| flushOnSend | producer | true | boolean | Flushes the EntityManager after the entity bean has been persisted.
+| remove | producer | false | boolean | Indicates to use entityManager.remove(entity).
+| usePassedInEntityManager | producer | false | boolean | If set to true then Camel will use the EntityManager from the header JpaConstants.ENTITYMANAGER instead of the configured entity manager on the component/endpoint. This allows end users to control which entity manager will be in use.
+| usePersist | producer | false | boolean | 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)!
+| entityManagerProperties | advanced |  | Map | Additional properties for the entity manager to use.
+| exchangePattern | advanced | InOnly | ExchangePattern | Sets the default exchange pattern when creating an exchange
+| synchronous | advanced | false | boolean | Sets whether synchronous processing should be strictly used or Camel is allowed to use asynchronous processing (if supported).
+| backoffErrorThreshold | scheduler |  | int | The number of subsequent error polls (failed due some error) that should happen before the backoffMultipler should kick-in.
+| backoffIdleThreshold | scheduler |  | int | The number of subsequent idle polls that should happen before the backoffMultipler should kick-in.
+| backoffMultiplier | scheduler |  | int | To let the scheduled polling consumer backoff if there has been a number of subsequent idles/errors in a row. The multiplier is then the number of polls that will be skipped before the next actual attempt is happening again. When this option is in use then backoffIdleThreshold and/or backoffErrorThreshold must also be configured.
+| delay | scheduler | 500 | long | Milliseconds before the next poll.
+| greedy | scheduler | false | boolean | If greedy is enabled then the ScheduledPollConsumer will run immediately again if the previous run polled 1 or more messages.
+| initialDelay | scheduler | 1000 | long | Milliseconds before the first poll starts.
+| runLoggingLevel | scheduler | TRACE | LoggingLevel | The consumer logs a start/complete log line when it polls. This option allows you to configure the logging level for that.
+| scheduledExecutorService | scheduler |  | ScheduledExecutorService | Allows for configuring a custom/shared thread pool to use for the consumer. By default each consumer has its own single threaded thread pool.
+| scheduler | scheduler | none | ScheduledPollConsumerScheduler | To use a cron scheduler from either camel-spring or camel-quartz2 component
+| schedulerProperties | scheduler |  | Map | To configure additional properties when using a custom scheduler or any of the Quartz2 Spring based scheduler.
+| startScheduler | scheduler | true | boolean | Whether the scheduler should be auto started.
+| timeUnit | scheduler | MILLISECONDS | TimeUnit | Time unit for initialDelay and delay options.
+| useFixedDelay | scheduler | true | boolean | Controls if fixed delay or fixed rate is used. See ScheduledExecutorService in JDK for details.
+|=======================================================================
+// endpoint options: END
+
+
+[[JPA-MessageHeaders]]
+Message Headers
+^^^^^^^^^^^^^^^
+
+Camel adds the following message headers to the exchange:
+
+[width="100%",cols="10%,10%,80%",options="header",]
+|=======================================================================
+|Header |Type |Description
+
+|`CamelJpaTemplate` |`JpaTemplate` |*Not supported anymore since Camel 2.12:* The `JpaTemplate` object that
+is used to access the entity bean. You need this object in some
+situations, for instance in a type converter or when you are doing some
+custom processing. See
+https://issues.apache.org/jira/browse/CAMEL-5932[CAMEL-5932] for the
+reason why the support for this header has been dropped.
+
+|`CamelEntityManager` |`EntityManager` |*Camel 2.12: JPA consumer / Camel 2.12.2: JPA producer:* The JPA
+`EntityManager` object being used by `JpaConsumer` or `JpaProducer`.
+
+|=======================================================================
+
+[[JPA-ConfiguringEntityManagerFactory]]
+Configuring EntityManagerFactory
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Its strongly advised to configure the JPA component to use a specific
+`EntityManagerFactory` instance. If failed to do so each `JpaEndpoint`
+will auto create their own instance of `EntityManagerFactory` which most
+often is not what you want.
+
+For example, you can instantiate a JPA component that references the
+`myEMFactory` entity manager factory, as follows:
+
+[source,xml]
+-------------------------------------------------------------------
+<bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
+   <property name="entityManagerFactory" ref="myEMFactory"/>
+</bean>
+-------------------------------------------------------------------
+
+In *Camel 2.3* the `JpaComponent` will auto lookup the
+`EntityManagerFactory` from the link:registry.html[Registry] which means
+you do not need to configure this on the `JpaComponent` as shown above.
+You only need to do so if there is ambiguity, in which case Camel will
+log a WARN.
+
+[[JPA-ConfiguringTransactionManager]]
+Configuring TransactionManager
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Since *Camel 2.3* the `JpaComponent` will auto lookup the
+`TransactionManager` from the link:registry.html[Registry.] If Camel
+won't find any `TransactionManager` instance registered, it will also
+look up for the�`TransactionTemplate` and try to
+extract�`TransactionManager` from it.
+
+If none `TransactionTemplate` is available in the registry,
+`JpaEndpoint` will auto create their own instance of
+`TransactionManager` which most often is not what you want.
+
+If more than single instance of the `TransactionManager` is found, Camel
+will log a WARN. In such cases you might want to instantiate and
+explicitly configure a JPA component that references the
+`myTransactionManager` transaction manager, as follows:
+
+[source,xml]
+-------------------------------------------------------------------
+<bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
+   <property name="entityManagerFactory" ref="myEMFactory"/>
+   <property name="transactionManager" ref="myTransactionManager"/>
+</bean>
+-------------------------------------------------------------------
+
+[[JPA-Usingaconsumerwithanamedquery]]
+Using a consumer with a named query
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+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:
+
+[source,java]
+----------------------------------------------------------------------------------
+@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:
+
+[source,java]
+----------------------------------------------------------------------------
+from("jpa://org.apache.camel.examples.MultiSteps?consumer.namedQuery=step1")
+.to("bean:myBusinessLogic");
+----------------------------------------------------------------------------
+
+[[JPA-Usingaconsumerwithaquery]]
+Using a consumer with a query
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+For consuming only selected entities, you can use the `consumer.query`
+URI query option. You only have to define the query option:
+
+[source,java]
+---------------------------------------------------------------------------------------------------------------------------------------
+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");
+---------------------------------------------------------------------------------------------------------------------------------------
+
+[[JPA-Usingaconsumerwithanativequery]]
+Using a consumer with a native query
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+For consuming only selected entities, you can use the
+`consumer.nativeQuery` URI query option. You only have to define the
+native query option:
+
+[source,java]
+---------------------------------------------------------------------------------------------------------------
+from("jpa://org.apache.camel.examples.MultiSteps?consumer.nativeQuery=select * from MultiSteps where step = 1")
+.to("bean:myBusinessLogic");
+---------------------------------------------------------------------------------------------------------------
+
+If you use the native query option, you will receive an object array in
+the message body.
+
+[[JPA-Example]]
+Example
+^^^^^^^
+
+See link:tracer-example.html[Tracer Example] for an example using
+link:jpa.html[JPA] to store traced messages into a database.
+
+[[JPA-UsingtheJPAbasedidempotentrepository]]
+Using the JPA based idempotent repository
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+In this section we will use the JPA based idempotent repository.
+
+First we need to setup a `persistence-unit` in the persistence.xml file:
+
+Second we have to setup a `org.springframework.orm.jpa.JpaTemplate`
+which is used by the
+`org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository`:
+
+Error formatting macro: snippet: java.lang.IndexOutOfBoundsException:
+Index: 20, Size: 20
+
+Afterwards we can configure our
+`org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository`:
+
+And finally we can create our JPA idempotent repository in the spring
+XML file as well:
+
+[source,xml]
+---------------------------------------------------------------
+<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>
+---------------------------------------------------------------
+
+*When running this Camel component tests inside your IDE*
+
+In case you run the
+https://svn.apache.org/repos/asf/camel/trunk/components/camel-jpa/src/test[tests
+of this component] directly inside your IDE (and not necessarily through
+Maven itself) then you could spot exceptions like:
+
+[source,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
+https://svn.apache.org/repos/asf/camel/trunk/components/camel-jpa/pom.xml[enhance
+the byte-code at build time]. To overcome this you would need to enable
+http://openjpa.apache.org/entity-enhancement.html#dynamic-enhancement[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:
+
+[source,java]
+-------------------------------------------------------------------------------------------
+ 
+-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
+image:https://cwiki.apache.org/confluence/s/en_GB/5982/f2b47fb3d636c8bc9fd0b11c0ec6d0ae18646be7.1/_/images/icons/emoticons/smile.png[(smile)]
+
+[[JPA-SeeAlso]]
+See Also
+^^^^^^^^
+
+* link:configuring-camel.html[Configuring Camel]
+* link:component.html[Component]
+* link:endpoint.html[Endpoint]
+* link:getting-started.html[Getting Started]
+
+* link:tracer-example.html[Tracer Example]
+

http://git-wip-us.apache.org/repos/asf/camel/blob/65aea6a5/docs/user-manual/en/SUMMARY.md
----------------------------------------------------------------------
diff --git a/docs/user-manual/en/SUMMARY.md b/docs/user-manual/en/SUMMARY.md
index 8eadaa2..a8df1e8 100644
--- a/docs/user-manual/en/SUMMARY.md
+++ b/docs/user-manual/en/SUMMARY.md
@@ -175,6 +175,7 @@
     * [Jing](jing.adoc)
     * [JIRA](jira.adoc)
     * [Jolt](jolt.adoc)
+    * [Jpa](jpa.adoc)
     * [Kafka](kafka.adoc)
     * [Metrics](metrics.adoc)
     * [Mock](mock.adoc)