camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [3/4] git commit: CAMEL-7372 Allow users to pass in their own EntityManager through the exchange
Date Wed, 16 Apr 2014 15:06:48 GMT
CAMEL-7372 Allow users to pass in their own EntityManager through the exchange


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

Branch: refs/heads/camel-2.12.x
Commit: c11b61f3ff484c2d53c1bb50d138745f0236eff9
Parents: 5618c48
Author: Tomohisa Igarashi <tm.igarashi@gmail.com>
Authored: Wed Apr 16 22:04:02 2014 +0900
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Wed Apr 16 08:07:54 2014 -0700

----------------------------------------------------------------------
 .../apache/camel/component/jpa/JpaEndpoint.java |  9 ++
 .../apache/camel/component/jpa/JpaProducer.java | 26 ++++--
 .../JpaProducerPassingEntityManagerTest.java    | 86 ++++++++++++++++++++
 3 files changed, 114 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c11b61f3/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
index 4d8eb00..5e3e6c9 100644
--- a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
+++ b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
@@ -54,6 +54,7 @@ public class JpaEndpoint extends ScheduledPollEndpoint {
     private int maxMessagesPerPoll;
     private boolean usePersist;
     private boolean joinTransaction = true;
+    private boolean usePassedInEntityManager = false;
 
     public JpaEndpoint() {
     }
@@ -250,6 +251,14 @@ public class JpaEndpoint extends ScheduledPollEndpoint {
         this.joinTransaction = joinTransaction;
     }
 
+    public boolean isUsePassedInEntityManager() {
+        return this.usePassedInEntityManager;
+    }
+
+    public void setUsePassedInEntityManager(boolean usePassedIn) {
+        this.usePassedInEntityManager = usePassedIn;
+    }
+
     // Implementation methods
     // -------------------------------------------------------------------------
 

http://git-wip-us.apache.org/repos/asf/camel/blob/c11b61f3/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
index 8afb038..a633e89 100644
--- a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
+++ b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
@@ -51,14 +51,15 @@ public class JpaProducer extends DefaultProducer {
     }
 
     public void process(final Exchange exchange) {
-        exchange.getIn().setHeader(JpaConstants.ENTITYMANAGER, entityManager);
+        final EntityManager targetEntityManager = getTargetEntityManager(exchange);
+        exchange.getIn().setHeader(JpaConstants.ENTITYMANAGER, targetEntityManager);
 
         final Object values = expression.evaluate(exchange, Object.class);
         if (values != null) {
             transactionTemplate.execute(new TransactionCallback<Object>() {
                 public Object doInTransaction(TransactionStatus status) {
                     if (getEndpoint().isJoinTransaction()) {
-                        entityManager.joinTransaction();
+                        targetEntityManager.joinTransaction();
                     }
                     if (values.getClass().isArray()) {
                         Object[] array = (Object[])values;
@@ -80,9 +81,9 @@ public class JpaProducer extends DefaultProducer {
                     if (getEndpoint().isFlushOnSend()) {
                         // there may be concurrency so need to join tx before flush
                         if (getEndpoint().isJoinTransaction()) {
-                            entityManager.joinTransaction();
+                            targetEntityManager.joinTransaction();
                         }
-                        entityManager.flush();
+                        targetEntityManager.flush();
                     }
 
                     return null;
@@ -94,18 +95,29 @@ public class JpaProducer extends DefaultProducer {
                  */
                 private Object save(final Object entity) {
                     // there may be concurrency so need to join tx before persist/merge
-                    entityManager.joinTransaction();
+                    targetEntityManager.joinTransaction();
                     if (getEndpoint().isUsePersist()) {
-                        entityManager.persist(entity);
+                        targetEntityManager.persist(entity);
                         return entity;
                     } else {
-                        return entityManager.merge(entity);
+                        return targetEntityManager.merge(entity);
                     }
                 }
             });
         }
     }
 
+    private EntityManager getTargetEntityManager(Exchange exchange) {
+        EntityManager em = this.entityManager;
+        if (getEndpoint().isUsePassedInEntityManager()) {
+            EntityManager passedIn = exchange.getIn().getHeader(JpaConstants.ENTITYMANAGER,
EntityManager.class);
+            if (passedIn != null) {
+                em = passedIn;
+            }
+        }
+        return em;
+    }
+
     @Override
     protected void doShutdown() throws Exception {
         super.doShutdown();

http://git-wip-us.apache.org/repos/asf/camel/blob/c11b61f3/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerPassingEntityManagerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerPassingEntityManagerTest.java
b/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerPassingEntityManagerTest.java
new file mode 100644
index 0000000..1f04de2
--- /dev/null
+++ b/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerPassingEntityManagerTest.java
@@ -0,0 +1,86 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor.jpa;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jpa.JpaConstants;
+import org.apache.camel.component.jpa.JpaEndpoint;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.examples.SendEmail;
+import org.apache.camel.spring.SpringRouteBuilder;
+import org.junit.Test;
+
+/**
+ * @version 
+ */
+public class JpaProducerPassingEntityManagerTest extends AbstractJpaTest {
+    protected static final String SELECT_ALL_STRING = "select x from " + SendEmail.class.getName()
+ " x";
+
+    @Test
+    public void testRouteJpa() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        context.startRoute("foo");
+        JpaEndpoint jpa = context.getEndpoint("jpa://" + SendEmail.class.getName(), JpaEndpoint.class);
+        EntityManagerFactory emf = jpa.getEntityManagerFactory();
+
+        // The entity instance is different if it is retrieved from different EntityManager
instance
+        EntityManager entityManager = emf.createEntityManager();
+        template.sendBody("direct:start", new SendEmail("foo@beer.org"));
+        Exchange exchange = mock.getReceivedExchanges().get(0);
+        SendEmail persistedEntity = exchange.getIn().getBody(SendEmail.class);
+        SendEmail emfindEntity = entityManager.find(SendEmail.class, persistedEntity.getId());
+        assertNotSame(emfindEntity, persistedEntity);
+        entityManager.close();
+        mock.reset();
+
+        // The same EntityManager returns same entity instance from its 1st level cache
+        entityManager = emf.createEntityManager();
+        template.sendBodyAndHeader("direct:start", new SendEmail("bar@beer.org"), JpaConstants.ENTITYMANAGER,
entityManager);
+        exchange = mock.getReceivedExchanges().get(0);
+        persistedEntity = exchange.getIn().getBody(SendEmail.class);
+        emfindEntity = entityManager.find(SendEmail.class, persistedEntity.getId());
+        assertSame(emfindEntity, persistedEntity);
+        entityManager.close();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new SpringRouteBuilder() {
+            public void configure() {
+                from("direct:start")
+                    .id("foo")
+                    .to("jpa://" + SendEmail.class.getName() + "?usePassedInEntityManager=true")
+                    .to("mock:result");
+            }
+        };
+    }
+
+    @Override
+    protected String routeXml() {
+        return "org/apache/camel/processor/jpa/springJpaRouteTest.xml";
+    }
+
+    @Override
+    protected String selectAllString() {
+        return SELECT_ALL_STRING;
+    }
+
+}


Mime
View raw message