Repository: camel Updated Branches: refs/heads/master bd6f84639 -> 6bd086fac
CAMEL-3142: JpaPollingConsumer - So you can more easily work with pollEnrich Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/6bd086fa Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/6bd086fa Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/6bd086fa Branch: refs/heads/master Commit: 6bd086fac7fb5350712ee5cda07332191ea29780 Parents: bd6f846 Author: Claus Ibsen <[email protected]> Authored: Sun May 1 10:16:24 2016 +0200 Committer: Claus Ibsen <[email protected]> Committed: Sun May 1 10:43:54 2016 +0200 ---------------------------------------------------------------------- .../camel/component/jpa/JpaComponent.java | 19 ++ .../apache/camel/component/jpa/JpaEndpoint.java | 17 ++ .../camel/component/jpa/JpaPollingConsumer.java | 259 +++++++++++++++++++ .../processor/jpa/JpaPollingConsumerTest.java | 93 +++++++ .../jpa/JpaPollingConsumerTimeoutTest.java | 43 +++ 5 files changed, 431 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/6bd086fa/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java index c2c0227..5427e9a 100644 --- a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java +++ b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java @@ -17,6 +17,7 @@ package org.apache.camel.component.jpa; import java.util.Map; +import java.util.concurrent.ExecutorService; import javax.persistence.EntityManagerFactory; import org.apache.camel.Endpoint; @@ -38,6 +39,7 @@ public class JpaComponent extends UriEndpointComponent { private PlatformTransactionManager transactionManager; private boolean joinTransaction = true; private boolean sharedEntityManager; + private ExecutorService pollingConsumerExecutorService; public JpaComponent() { super(JpaEndpoint.class); @@ -93,6 +95,14 @@ public class JpaComponent extends UriEndpointComponent { this.sharedEntityManager = sharedEntityManager; } + synchronized ExecutorService getOrCreatePollingConsumerExecutorService() { + if (pollingConsumerExecutorService == null) { + LOG.debug("Creating thread pool for JpaPollingConsumer to support polling using timeout"); + pollingConsumerExecutorService = getCamelContext().getExecutorServiceManager().newDefaultThreadPool(this, "JpaPollingConsumer"); + } + return pollingConsumerExecutorService; + } + // Implementation methods //------------------------------------------------------------------------- @@ -173,4 +183,13 @@ public class JpaComponent extends UriEndpointComponent { } } + @Override + protected void doStop() throws Exception { + super.doStop(); + + if (pollingConsumerExecutorService != null) { + getCamelContext().getExecutorServiceManager().shutdown(pollingConsumerExecutorService); + pollingConsumerExecutorService = null; + } + } } http://git-wip-us.apache.org/repos/asf/camel/blob/6bd086fa/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 fc97224..c5b9734 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 @@ -26,6 +26,7 @@ import org.apache.camel.Exchange; import org.apache.camel.Expression; import org.apache.camel.InvalidPayloadException; import org.apache.camel.InvalidPayloadRuntimeException; +import org.apache.camel.PollingConsumer; import org.apache.camel.Processor; import org.apache.camel.Producer; import org.apache.camel.impl.ScheduledPollEndpoint; @@ -141,6 +142,11 @@ public class JpaEndpoint extends ScheduledPollEndpoint { this.transactionManager = transactionManager; } + @Override + public JpaComponent getComponent() { + return (JpaComponent) super.getComponent(); + } + public Producer createProducer() throws Exception { validate(); return new JpaProducer(this, getProducerExpression()); @@ -165,6 +171,17 @@ public class JpaEndpoint extends ScheduledPollEndpoint { } @Override + public PollingConsumer createPollingConsumer() throws Exception { + JpaPollingConsumer consumer = new JpaPollingConsumer(this); + consumer.setQuery(getQuery()); + consumer.setNamedQuery(getNamedQuery()); + consumer.setNativeQuery(getNativeQuery()); + consumer.setParameters(getParameters()); + consumer.setResultClass(getResultClass()); + return consumer; + } + + @Override public void configureProperties(Map<String, Object> options) { super.configureProperties(options); Map<String, Object> emProperties = IntrospectionSupport.extractProperties(options, "emf."); http://git-wip-us.apache.org/repos/asf/camel/blob/6bd086fa/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaPollingConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaPollingConsumer.java b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaPollingConsumer.java new file mode 100644 index 0000000..c9ea60d --- /dev/null +++ b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaPollingConsumer.java @@ -0,0 +1,259 @@ +/** + * 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.component.jpa; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import javax.persistence.Entity; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Query; + +import org.apache.camel.Exchange; +import org.apache.camel.impl.PollingConsumerSupport; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.support.TransactionCallback; +import org.springframework.transaction.support.TransactionTemplate; + +import static org.apache.camel.component.jpa.JpaHelper.getTargetEntityManager; + +public class JpaPollingConsumer extends PollingConsumerSupport { + + private static final Logger LOG = LoggerFactory.getLogger(JpaProducer.class); + private transient ExecutorService executorService; + private final EntityManagerFactory entityManagerFactory; + private final TransactionTemplate transactionTemplate; + private String query; + private String namedQuery; + private String nativeQuery; + private Class<?> resultClass; + private QueryFactory queryFactory; + private Map<String, Object> parameters; + + public JpaPollingConsumer(JpaEndpoint endpoint) { + super(endpoint); + this.entityManagerFactory = endpoint.getEntityManagerFactory(); + this.transactionTemplate = endpoint.createTransactionTemplate(); + } + + @Override + public JpaEndpoint getEndpoint() { + return (JpaEndpoint) super.getEndpoint(); + } + + public String getQuery() { + return query; + } + + public void setQuery(String query) { + this.query = query; + } + + public String getNamedQuery() { + return namedQuery; + } + + public void setNamedQuery(String namedQuery) { + this.namedQuery = namedQuery; + } + + public String getNativeQuery() { + return nativeQuery; + } + + public void setNativeQuery(String nativeQuery) { + this.nativeQuery = nativeQuery; + } + + public Class<?> getResultClass() { + return resultClass; + } + + public void setResultClass(Class<?> resultClass) { + this.resultClass = resultClass; + } + + public QueryFactory getQueryFactory() { + return queryFactory; + } + + public void setQueryFactory(QueryFactory queryFactory) { + this.queryFactory = queryFactory; + } + + public Map<String, Object> getParameters() { + return parameters; + } + + public void setParameters(Map<String, Object> parameters) { + this.parameters = parameters; + } + + @Override + public Exchange receive() { + // resolve the entity manager before evaluating the expression + final EntityManager entityManager = getTargetEntityManager(null, entityManagerFactory, + getEndpoint().isUsePassedInEntityManager(), getEndpoint().isSharedEntityManager(), true); + + Object out = transactionTemplate.execute(new TransactionCallback<Object>() { + public Object doInTransaction(TransactionStatus status) { + if (getEndpoint().isJoinTransaction()) { + entityManager.joinTransaction(); + } + + Query query = getQueryFactory().createQuery(entityManager); + configureParameters(query); + LOG.trace("Created query {}", query); + + Object answer; + List<?> results = query.getResultList(); + + if (results != null && results.size() == 1) { + // we only have 1 entity so return that + answer = results.get(0); + } else { + // we have more data so return a list + answer = results; + } + + // commit + LOG.debug("Flushing EntityManager"); + entityManager.flush(); + // must clear after flush + entityManager.clear(); + + return answer; + } + + }); + + Exchange exchange = createExchange(out, entityManager); + exchange.getIn().setBody(out); + return exchange; + } + + @Override + public Exchange receiveNoWait() { + // call receive as-is + return receive(); + } + + @Override + public Exchange receive(long timeout) { + // need to use a thread pool to perform the task so we can support timeout + if (executorService == null) { + executorService = getEndpoint().getComponent().getOrCreatePollingConsumerExecutorService(); + } + + // the task is the receive method + Future<Exchange> future = executorService.submit((Callable<Exchange>) this::receive); + try { + return future.get(timeout, TimeUnit.MILLISECONDS); + } catch (ExecutionException | InterruptedException e) { + throw ObjectHelper.wrapRuntimeCamelException(e); + } catch (TimeoutException e) { + // ignore as we hit timeout then return null + } + + return null; + } + + @Override + protected void doStart() throws Exception { + if (queryFactory == null) { + queryFactory = createQueryFactory(); + if (queryFactory == null) { + throw new IllegalArgumentException("No queryType property configured on this consumer, nor an entityType configured on the endpoint so cannot consume"); + } + } + } + + @Override + protected void doStop() throws Exception { + // noop + + } + + protected void configureParameters(Query query) { + int maxResults = getEndpoint().getMaximumResults(); + if (maxResults > 0) { + query.setMaxResults(maxResults); + } + // setup the parameter + if (parameters != null) { + for (Map.Entry<String, Object> entry : parameters.entrySet()) { + query.setParameter(entry.getKey(), entry.getValue()); + } + } + } + + protected Exchange createExchange(Object result, EntityManager entityManager) { + Exchange exchange = getEndpoint().createExchange(); + exchange.getIn().setBody(result); + exchange.getIn().setHeader(JpaConstants.ENTITY_MANAGER, entityManager); + return exchange; + } + + protected QueryFactory createQueryFactory() { + if (query != null) { + return QueryBuilder.query(query); + } else if (namedQuery != null) { + return QueryBuilder.namedQuery(namedQuery); + } else if (nativeQuery != null) { + if (resultClass != null) { + return QueryBuilder.nativeQuery(nativeQuery, resultClass); + } else { + return QueryBuilder.nativeQuery(nativeQuery); + } + } else { + Class<?> entityType = getEndpoint().getEntityType(); + + if (entityType == null) { + return null; + } else { + // Check if we have a property name on the @Entity annotation + String name = getEntityName(entityType); + if (name != null) { + return QueryBuilder.query("select x from " + name + " x"); + } else { + // Remove package name of the entity to be conform with JPA 1.0 spec + return QueryBuilder.query("select x from " + entityType.getSimpleName() + " x"); + } + } + } + } + + protected String getEntityName(Class<?> clazz) { + Entity entity = clazz.getAnnotation(Entity.class); + + // Check if the property name has been defined for Entity annotation + if (entity != null && !entity.name().equals("")) { + return entity.name(); + } else { + return null; + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/6bd086fa/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaPollingConsumerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaPollingConsumerTest.java b/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaPollingConsumerTest.java new file mode 100644 index 0000000..5b72211 --- /dev/null +++ b/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaPollingConsumerTest.java @@ -0,0 +1,93 @@ +/** + * 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 java.util.List; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.examples.Customer; +import org.apache.camel.spring.SpringRouteBuilder; +import org.junit.Test; +import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.support.TransactionCallback; + +public class JpaPollingConsumerTest extends AbstractJpaTest { + protected static final String SELECT_ALL_STRING = "select x from " + Customer.class.getName() + " x"; + + protected void save(final Customer customer) { + transactionTemplate.execute(new TransactionCallback<Object>() { + public Object doInTransaction(TransactionStatus status) { + entityManager.joinTransaction(); + entityManager.persist(customer); + entityManager.flush(); + return null; + } + }); + } + + protected void assertEntitiesInDatabase(int count, String entity) { + List<?> results = entityManager.createQuery("select o from " + entity + " o").getResultList(); + assertEquals(count, results.size()); + } + + @Test + public void testPollingConsumer() throws Exception { + Customer customer = new Customer(); + customer.setName("Donald Duck"); + save(customer); + Customer customer2 = new Customer(); + customer2.setName("Goofy"); + save(customer2); + + assertEntitiesInDatabase(2, Customer.class.getName()); + + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("Hello Donald Duck how are you today?"); + + template.sendBodyAndHeader("direct:start", "Hello NAME how are you today?", "name", "Donald%"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new SpringRouteBuilder() { + public void configure() { + from("direct:start") + .pollEnrich().simple("jpa://" + Customer.class.getName() + "?query=select c from Customer c where c.name like '${header.name}'") + .aggregationStrategy((a, b) -> { + String name = b.getIn().getBody(Customer.class).getName(); + String phrase = a.getIn().getBody(String.class).replace("NAME", name); + a.getIn().setBody(phrase); + return a; + }) + .to("mock:result"); + } + }; + } + + @Override + protected String routeXml() { + return "org/apache/camel/processor/jpa/springJpaRouteTest.xml"; + } + + @Override + protected String selectAllString() { + return SELECT_ALL_STRING; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/6bd086fa/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaPollingConsumerTimeoutTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaPollingConsumerTimeoutTest.java b/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaPollingConsumerTimeoutTest.java new file mode 100644 index 0000000..4c8a799 --- /dev/null +++ b/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaPollingConsumerTimeoutTest.java @@ -0,0 +1,43 @@ +/** + * 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 org.apache.camel.builder.RouteBuilder; +import org.apache.camel.examples.Customer; +import org.apache.camel.spring.SpringRouteBuilder; + +public class JpaPollingConsumerTimeoutTest extends JpaPollingConsumerTest { + + @Override + protected RouteBuilder createRouteBuilder() { + return new SpringRouteBuilder() { + public void configure() { + from("direct:start") + .pollEnrich().simple("jpa://" + Customer.class.getName() + "?query=select c from Customer c where c.name like '${header.name}'") + .timeout(5000) + .aggregationStrategy((a, b) -> { + String name = b.getIn().getBody(Customer.class).getName(); + String phrase = a.getIn().getBody(String.class).replace("NAME", name); + a.getIn().setBody(phrase); + return a; + }) + .to("mock:result"); + } + }; + } + +} \ No newline at end of file
