org.activiti.engine.impl.persistence.entity.JobEntityManager Maven / Gradle / Ivy
The newest version!
/* Licensed 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.activiti.engine.impl.persistence.entity;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.engine.delegate.event.impl.ActivitiEventBuilder;
import org.activiti.engine.impl.JobQueryImpl;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.asyncexecutor.AsyncExecutor;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.cfg.TransactionListener;
import org.activiti.engine.impl.cfg.TransactionState;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.jobexecutor.AsyncJobAddedNotification;
import org.activiti.engine.impl.jobexecutor.JobAddedNotification;
import org.activiti.engine.impl.jobexecutor.JobExecutor;
import org.activiti.engine.impl.persistence.AbstractManager;
import org.activiti.engine.runtime.Job;
/**
* @author Tom Baeyens
* @author Daniel Meyer
* @author Joram Barrez
*/
public class JobEntityManager extends AbstractManager {
public void send(MessageEntity message) {
ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();
if (processEngineConfiguration.isAsyncExecutorEnabled()) {
// If the async executor is enabled, we need to set the duedate of the job to the current date + the default lock time.
// This is cope with the case where the async job executor or the process engine goes down
// before executing the job. This way, other async job executors can pick the job up after the max lock time.
Date dueDate = new Date(processEngineConfiguration.getClock().getCurrentTime().getTime()
+ processEngineConfiguration.getAsyncExecutor().getAsyncJobLockTimeInMillis());
message.setDuedate(dueDate);
message.setLockExpirationTime(null); // was set before, but to be quickly picked up needs to be set to null
} else if (!processEngineConfiguration.isJobExecutorActivate()) {
// If the async executor is disabled AND there is no old school job executor,
// The job needs to be picked up as soon as possible. So the due date is now set to the current time
message.setDuedate(processEngineConfiguration.getClock().getCurrentTime());
message.setLockExpirationTime(null); // was set before, but to be quickly picked up needs to be set to null
}
message.insert();
if (processEngineConfiguration.isAsyncExecutorEnabled()) {
hintAsyncExecutor(message);
} else {
hintJobExecutor(message);
}
}
public void schedule(TimerEntity timer) {
Date duedate = timer.getDuedate();
if (duedate==null) {
throw new ActivitiIllegalArgumentException("duedate is null");
}
timer.insert();
ProcessEngineConfiguration engineConfiguration = Context.getProcessEngineConfiguration();
if (engineConfiguration.isAsyncExecutorEnabled() == false &&
timer.getDuedate().getTime() <= (engineConfiguration.getClock().getCurrentTime().getTime())) {
hintJobExecutor(timer);
}
}
public void retryAsyncJob(JobEntity job) {
AsyncExecutor asyncExecutor = Context.getProcessEngineConfiguration().getAsyncExecutor();
asyncExecutor.executeAsyncJob(job);
}
protected void hintAsyncExecutor(JobEntity job) {
AsyncExecutor asyncExecutor = Context.getProcessEngineConfiguration().getAsyncExecutor();
// notify job executor:
TransactionListener transactionListener = new AsyncJobAddedNotification(job, asyncExecutor);
Context.getCommandContext()
.getTransactionContext()
.addTransactionListener(TransactionState.COMMITTED, transactionListener);
}
protected void hintJobExecutor(JobEntity job) {
JobExecutor jobExecutor = Context.getProcessEngineConfiguration().getJobExecutor();
// notify job executor:
TransactionListener transactionListener = new JobAddedNotification(jobExecutor);
Context.getCommandContext()
.getTransactionContext()
.addTransactionListener(TransactionState.COMMITTED, transactionListener);
}
public void cancelTimers(ExecutionEntity execution) {
List timers = Context
.getCommandContext()
.getJobEntityManager()
.findTimersByExecutionId(execution.getId());
for (TimerEntity timer: timers) {
if (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
ActivitiEventBuilder.createEntityEvent(ActivitiEventType.JOB_CANCELED, timer));
}
timer.delete();
}
}
public JobEntity findJobById(String jobId) {
return (JobEntity) getDbSqlSession().selectOne("selectJob", jobId);
}
@SuppressWarnings("unchecked")
public List findNextJobsToExecute(Page page) {
ProcessEngineConfiguration processEngineConfig = Context.getProcessEngineConfiguration();
Date now = processEngineConfig.getClock().getCurrentTime();
return getDbSqlSession().selectList("selectNextJobsToExecute", now, page);
}
@SuppressWarnings("unchecked")
public List findNextTimerJobsToExecute(Page page) {
ProcessEngineConfiguration processEngineConfig = Context.getProcessEngineConfiguration();
Date now = processEngineConfig.getClock().getCurrentTime();
return getDbSqlSession().selectList("selectNextTimerJobsToExecute", now, page);
}
@SuppressWarnings("unchecked")
public List findAsyncJobsDueToExecute(Page page) {
ProcessEngineConfiguration processEngineConfig = Context.getProcessEngineConfiguration();
Date now = processEngineConfig.getClock().getCurrentTime();
return getDbSqlSession().selectList("selectAsyncJobsDueToExecute", now, page);
}
@SuppressWarnings("unchecked")
public List findJobsByLockOwner(String lockOwner, int start, int maxNrOfJobs) {
return getDbSqlSession().selectList("selectJobsByLockOwner", lockOwner, start, maxNrOfJobs);
}
@SuppressWarnings("unchecked")
public List findJobsByExecutionId(String executionId) {
return getDbSqlSession().selectList("selectJobsByExecutionId", executionId);
}
@SuppressWarnings("unchecked")
public List findExclusiveJobsToExecute(String processInstanceId) {
Map params = new HashMap();
params.put("pid", processInstanceId);
params.put("now", Context.getProcessEngineConfiguration().getClock().getCurrentTime());
return getDbSqlSession().selectList("selectExclusiveJobsToExecute", params);
}
@SuppressWarnings("unchecked")
public List findUnlockedTimersByDuedate(Date duedate, Page page) {
final String query = "selectUnlockedTimersByDuedate";
return getDbSqlSession().selectList(query, duedate, page);
}
@SuppressWarnings("unchecked")
public List findTimersByExecutionId(String executionId) {
return getDbSqlSession().selectList("selectTimersByExecutionId", executionId);
}
@SuppressWarnings("unchecked")
public List findJobsByQueryCriteria(JobQueryImpl jobQuery, Page page) {
final String query = "selectJobByQueryCriteria";
return getDbSqlSession().selectList(query, jobQuery, page);
}
@SuppressWarnings("unchecked")
public List findJobsByConfiguration(String jobHandlerType, String jobHandlerConfiguration) {
Map params = new HashMap();
params.put("handlerType", jobHandlerType);
params.put("handlerConfiguration", String.format("%%s%%",jobHandlerConfiguration));
return getDbSqlSession().selectList("selectJobsByConfiguration", params);
}
public long findJobCountByQueryCriteria(JobQueryImpl jobQuery) {
return (Long) getDbSqlSession().selectOne("selectJobCountByQueryCriteria", jobQuery);
}
public void updateJobTenantIdForDeployment(String deploymentId, String newTenantId) {
HashMap params = new HashMap();
params.put("deploymentId", deploymentId);
params.put("tenantId", newTenantId);
getDbSqlSession().update("updateJobTenantIdForDeployment", params);
}
public int updateJobLockForAllJobs(String lockOwner, Date expirationTime) {
HashMap params = new HashMap();
params.put("lockOwner", lockOwner);
params.put("lockExpirationTime", expirationTime);
params.put("dueDate", Context.getProcessEngineConfiguration().getClock().getCurrentTime());
return getDbSqlSession().update("updateJobLockForAllJobs", params);
}
}