Author: jstrachan
Date: Tue Apr 3 08:48:45 2007
New Revision: 525183
URL: http://svn.apache.org/viewvc?view=rev&rev=525183
Log:
reuse the spring transaction handling utilities so that things play nice with springs declarative
transaction handling
Added:
activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/Callback.java
(with props)
activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/DefaultTransactionStrategy.java
(with props)
activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaTemplateTransactionStrategy.java
(with props)
activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/TransactionStrategy.java
(with props)
Removed:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/Provider.java
Modified:
activemq/camel/trunk/camel-core/src/test/ide-resources/log4j.properties
activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/DeleteHandler.java
activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java
activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java
activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/QueryBuilder.java
activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/QueryFactory.java
activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java
Modified: activemq/camel/trunk/camel-core/src/test/ide-resources/log4j.properties
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/ide-resources/log4j.properties?view=diff&rev=525183&r1=525182&r2=525183
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/ide-resources/log4j.properties (original)
+++ activemq/camel/trunk/camel-core/src/test/ide-resources/log4j.properties Tue Apr 3 08:48:45
2007
@@ -26,6 +26,7 @@
#log4j.logger.org.apache.activemq=DEBUG
log4j.logger.org.apache.cxf=WARN
+log4j.logger.org.springframework=INFO
# CONSOLE appender not used by default
log4j.appender.out=org.apache.log4j.ConsoleAppender
Added: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/Callback.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/Callback.java?view=auto&rev=525183
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/Callback.java
(added)
+++ activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/Callback.java
Tue Apr 3 08:48:45 2007
@@ -0,0 +1,26 @@
+/**
+ *
+ * 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;
+
+/**
+ * @version $Revision$
+ */
+public interface Callback<R,P> {
+
+ R callback(P parameter);
+}
Propchange: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/Callback.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/Callback.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/Callback.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/DefaultTransactionStrategy.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/DefaultTransactionStrategy.java?view=auto&rev=525183
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/DefaultTransactionStrategy.java
(added)
+++ activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/DefaultTransactionStrategy.java
Tue Apr 3 08:48:45 2007
@@ -0,0 +1,81 @@
+/**
+ *
+ * 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 org.apache.camel.impl.ServiceSupport;
+import static org.apache.camel.util.ObjectHelper.notNull;
+import org.springframework.orm.jpa.JpaCallback;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+
+/**
+ * @version $Revision$
+ */
+public class DefaultTransactionStrategy extends ServiceSupport implements TransactionStrategy
{
+ private EntityManagerFactory entityManagerFactory;
+ private EntityManager entityManager;
+
+ public DefaultTransactionStrategy(EntityManagerFactory entityManagerFactory) {
+ notNull(entityManagerFactory, "entityManagerFactory");
+ this.entityManagerFactory = entityManagerFactory;
+ }
+
+ public DefaultTransactionStrategy(EntityManager entityManager) {
+ notNull(entityManager, "entityManager");
+ this.entityManager = entityManager;
+ }
+
+ public Object execute(JpaCallback callback) {
+ EntityManager em = getEntityManager();
+ EntityTransaction transaction = em.getTransaction();
+ transaction.begin();
+
+ try {
+ Object answer = callback.doInJpa(em);
+ transaction.commit();
+ return answer;
+ }
+ catch (RuntimeException e) {
+ if (transaction != null) {
+ transaction.rollback();
+ }
+ throw e;
+ }
+ }
+
+ public EntityManager getEntityManager() {
+ if (entityManager == null){
+ entityManager = entityManagerFactory.createEntityManager();
+ }
+ return entityManager;
+ }
+
+ protected void doStart() throws Exception {
+ // force lazy construction
+ getEntityManager();
+ }
+
+ protected void doStop() throws Exception {
+ if (entityManager != null) {
+ entityManager.close();
+ }
+ }
+
+}
Propchange: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/DefaultTransactionStrategy.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/DefaultTransactionStrategy.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/DefaultTransactionStrategy.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/DeleteHandler.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/DeleteHandler.java?view=diff&rev=525183&r1=525182&r2=525183
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/DeleteHandler.java
(original)
+++ activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/DeleteHandler.java
Tue Apr 3 08:48:45 2007
@@ -31,8 +31,8 @@
* Deletes the entity bean after it has been processed either by actually
* deleting the object or updating it in a way so that future queries do not return this
object again.
*
- * @param consumer the consumer which has access to the {@link JpaEndpoint} and JPA {@link
EntityManager}
+ * @param entityManager
* @param entityBean the entity bean that has been processed and should be deleted
*/
- void deleteObject(JpaConsumer consumer, T entityBean);
+ void deleteObject(EntityManager entityManager, Object entityBean);
}
Modified: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java?view=diff&rev=525183&r1=525182&r2=525183
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java
(original)
+++ activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java
Tue Apr 3 08:48:45 2007
@@ -22,14 +22,31 @@
import org.apache.camel.Endpoint;
import org.apache.camel.EndpointResolver;
import org.apache.camel.Exchange;
+import org.apache.camel.Service;
import org.apache.camel.impl.DefaultComponent;
+import org.apache.camel.impl.ServiceSupport;
import org.apache.camel.util.ObjectHelper;
+import org.springframework.orm.jpa.JpaTemplate;
+import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;
+import org.springframework.orm.jpa.JpaTransactionManager;
+import org.springframework.orm.jpa.SharedEntityManagerCreator;
+import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
+import org.springframework.orm.jpa.JpaCallback;
+import org.springframework.jca.support.LocalConnectionFactoryBean;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.TransactionStatus;
+import org.springframework.transaction.support.TransactionTemplate;
+import org.springframework.transaction.support.TransactionCallback;
+import org.springframework.transaction.jta.JtaTransactionManager;
+import org.springframework.dao.DataAccessException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.net.URI;
import java.util.Map;
+import java.util.List;
+import java.util.ArrayList;
/**
* A JPA Component
@@ -40,6 +57,7 @@
private EntityManagerFactory entityManagerFactory;
private Map entityManagerProperties;
private String entityManagerName = "camel";
+ private JpaTemplate template;
public Component resolveComponent(CamelContext container, String uri) throws Exception
{
return null;
@@ -65,6 +83,17 @@
// Properties
//-------------------------------------------------------------------------
+ public JpaTemplate getTemplate() {
+ if (template == null) {
+ template = createTemplate();
+ }
+ return template;
+ }
+
+ public void setTemplate(JpaTemplate template) {
+ this.template = template;
+ }
+
public EntityManagerFactory getEntityManagerFactory() {
if (entityManagerFactory == null) {
entityManagerFactory = createEntityManagerFactory();
@@ -97,6 +126,34 @@
// Implementation methods
//-------------------------------------------------------------------------
+ protected JpaTemplate createTemplate() {
+ /* EntityManagerFactory emf = getEntityManagerFactory();
+ JpaTransactionManager transactionManager = new JpaTransactionManager(emf);
+ transactionManager.afterPropertiesSet();
+
+ final TransactionTemplate tranasctionTemplate = new TransactionTemplate(transactionManager);
+ tranasctionTemplate.afterPropertiesSet();
+
+ // lets auto-default to a JpaTemplate which implicitly creates a transaction
+ // TODO surely there's a cleaner way to get the JpaTemplate to create a transaction
if one is not present??
+ return new JpaTemplate(emf) {
+ @Override
+ public Object execute(final JpaCallback action, final boolean exposeNativeEntityManager)
throws DataAccessException {
+ return tranasctionTemplate.execute(new TransactionCallback() {
+ public Object doInTransaction(TransactionStatus status) {
+ return doExecute(action, exposeNativeEntityManager);
+ }
+ });
+
+ }
+
+ public Object doExecute(final JpaCallback action, final boolean exposeNativeEntityManager)
throws DataAccessException {
+ return super.execute(action, exposeNativeEntityManager);
+ }
+ };*/
+ return new JpaTemplate(getEntityManagerFactory());
+ }
+
protected EntityManagerFactory createEntityManagerFactory() {
//return Persistence.createEntityManagerFactory(entityManagerName);
return Persistence.createEntityManagerFactory(entityManagerName, getEntityManagerProperties());
Modified: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java?view=diff&rev=525183&r1=525182&r2=525183
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java
(original)
+++ activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java
Tue Apr 3 08:48:45 2007
@@ -22,11 +22,13 @@
import org.apache.camel.impl.PollingConsumer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.springframework.orm.jpa.JpaTemplate;
+import org.springframework.orm.jpa.JpaCallback;
import javax.persistence.EntityManager;
-import javax.persistence.EntityTransaction;
import javax.persistence.LockModeType;
import javax.persistence.Query;
+import javax.persistence.PersistenceException;
import java.util.List;
/**
@@ -35,15 +37,14 @@
public class JpaConsumer extends PollingConsumer<Exchange> {
private static final transient Log log = LogFactory.getLog(JpaConsumer.class);
private final JpaEndpoint endpoint;
- private final EntityManager entityManager;
+ private final TransactionStrategy template;
private QueryFactory queryFactory;
private DeleteHandler<Object> deleteHandler;
- private EntityTransaction transaction;
- public JpaConsumer(JpaEndpoint endpoint, Processor<Exchange> processor, EntityManager
entityManager) {
+ public JpaConsumer(JpaEndpoint endpoint, Processor<Exchange> processor) {
super(endpoint, processor);
this.endpoint = endpoint;
- this.entityManager = entityManager;
+ this.template = endpoint.createTransactionStrategy();
}
/**
@@ -51,44 +52,35 @@
*/
public synchronized void run() {
log.debug("Starting to poll for new database entities to process");
- transaction = entityManager.getTransaction();
- transaction.begin();
-
try {
- Query query = getQueryFactory().createQuery(this);
- configureParameters(query);
- List results = query.getResultList();
- for (Object result : results) {
- if (log.isDebugEnabled()) {
- log.debug("Processing new entity: " + result);
- }
-
- if (lockEntity(result)) {
- // lets turn the result into an exchange and fire it into the processor
- Exchange exchange = createExchange(result);
- getProcessor().onExchange(exchange);
- getDeleteHandler().deleteObject(this, result);
+ template.execute(new JpaCallback() {
+ public Object doInJpa(EntityManager entityManager) throws PersistenceException
{
+ Query query = getQueryFactory().createQuery(entityManager);
+ configureParameters(query);
+ List results = query.getResultList();
+ for (Object result : results) {
+ if (log.isDebugEnabled()) {
+ log.debug("Processing new entity: " + result);
+ }
+
+ if (lockEntity(result, entityManager)) {
+ // lets turn the result into an exchange and fire it into the
processor
+ Exchange exchange = createExchange(result);
+ getProcessor().onExchange(exchange);
+ getDeleteHandler().deleteObject(entityManager, result);
+ }
+ }
+ return null;
}
- }
-
- transaction.commit();
- transaction = null;
+ });
}
catch (RuntimeException e) {
log.warn("Caught: " + e, e);
- if (transaction != null) {
- transaction.rollback();
- }
}
}
// Properties
//-------------------------------------------------------------------------
-
- public EntityManager getEntityManager() {
- return entityManager;
- }
-
public JpaEndpoint getEndpoint() {
return endpoint;
}
@@ -117,22 +109,15 @@
// Implementation methods
//-------------------------------------------------------------------------
- @Override
- protected synchronized void doStop() throws Exception {
- if (transaction != null) {
- transaction.rollback();
- }
- entityManager.close();
- super.doStop();
- }
/**
* A strategy method to lock an object with an exclusive lock so that it can be processed
*
* @param entity the entity to be locked
+ * @param entityManager
* @return true if the entity was locked
*/
- protected boolean lockEntity(Object entity) {
+ protected boolean lockEntity(Object entity, EntityManager entityManager) {
try {
if (log.isDebugEnabled()) {
log.debug("Acquiring exclusive lock on entity: " + entity);
@@ -162,8 +147,8 @@
// TODO auto-discover an annotation in the entity bean to indicate the process completed
method call?
return new DeleteHandler<Object>() {
- public void deleteObject(JpaConsumer consumer, Object entityBean) {
- consumer.getEntityManager().remove(entityBean);
+ public void deleteObject(EntityManager entityManager, Object entityBean) {
+ entityManager.remove(entityBean);
}
};
}
Modified: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java?view=diff&rev=525183&r1=525182&r2=525183
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
(original)
+++ activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
Tue Apr 3 08:48:45 2007
@@ -26,14 +26,17 @@
import org.apache.camel.builder.ExpressionBuilder;
import org.apache.camel.impl.DefaultEndpoint;
import org.apache.camel.impl.DefaultExchange;
+import org.springframework.orm.jpa.JpaTemplate;
import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
/**
* @version $Revision$
*/
public class JpaEndpoint extends DefaultEndpoint<Exchange> {
private final JpaComponent component;
+ private JpaTemplate template;
private Expression<Exchange> producerExpression;
private int maximumResults = -1;
private Class<?> entityType;
@@ -41,6 +44,7 @@
public JpaEndpoint(String uri, JpaComponent component) {
super(uri, component);
this.component = component;
+ this.template = component.getTemplate();
}
public Exchange createExchange() {
@@ -48,15 +52,24 @@
}
public Producer<Exchange> createProducer() throws Exception {
- return startService(new JpaProducer(this, createEntityManager(), getProducerExpression()));
+ return startService(new JpaProducer(this, getProducerExpression()));
}
public Consumer<Exchange> createConsumer(Processor<Exchange> processor) throws
Exception {
- return startService(new JpaConsumer(this, processor, createEntityManager()));
+ return startService(new JpaConsumer(this, processor));
}
+
// Properties
//-------------------------------------------------------------------------
+ public JpaTemplate getTemplate() {
+ return template;
+ }
+
+ public void setTemplate(JpaTemplate template) {
+ this.template = template;
+ }
+
public Expression<Exchange> getProducerExpression() {
if (producerExpression == null) {
producerExpression = createProducerExpression();
@@ -88,6 +101,12 @@
//-------------------------------------------------------------------------
protected EntityManager createEntityManager() {
return component.createEntityManager();
+ }
+
+ protected TransactionStrategy createTransactionStrategy() {
+ EntityManagerFactory emf = component.getEntityManagerFactory();
+ return JpaTemplateTransactionStrategy.newInstance(emf, getTemplate());
+ //return new DefaultTransactionStrategy(emf);
}
protected Expression<Exchange> createProducerExpression() {
Modified: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java?view=diff&rev=525183&r1=525182&r2=525183
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
(original)
+++ activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
Tue Apr 3 08:48:45 2007
@@ -17,54 +17,44 @@
*/
package org.apache.camel.component.jpa;
+import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.converter.ObjectConverter;
import org.apache.camel.impl.DefaultProducer;
+import org.springframework.orm.jpa.JpaTemplate;
+import org.springframework.orm.jpa.JpaCallback;
import javax.persistence.EntityManager;
-import javax.persistence.EntityTransaction;
+import javax.persistence.PersistenceException;
import java.util.Iterator;
/**
* @version $Revision$
*/
public class JpaProducer extends DefaultProducer<Exchange> {
- private EntityManager entityManager;
- private final JpaEndpoint endpoint;
- private Expression<Exchange> expression;
+ private final TransactionStrategy template;
+ private final Expression<Exchange> expression;
- public JpaProducer(JpaEndpoint endpoint, EntityManager entityManager, Expression<Exchange>
expression) {
+ public JpaProducer(JpaEndpoint endpoint, Expression<Exchange> expression) {
super(endpoint);
- this.endpoint = endpoint;
- this.entityManager = entityManager;
this.expression = expression;
+ this.template = endpoint.createTransactionStrategy();
}
public void onExchange(Exchange exchange) {
- Object values = expression.evaluate(exchange);
+ final Object values = expression.evaluate(exchange);
if (values != null) {
- // TODO remove explicit transaction handling?
- EntityTransaction transaction = entityManager.getTransaction();
- transaction.begin();
- try {
- Iterator iter = ObjectConverter.iterator(values);
- while (iter.hasNext()) {
- Object value = iter.next();
- entityManager.persist(value);
+ template.execute(new JpaCallback() {
+ public Object doInJpa(EntityManager entityManager) throws PersistenceException
{
+ Iterator iter = ObjectConverter.iterator(values);
+ while (iter.hasNext()) {
+ Object value = iter.next();
+ entityManager.persist(value);
+ }
+ return null;
}
- transaction.commit();
- }
- catch (RuntimeException e) {
- transaction.rollback();
- throw e;
- }
+ });
}
- }
-
- @Override
- protected void doStop() throws Exception {
- entityManager.close();
- super.doStop();
}
}
Added: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaTemplateTransactionStrategy.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaTemplateTransactionStrategy.java?view=auto&rev=525183
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaTemplateTransactionStrategy.java
(added)
+++ activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaTemplateTransactionStrategy.java
Tue Apr 3 08:48:45 2007
@@ -0,0 +1,81 @@
+/**
+ *
+ * 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 org.apache.camel.impl.ServiceSupport;
+import org.springframework.orm.jpa.JpaCallback;
+import org.springframework.orm.jpa.JpaTemplate;
+import org.springframework.orm.jpa.JpaTransactionManager;
+import org.springframework.transaction.TransactionStatus;
+import org.springframework.transaction.support.TransactionCallback;
+import org.springframework.transaction.support.TransactionTemplate;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceException;
+import javax.persistence.EntityManagerFactory;
+
+/**
+ * Delegates the strategy to the {@link JpaTemplate} and {@link TransactionTemplate} for
transaction handling
+ *
+ * @version $Revision$
+ */
+public class JpaTemplateTransactionStrategy extends ServiceSupport implements TransactionStrategy
{
+ private final JpaTemplate jpaTemplate;
+ private final TransactionTemplate transactionTemplate;
+
+ /**
+ * Creates a new implementation from the given JPA factory
+ */
+ public static JpaTemplateTransactionStrategy newInstance(EntityManagerFactory emf) {
+ JpaTemplate template = new JpaTemplate(emf);
+ return newInstance(emf, template);
+ }
+
+ public static JpaTemplateTransactionStrategy newInstance(EntityManagerFactory emf, JpaTemplate
template) {
+ JpaTransactionManager transactionManager = new JpaTransactionManager(emf);
+ transactionManager.afterPropertiesSet();
+
+ TransactionTemplate tranasctionTemplate = new TransactionTemplate(transactionManager);
+ tranasctionTemplate.afterPropertiesSet();
+
+ return new JpaTemplateTransactionStrategy(template, tranasctionTemplate);
+ }
+
+ public JpaTemplateTransactionStrategy(JpaTemplate jpaTemplate, TransactionTemplate transactionTemplate)
{
+ this.jpaTemplate = jpaTemplate;
+ this.transactionTemplate = transactionTemplate;
+ }
+
+ public Object execute(final JpaCallback callback) {
+ return transactionTemplate.execute(new TransactionCallback() {
+ public Object doInTransaction(TransactionStatus status) {
+ return jpaTemplate.execute(new JpaCallback() {
+ public Object doInJpa(EntityManager entityManager) throws PersistenceException
{
+ return callback.doInJpa(entityManager);
+ }
+ });
+ }
+ });
+ }
+
+ protected void doStart() throws Exception {
+ }
+
+ protected void doStop() throws Exception {
+ }
+}
Propchange: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaTemplateTransactionStrategy.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaTemplateTransactionStrategy.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaTemplateTransactionStrategy.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/QueryBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/QueryBuilder.java?view=diff&rev=525183&r1=525182&r2=525183
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/QueryBuilder.java
(original)
+++ activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/QueryBuilder.java
Tue Apr 3 08:48:45 2007
@@ -18,8 +18,8 @@
package org.apache.camel.component.jpa;
import javax.persistence.Query;
+import javax.persistence.EntityManager;
import java.util.Arrays;
-import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Collection;
@@ -40,8 +40,8 @@
*/
public static QueryBuilder query(final String query) {
return new QueryBuilder() {
- protected Query makeQueryObject(JpaConsumer consumer) {
- return consumer.getEntityManager().createQuery(query);
+ protected Query makeQueryObject(EntityManager entityManager) {
+ return entityManager.createQuery(query);
}
@Override
@@ -56,8 +56,8 @@
*/
public static QueryBuilder namedQuery(final String namedQuery) {
return new QueryBuilder() {
- protected Query makeQueryObject(JpaConsumer consumer) {
- return consumer.getEntityManager().createNamedQuery(namedQuery);
+ protected Query makeQueryObject(EntityManager entityManager) {
+ return entityManager.createNamedQuery(namedQuery);
}
@Override
@@ -72,8 +72,8 @@
*/
public static QueryBuilder nativeQuery(final String nativeQuery) {
return new QueryBuilder() {
- protected Query makeQueryObject(JpaConsumer consumer) {
- return consumer.getEntityManager().createNativeQuery(nativeQuery);
+ protected Query makeQueryObject(EntityManager entityManager) {
+ return entityManager.createNativeQuery(nativeQuery);
}
@Override
@@ -102,7 +102,7 @@
public QueryBuilder parameters(final Collection parameters) {
checkNoParametersConfigured();
parameterBuilder = new ParameterBuilder() {
- public void populateQuery(JpaConsumer consumer, Query query) {
+ public void populateQuery(EntityManager entityManager, Query query) {
int counter = 0;
for (Object parameter : parameters) {
query.setParameter(counter++, parameter);
@@ -126,7 +126,7 @@
public QueryBuilder parameters(final Map<String, Object> parameterMap) {
checkNoParametersConfigured();
parameterBuilder = new ParameterBuilder() {
- public void populateQuery(JpaConsumer consumer, Query query) {
+ public void populateQuery(EntityManager entityManager, Query query) {
Set<Map.Entry<String, Object>> entries = parameterMap.entrySet();
for (Map.Entry<String, Object> entry : entries) {
query.setParameter(entry.getKey(), entry.getValue());
@@ -147,9 +147,9 @@
}
}
- public Query createQuery(JpaConsumer consumer) {
- Query query = makeQueryObject(consumer);
- populateQuery(consumer, query);
+ public Query createQuery(EntityManager entityManager) {
+ Query query = makeQueryObject(entityManager);
+ populateQuery(entityManager, query);
return query;
}
@@ -162,18 +162,18 @@
}
}
- protected void populateQuery(JpaConsumer consumer, Query query) {
+ protected void populateQuery(EntityManager entityManager, Query query) {
if (parameterBuilder != null) {
- parameterBuilder.populateQuery(consumer, query);
+ parameterBuilder.populateQuery(entityManager, query);
}
}
- protected abstract Query makeQueryObject(JpaConsumer consumer);
+ protected abstract Query makeQueryObject(EntityManager entityManager);
/**
* A plugin strategy to populate the query with parameters
*/
protected abstract static class ParameterBuilder {
- public abstract void populateQuery(JpaConsumer consumer, Query query);
+ public abstract void populateQuery(EntityManager entityManager, Query query);
}
}
Modified: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/QueryFactory.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/QueryFactory.java?view=diff&rev=525183&r1=525182&r2=525183
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/QueryFactory.java
(original)
+++ activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/QueryFactory.java
Tue Apr 3 08:48:45 2007
@@ -30,8 +30,8 @@
/**
* Creates a new query to find objects to be processed
*
- * @param consumer the consumer which has access to the {@link JpaEndpoint} and JPA {@link
EntityManager}
+ * @param entityManager
* @return the query configured with any parameters etc
*/
- Query createQuery(JpaConsumer consumer);
+ Query createQuery(EntityManager entityManager);
}
Added: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/TransactionStrategy.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/TransactionStrategy.java?view=auto&rev=525183
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/TransactionStrategy.java
(added)
+++ activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/TransactionStrategy.java
Tue Apr 3 08:48:45 2007
@@ -0,0 +1,31 @@
+/**
+ *
+ * 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 org.springframework.orm.jpa.JpaCallback;
+import org.apache.camel.Service;
+
+import javax.persistence.EntityManager;
+
+/**
+ * @version $Revision$
+ */
+public interface TransactionStrategy extends Service {
+
+ public Object execute(JpaCallback callback);
+}
Propchange: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/TransactionStrategy.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/TransactionStrategy.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: activemq/camel/trunk/camel-jpa/src/main/java/org/apache/camel/component/jpa/TransactionStrategy.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java?view=diff&rev=525183&r1=525182&r2=525183
==============================================================================
--- activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java
(original)
+++ activemq/camel/trunk/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java
Tue Apr 3 08:48:45 2007
@@ -30,11 +30,12 @@
import static org.apache.camel.util.ServiceHelper.stopServices;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.springframework.orm.jpa.JpaCallback;
+import org.springframework.orm.jpa.JpaTemplate;
import javax.persistence.EntityManager;
-import javax.persistence.EntityTransaction;
+import javax.persistence.PersistenceException;
import java.util.List;
-import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -46,25 +47,25 @@
protected CamelContext camelContext = new DefaultCamelContext();
protected CamelClient client = new CamelClient(camelContext);
protected JpaEndpoint endpoint;
- protected EntityManager entityManager;
+ protected TransactionStrategy transactionStrategy;
+ protected JpaTemplate template;
protected Consumer<Exchange> consumer;
protected Exchange receivedExchange;
protected CountDownLatch latch = new CountDownLatch(1);
protected String entityName = SendEmail.class.getName();
protected String queryText = "select o from " + entityName + " o";
- protected EntityTransaction transaction;
public void testProducerInsertsIntoDatabaseThenConsumerFiresMessageExchange() throws
Exception {
- // lets assert that there are no existing send mail tasks
- transaction = entityManager.getTransaction();
- transaction.begin();
-
- // lets delete any exiting records before the test
- entityManager.createQuery("delete from " + entityName).executeUpdate();
+ transactionStrategy.execute(new JpaCallback() {
+ public Object doInJpa(EntityManager entityManager) throws PersistenceException
{
+ // lets delete any exiting records before the test
+ entityManager.createQuery("delete from " + entityName).executeUpdate();
+ return null;
+ }
+ });
- List results = entityManager.createQuery(queryText).getResultList();
+ List results = template.find(queryText);
assertEquals("Should have no results: " + results, 0, results.size());
- transaction.commit();
// lets produce some objects
client.send(endpoint, new Processor<Exchange>() {
@@ -74,13 +75,10 @@
});
// now lets assert that there is a result
- transaction.begin();
- results = entityManager.createQuery(queryText).getResultList();
+ results = template.find(queryText);
assertEquals("Should have no results: " + results, 1, results.size());
SendEmail mail = (SendEmail) results.get(0);
assertEquals("address property", "foo@bar.com", mail.getAddress());
- transaction.commit();
- transaction = null;
// now lets create a consumer to consume it
consumer = endpoint.createConsumer(new Processor<Exchange>() {
@@ -112,10 +110,13 @@
assertTrue("Should be a JPA endpoint but was: " + value, value instanceof JpaEndpoint);
endpoint = (JpaEndpoint) value;
- entityManager = endpoint.createEntityManager();
+ transactionStrategy = endpoint.createTransactionStrategy();
+ template = endpoint.getTemplate();
}
protected JpaComponent createJpaComponent() {
+ // TODO zap this!
+
JpaComponent answer = new JpaComponent();
/*
Properties properties = new Properties();
@@ -130,13 +131,7 @@
@Override
protected void tearDown() throws Exception {
- if (transaction != null) {
- transaction.rollback();
- transaction = null;
- }
- if (entityManager != null) {
- entityManager.close();
- }
+
stopServices(consumer, client, camelContext);
super.tearDown();
|