Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id F24D5116CE for ; Thu, 18 Sep 2014 12:40:38 +0000 (UTC) Received: (qmail 79519 invoked by uid 500); 18 Sep 2014 12:40:38 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 79470 invoked by uid 500); 18 Sep 2014 12:40:38 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 79461 invoked by uid 99); 18 Sep 2014 12:40:38 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 18 Sep 2014 12:40:38 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 75D9AA1AD46; Thu, 18 Sep 2014 12:40:38 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: ningjiang@apache.org To: commits@camel.apache.org Message-Id: <7ce51fdeab604ef59c0ecf72c83a3402@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: git commit: CAMEL-7828 Support the pass the exchange as the parameter to @PreConsumed and @Consumed method Date: Thu, 18 Sep 2014 12:40:38 +0000 (UTC) Repository: camel Updated Branches: refs/heads/master 75f2b354e -> 70a637fb2 CAMEL-7828 Support the pass the exchange as the parameter to @PreConsumed and @Consumed method Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/70a637fb Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/70a637fb Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/70a637fb Branch: refs/heads/master Commit: 70a637fb210149c8a87251edc971d70e2f49c5a5 Parents: 75f2b35 Author: Willem Jiang Authored: Thu Sep 18 20:29:27 2014 +0800 Committer: Willem Jiang Committed: Thu Sep 18 20:29:27 2014 +0800 ---------------------------------------------------------------------- .../camel/component/jpa/DeleteHandler.java | 5 +- .../apache/camel/component/jpa/JpaConsumer.java | 56 +++++++++++++------- .../org/apache/camel/examples/MultiSteps.java | 11 +++- 3 files changed, 52 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/70a637fb/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/DeleteHandler.java ---------------------------------------------------------------------- diff --git a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/DeleteHandler.java b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/DeleteHandler.java index f62da9d..820ce36 100644 --- a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/DeleteHandler.java +++ b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/DeleteHandler.java @@ -18,6 +18,8 @@ package org.apache.camel.component.jpa; import javax.persistence.EntityManager; +import org.apache.camel.Exchange; + /** * A strategy for deleting entity beans which have been processed; either by a real delete or by an update of some * application specific properties so that the entity bean will not be found in future polling queries. @@ -32,6 +34,7 @@ public interface DeleteHandler { * * @param entityManager the entity manager * @param entityBean the entity bean that has been processed and should be deleted + * @param exchange the exchange that could be used to update the entityBean */ - void deleteObject(EntityManager entityManager, Object entityBean); + void deleteObject(EntityManager entityManager, Object entityBean, Exchange exchange); } http://git-wip-us.apache.org/repos/asf/camel/blob/70a637fb/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java index 95c9d26..9824f7d 100644 --- a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java +++ b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java @@ -169,10 +169,9 @@ public class JpaConsumer extends ScheduledBatchPollingConsumer { // update pending number of exchanges pendingExchanges = total - index - 1; - if (lockEntity(result, entityManager)) { // Run the @PreConsumed callback - createPreDeleteHandler().deleteObject(entityManager, result); + createPreDeleteHandler().deleteObject(entityManager, result, exchange); // process the current exchange LOG.debug("Processing exchange: {}", exchange); @@ -183,7 +182,7 @@ public class JpaConsumer extends ScheduledBatchPollingConsumer { } // Run the @Consumed callback - getDeleteHandler().deleteObject(entityManager, result); + getDeleteHandler().deleteObject(entityManager, result, exchange); } } @@ -393,7 +392,7 @@ public class JpaConsumer extends ScheduledBatchPollingConsumer { protected DeleteHandler createPreDeleteHandler() { // Look for @PreConsumed to allow custom callback before the Entity has been consumed - Class entityType = getEndpoint().getEntityType(); + final Class entityType = getEndpoint().getEntityType(); if (entityType != null) { // Inspect the method(s) annotated with @PreConsumed List methods = ObjectHelper.findMethodsWithAnnotation(entityType, PreConsumed.class); @@ -401,16 +400,19 @@ public class JpaConsumer extends ScheduledBatchPollingConsumer { throw new IllegalStateException("Only one method can be annotated with the @PreConsumed annotation but found: " + methods); } else if (methods.size() == 1) { // Inspect the parameters of the @PreConsumed method - Class[] parameters = methods.get(0).getParameterTypes(); - if (parameters.length != 0) { - throw new IllegalStateException("@PreConsumed annotated method cannot have parameters!"); - } - final Method method = methods.get(0); + final boolean useExchangeParameter = checkParameters(method); return new DeleteHandler() { @Override - public void deleteObject(EntityManager entityManager, Object entityBean) { - ObjectHelper.invokeMethod(method, entityBean); + public void deleteObject(EntityManager entityManager, Object entityBean, Exchange exchange) { + // The entityBean could be an Object array + if (entityType.isInstance(entityBean)) { + if (useExchangeParameter) { + ObjectHelper.invokeMethod(method, entityBean, exchange); + } else { + ObjectHelper.invokeMethod(method, entityBean); + } + } } }; } @@ -419,7 +421,7 @@ public class JpaConsumer extends ScheduledBatchPollingConsumer { // else do nothing return new DeleteHandler() { @Override - public void deleteObject(EntityManager entityManager, Object entityBean) { + public void deleteObject(EntityManager entityManager, Object entityBean, Exchange exchange) { // Do nothing } }; @@ -427,35 +429,53 @@ public class JpaConsumer extends ScheduledBatchPollingConsumer { protected DeleteHandler createDeleteHandler() { // look for @Consumed to allow custom callback when the Entity has been consumed - Class entityType = getEndpoint().getEntityType(); + final Class entityType = getEndpoint().getEntityType(); if (entityType != null) { List methods = ObjectHelper.findMethodsWithAnnotation(entityType, Consumed.class); if (methods.size() > 1) { throw new IllegalArgumentException("Only one method can be annotated with the @Consumed annotation but found: " + methods); } else if (methods.size() == 1) { final Method method = methods.get(0); - + final boolean useExchangeParameter = checkParameters(method); return new DeleteHandler() { - public void deleteObject(EntityManager entityManager, Object entityBean) { - ObjectHelper.invokeMethod(method, entityBean); + public void deleteObject(EntityManager entityManager, Object entityBean, Exchange exchange) { + if (entityType.isInstance(entityBean)) { + if (useExchangeParameter) { + ObjectHelper.invokeMethod(method, entityBean, exchange); + } else { + ObjectHelper.invokeMethod(method, entityBean); + } + } } }; } } if (getEndpoint().isConsumeDelete()) { return new DeleteHandler() { - public void deleteObject(EntityManager entityManager, Object entityBean) { + public void deleteObject(EntityManager entityManager, Object entityBean, Exchange exchange) { entityManager.remove(entityBean); } }; } else { return new DeleteHandler() { - public void deleteObject(EntityManager entityManager, Object entityBean) { + public void deleteObject(EntityManager entityManager, Object entityBean, Exchange exchange) { // do nothing } }; } } + + protected boolean checkParameters(Method method) { + boolean result = false; + Class[] parameters = method.getParameterTypes(); + if (parameters.length == 1 && parameters[0].isAssignableFrom(Exchange.class)) { + result = true; + } + if (parameters.length > 0 && !result) { + throw new IllegalStateException("@PreConsumed annotated method cannot have parameter other than Exchange"); + } + return result; + } protected void configureParameters(Query query) { int maxResults = getEndpoint().getMaximumResults(); http://git-wip-us.apache.org/repos/asf/camel/blob/70a637fb/components/camel-jpa/src/test/java/org/apache/camel/examples/MultiSteps.java ---------------------------------------------------------------------- diff --git a/components/camel-jpa/src/test/java/org/apache/camel/examples/MultiSteps.java b/components/camel-jpa/src/test/java/org/apache/camel/examples/MultiSteps.java index afca4a6..fb45349 100644 --- a/components/camel-jpa/src/test/java/org/apache/camel/examples/MultiSteps.java +++ b/components/camel-jpa/src/test/java/org/apache/camel/examples/MultiSteps.java @@ -21,7 +21,10 @@ import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.NamedQuery; +import org.apache.camel.Exchange; import org.apache.camel.component.jpa.Consumed; +import org.apache.camel.component.jpa.PreConsumed; +import org.junit.Assert; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -73,6 +76,13 @@ public class MultiSteps { public void setStep(int step) { this.step = step; } + + @PreConsumed + public void beforeGoToNextStep(Exchange exchange) { + // we could do some thing to update the entity by using the exchange property + Assert.assertNotNull(exchange); + LOG.info("Calling beforeGoToNextStep"); + } /** * This method is invoked after the entity bean is processed successfully by a Camel endpoint @@ -80,7 +90,6 @@ public class MultiSteps { @Consumed public void goToNextStep() { setStep(getStep() + 1); - LOG.info("Invoked the completion complete method. Now updated the step to: {}", getStep()); }