org.apache.camel.component.jpa.JpaPollingConsumer Maven / Gradle / Ivy
/*
* 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.LockModeType;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import org.apache.camel.Exchange;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.support.PollingConsumerSupport;
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 transient ExecutorService executorService;
private final EntityManagerFactory entityManagerFactory;
private final TransactionTemplate transactionTemplate;
private String query;
private String namedQuery;
private String nativeQuery;
private LockModeType lockModeType = LockModeType.PESSIMISTIC_WRITE;
private Class> resultClass;
private QueryFactory queryFactory;
private Map 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 LockModeType getLockModeType() {
return lockModeType;
}
public void setLockModeType(LockModeType lockModeType) {
this.lockModeType = lockModeType;
}
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 getParameters() {
return parameters;
}
public void setParameters(Map 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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy