Updated Branches:
refs/heads/master db5cf14c6 -> 2798d9301
[CAMEL-7126] Created EntityManagerTemplate.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/2798d930
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/2798d930
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/2798d930
Branch: refs/heads/master
Commit: 2798d9301e5708964b41adf265d4ca9906be70e8
Parents: db5cf14
Author: Henryk Konsek <hekonsek@gmail.com>
Authored: Thu Jan 23 18:07:37 2014 +0100
Committer: Henryk Konsek <hekonsek@gmail.com>
Committed: Thu Jan 23 18:07:37 2014 +0100
----------------------------------------------------------------------
.../apache/camel/bam/EntityManagerCallback.java | 25 ++++++++
.../apache/camel/bam/EntityManagerTemplate.java | 43 ++++++++++++++
.../org/apache/camel/bam/EntityManagers.java | 5 +-
.../camel/bam/model/ProcessDefinition.java | 60 ++++++++++----------
.../bam/processor/JpaBamProcessorSupport.java | 60 +++++++++++++-------
5 files changed, 140 insertions(+), 53 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/2798d930/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagerCallback.java
----------------------------------------------------------------------
diff --git a/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagerCallback.java
b/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagerCallback.java
new file mode 100644
index 0000000..5305fc1
--- /dev/null
+++ b/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagerCallback.java
@@ -0,0 +1,25 @@
+/**
+ * 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.bam;
+
+import javax.persistence.EntityManager;
+
+public interface EntityManagerCallback<T> {
+
+ T execute(EntityManager entityManager);
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/2798d930/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagerTemplate.java
----------------------------------------------------------------------
diff --git a/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagerTemplate.java
b/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagerTemplate.java
new file mode 100644
index 0000000..50db9bf
--- /dev/null
+++ b/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagerTemplate.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.bam;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+import static org.apache.camel.bam.EntityManagers.closeNonTransactionalEntityManager;
+import static org.apache.camel.bam.EntityManagers.resolveEntityManager;
+
+public class EntityManagerTemplate {
+
+ private final EntityManagerFactory entityManagerFactory;
+
+ public EntityManagerTemplate(EntityManagerFactory entityManagerFactory) {
+ this.entityManagerFactory = entityManagerFactory;
+ }
+
+ public <T> T execute(EntityManagerCallback<T> entityManagerCallback) {
+ EntityManager entityManager = null;
+ try {
+ entityManager = resolveEntityManager(entityManagerFactory);
+ return entityManagerCallback.execute(entityManager);
+ } finally {
+ closeNonTransactionalEntityManager(entityManager);
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/2798d930/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagers.java
----------------------------------------------------------------------
diff --git a/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagers.java b/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagers.java
index e34db2b..297bef4 100644
--- a/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagers.java
+++ b/components/camel-bam/src/main/java/org/apache/camel/bam/EntityManagers.java
@@ -48,8 +48,11 @@ public final class EntityManagers {
}
public static void closeNonTransactionalEntityManager(EntityManager entityManager) {
+ if (entityManager == null) {
+ return;
+ }
boolean isTransactional = TransactionSynchronizationManager.hasResource(entityManager.getEntityManagerFactory());
- if (entityManager != null && isTransactional) {
+ if (isTransactional) {
entityManager.close();
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/2798d930/components/camel-bam/src/main/java/org/apache/camel/bam/model/ProcessDefinition.java
----------------------------------------------------------------------
diff --git a/components/camel-bam/src/main/java/org/apache/camel/bam/model/ProcessDefinition.java
b/components/camel-bam/src/main/java/org/apache/camel/bam/model/ProcessDefinition.java
index 42d73df..1d07785 100644
--- a/components/camel-bam/src/main/java/org/apache/camel/bam/model/ProcessDefinition.java
+++ b/components/camel-bam/src/main/java/org/apache/camel/bam/model/ProcessDefinition.java
@@ -22,15 +22,15 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Table;
+
+import org.apache.camel.bam.EntityManagerCallback;
+import org.apache.camel.bam.EntityManagerTemplate;
import org.apache.camel.bam.QueryUtils;
import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.jpa.JpaTemplate;
-import static org.apache.camel.bam.EntityManagers.closeNonTransactionalEntityManager;
-import static org.apache.camel.bam.EntityManagers.resolveEntityManager;
-
/**
* @version
*/
@@ -51,44 +51,44 @@ public class ProcessDefinition extends EntitySupport {
}
public static ProcessDefinition getRefreshedProcessDefinition(JpaTemplate template, ProcessDefinition
definition) {
+ EntityManagerTemplate entityManagerTemplate = new EntityManagerTemplate(template.getEntityManagerFactory());
// TODO refresh doesn't tend to work - maybe its a spring thing?
// template.refresh(definition);
ObjectHelper.notNull(definition, "definition");
- Long id = definition.getId();
+ final Long id = definition.getId();
if (id == null) {
LOG.warn("No primary key is available!");
return findOrCreateProcessDefinition(template, definition.getName());
}
- EntityManager entityManager = null;
- try {
- entityManager = resolveEntityManager(template.getEntityManagerFactory());
- definition = entityManager.find(ProcessDefinition.class, id);
- } finally {
- closeNonTransactionalEntityManager(entityManager);
- }
- return definition;
+ return entityManagerTemplate.execute(new EntityManagerCallback<ProcessDefinition>()
{
+ @Override
+ public ProcessDefinition execute(EntityManager entityManager) {
+ return entityManager.find(ProcessDefinition.class, id);
+ }
+ });
}
- public static ProcessDefinition findOrCreateProcessDefinition(JpaTemplate template, String
processName) {
- EntityManager entityManager = null;
- try {
- entityManager = resolveEntityManager(template.getEntityManagerFactory());
- String definitionsQuery = "select x from " + QueryUtils.getTypeName(ProcessDefinition.class)
- + " x where x.name = :processName";
- List<ProcessDefinition> list = entityManager.createQuery(definitionsQuery,
ProcessDefinition.class).
- setParameter("processName", processName).
- getResultList();
- if (!list.isEmpty()) {
- return list.get(0);
- } else {
- ProcessDefinition answer = new ProcessDefinition();
- answer.setName(processName);
- template.persist(answer);
- return answer;
+ public static ProcessDefinition findOrCreateProcessDefinition(JpaTemplate template, final
String processName) {
+ EntityManagerTemplate entityManagerTemplate = new EntityManagerTemplate(template.getEntityManagerFactory());
+ final String definitionsQuery = "select x from " + QueryUtils.getTypeName(ProcessDefinition.class)
+ + " x where x.name = :processName";
+ List<ProcessDefinition> list = entityManagerTemplate.execute(new EntityManagerCallback<List<ProcessDefinition>>()
{
+ @Override
+ public List<ProcessDefinition> execute(EntityManager entityManager) {
+ return entityManager.createQuery(definitionsQuery, ProcessDefinition.class).
+ setParameter("processName", processName).
+ getResultList();
}
- } finally {
- closeNonTransactionalEntityManager(entityManager);
+ });
+ if (!list.isEmpty()) {
+ return list.get(0);
+ } else {
+ ProcessDefinition answer = new ProcessDefinition();
+ answer.setName(processName);
+ template.persist(answer);
+ return answer;
}
}
+
}
http://git-wip-us.apache.org/repos/asf/camel/blob/2798d930/components/camel-bam/src/main/java/org/apache/camel/bam/processor/JpaBamProcessorSupport.java
----------------------------------------------------------------------
diff --git a/components/camel-bam/src/main/java/org/apache/camel/bam/processor/JpaBamProcessorSupport.java
b/components/camel-bam/src/main/java/org/apache/camel/bam/processor/JpaBamProcessorSupport.java
index 69109cd..e728f50 100644
--- a/components/camel-bam/src/main/java/org/apache/camel/bam/processor/JpaBamProcessorSupport.java
+++ b/components/camel-bam/src/main/java/org/apache/camel/bam/processor/JpaBamProcessorSupport.java
@@ -26,6 +26,8 @@ import javax.persistence.EntityManager;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.Processor;
+import org.apache.camel.bam.EntityManagerCallback;
+import org.apache.camel.bam.EntityManagerTemplate;
import org.apache.camel.bam.QueryUtils;
import org.apache.camel.bam.model.ProcessDefinition;
import org.apache.camel.bam.rules.ActivityRules;
@@ -35,9 +37,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.transaction.support.TransactionTemplate;
-import static org.apache.camel.bam.EntityManagers.closeNonTransactionalEntityManager;
-import static org.apache.camel.bam.EntityManagers.resolveEntityManager;
-
/**
* A base class for JPA based BAM which can use any entity to store the process
* instance information which allows derived classes to specialise the process
@@ -52,6 +51,7 @@ public class JpaBamProcessorSupport<T> extends BamProcessorSupport<T>
{
private ActivityRules activityRules;
private JpaTemplate template;
+ private EntityManagerTemplate entityManagerTemplate;
private String findByKeyQuery;
private String keyPropertyName = "correlationKey";
private boolean correlationKeyIsPrimary = true;
@@ -61,6 +61,7 @@ public class JpaBamProcessorSupport<T> extends BamProcessorSupport<T>
{
super(transactionTemplate, correlationKeyExpression, entitytype);
this.activityRules = activityRules;
this.template = template;
+ this.entityManagerTemplate = new EntityManagerTemplate(template.getEntityManagerFactory());
}
public JpaBamProcessorSupport(TransactionTemplate transactionTemplate, JpaTemplate template,
@@ -68,6 +69,7 @@ public class JpaBamProcessorSupport<T> extends BamProcessorSupport<T>
{
super(transactionTemplate, correlationKeyExpression);
this.activityRules = activityRules;
this.template = template;
+ this.entityManagerTemplate = new EntityManagerTemplate(template.getEntityManagerFactory());
}
public String getFindByKeyQuery() {
@@ -117,10 +119,8 @@ public class JpaBamProcessorSupport<T> extends BamProcessorSupport<T>
{
// -----------------------------------------------------------------------
protected T loadEntity(Exchange exchange, Object key) throws Exception {
LOCK.lock();
- EntityManager entityManager = null;
try {
LOG.trace("LoadEntity call");
- entityManager = resolveEntityManager(template.getEntityManagerFactory());
T entity = findEntityByCorrelationKey(key);
if (entity == null) {
entity = createEntity(exchange, key);
@@ -128,37 +128,53 @@ public class JpaBamProcessorSupport<T> extends BamProcessorSupport<T>
{
ProcessDefinition definition = ProcessDefinition.getRefreshedProcessDefinition(template,
getActivityRules().getProcessRules().getProcessDefinition());
setProcessDefinitionProperty(entity, definition);
- entityManager.persist(entity);
+ final T finalEntity = entity;
+ entityManagerTemplate.execute(new EntityManagerCallback<Object>() {
+ @Override
+ public Object execute(EntityManager entityManager) {
+ entityManager.persist(finalEntity);
+ return null;
+ }
+ });
// Now we must flush to avoid concurrent updates clashing trying to
// insert the same row
LOG.debug("About to flush on entity: {} with key: {}", entity, key);
- entityManager.flush();
+ entityManagerTemplate.execute(new EntityManagerCallback<Object>() {
+ @Override
+ public Object execute(EntityManager entityManager) {
+ entityManager.flush();
+ return null;
+ }
+ });
}
return entity;
} finally {
LOCK.unlock();
- closeNonTransactionalEntityManager(entityManager);
}
}
@SuppressWarnings("unchecked")
- protected T findEntityByCorrelationKey(Object key) {
- EntityManager entityManager = null;
- try {
- entityManager = resolveEntityManager(template.getEntityManagerFactory());
- if (isCorrelationKeyIsPrimary()) {
- return entityManager.find(getEntityType(), key);
- } else {
- List<T> list = entityManager.createQuery(getFindByKeyQuery()).setParameter("key",
key).getResultList();
- if (list.isEmpty()) {
- return null;
- } else {
- return list.get(0);
+ protected T findEntityByCorrelationKey(final Object key) {
+ if (isCorrelationKeyIsPrimary()) {
+ return entityManagerTemplate.execute(new EntityManagerCallback<T>() {
+ @Override
+ public T execute(EntityManager entityManager) {
+ return entityManager.find(getEntityType(), key);
}
+ });
+ } else {
+ List<T> list = entityManagerTemplate.execute(new EntityManagerCallback<List<T>>()
{
+ @Override
+ public List<T> execute(EntityManager entityManager) {
+ return entityManager.createQuery(getFindByKeyQuery()).setParameter("key",
key).getResultList();
+ }
+ });
+ if (list.isEmpty()) {
+ return null;
+ } else {
+ return list.get(0);
}
- } finally {
- closeNonTransactionalEntityManager(entityManager);
}
}
|