Java源码示例:org.activiti.engine.impl.persistence.entity.JobEntity
示例1
protected org.activiti5.engine.impl.persistence.entity.JobEntity convertToActiviti5JobEntity(final JobEntity job, final ProcessEngineConfigurationImpl processEngineConfiguration) {
org.activiti5.engine.impl.persistence.entity.JobEntity activity5Job = new org.activiti5.engine.impl.persistence.entity.JobEntity();
activity5Job.setJobType(job.getJobType());
activity5Job.setDuedate(job.getDuedate());
activity5Job.setExclusive(job.isExclusive());
activity5Job.setExecutionId(job.getExecutionId());
activity5Job.setId(job.getId());
activity5Job.setJobHandlerConfiguration(job.getJobHandlerConfiguration());
activity5Job.setJobHandlerType(job.getJobHandlerType());
activity5Job.setEndDate(job.getEndDate());
activity5Job.setRepeat(job.getRepeat());
activity5Job.setProcessDefinitionId(job.getProcessDefinitionId());
activity5Job.setProcessInstanceId(job.getProcessInstanceId());
activity5Job.setRetries(job.getRetries());
activity5Job.setRevision(job.getRevision());
activity5Job.setTenantId(job.getTenantId());
activity5Job.setExceptionMessage(job.getExceptionMessage());
return activity5Job;
}
示例2
public void run() {
if (job == null) {
job = processEngineConfiguration.getCommandExecutor().execute(new Command<JobEntity>() {
@Override
public JobEntity execute(CommandContext commandContext) {
return commandContext.getJobEntityManager().findById(jobId);
}
});
}
if (isHandledByActiviti5Engine()) {
return;
}
boolean lockNotNeededOrSuccess = lockJobIfNeeded();
if (lockNotNeededOrSuccess) {
executeJob();
unlockJobIfNeeded();
}
}
示例3
@Deployment(resources = "org/activiti/engine/test/api/event/JobEventsTest.testJobEntityEvents.bpmn20.xml")
public void testActivityTimeOutEvent() {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("testJobEvents");
Job theJob = managementService.createTimerJobQuery().processInstanceId(processInstance.getId()).singleResult();
assertNotNull(theJob);
// Force timer to fire
Calendar tomorrow = Calendar.getInstance();
tomorrow.add(Calendar.DAY_OF_YEAR, 1);
processEngineConfiguration.getClock().setCurrentTime(tomorrow.getTime());
waitForJobExecutorToProcessAllJobsAndExecutableTimerJobs(2000, 100);
// Check timeout has been dispatched
assertEquals(1, listener.getEventsReceived().size());
FlowableEvent activitiEvent = listener.getEventsReceived().get(0);
assertEquals("ACTIVITY_CANCELLED event expected", FlowableEngineEventType.ACTIVITY_CANCELLED, activitiEvent.getType());
FlowableActivityCancelledEvent cancelledEvent = (FlowableActivityCancelledEvent) activitiEvent;
assertTrue("TIMER is the cause of the cancellation", cancelledEvent.getCause() instanceof JobEntity);
}
示例4
@Deployment
public void testActivityTimeOutEventInCallActivity() {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("timerOnCallActivity");
Job theJob = managementService.createTimerJobQuery().processInstanceId(processInstance.getId()).singleResult();
assertNotNull(theJob);
// Force timer to fire
Calendar timeToFire = Calendar.getInstance();
timeToFire.add(Calendar.HOUR, 2);
timeToFire.add(Calendar.SECOND, 5);
processEngineConfiguration.getClock().setCurrentTime(timeToFire.getTime());
waitForJobExecutorToProcessAllJobsAndExecutableTimerJobs(10000, 500);
// Check timeout-events have been dispatched
assertEquals(4, listener.getEventsReceived().size());
List<String> eventIdList = new ArrayList<String>();
for (FlowableEvent event : listener.getEventsReceived()) {
assertEquals(FlowableEngineEventType.ACTIVITY_CANCELLED, event.getType());
assertTrue("TIMER is the cause of the cancellation", ((FlowableActivityCancelledEvent) event).getCause() instanceof JobEntity);
eventIdList.add(((ActivitiActivityEventImpl) event).getActivityId());
}
assertTrue(eventIdList.indexOf("innerTask1") >= 0);
assertTrue(eventIdList.indexOf("innerTask2") >= 0);
assertTrue(eventIdList.indexOf("innerFork") >= 0);
assertTrue(eventIdList.indexOf("callActivity") >= 0);
}
示例5
@Override
public JobEntity moveDeadLetterJobToExecutableJob(DeadLetterJobEntity deadLetterJobEntity, int retries) {
if (deadLetterJobEntity == null) {
throw new ActivitiIllegalArgumentException("Null job provided");
}
JobEntity executableJob = createExecutableJobFromOtherJob(deadLetterJobEntity);
executableJob.setRetries(retries);
boolean insertSuccesful = processEngineConfiguration.getJobEntityManager().insertJobEntity(executableJob);
if (insertSuccesful) {
processEngineConfiguration.getDeadLetterJobEntityManager().delete(deadLetterJobEntity);
triggerExecutorIfNeeded(executableJob);
return executableJob;
}
return null;
}
示例6
private void createJobWithoutExceptionStacktrace() {
CommandExecutor commandExecutor = (CommandExecutor) processEngineConfiguration.getFlowable5CompatibilityHandler().getRawCommandExecutor();
commandExecutor.execute(new Command<Void>() {
public Void execute(CommandContext commandContext) {
JobEntityManager jobManager = commandContext.getJobEntityManager();
jobEntity = new JobEntity();
jobEntity.setJobType(Job.JOB_TYPE_MESSAGE);
jobEntity.setRevision(1);
jobEntity.setLockOwner(UUID.randomUUID().toString());
jobEntity.setRetries(0);
jobEntity.setExceptionMessage("I'm supposed to fail");
jobManager.insert(jobEntity);
assertNotNull(jobEntity.getId());
return null;
}
});
}
示例7
/**
* Gets the JobEntity from the given ExecuteAsyncJobCmd.
*
* @param executeAsyncJobCmd The ExecuteAsyncJobCmd
*
* @return The JobEntity
*/
private JobEntity getJobEntity(ExecuteAsyncJobCmd executeAsyncJobCmd)
{
/*
* Unfortunately, ExecuteAsyncJobCmd does not provide an accessible method to get the JobEntity stored within it.
* We use reflection to force the value out of the object.
* Also, we cannot simply get the entity and update it. We must retrieve it through the entity manager so it registers in Activiti's persistent object
* cache. This way when the transaction commits, Activiti is aware of any changes in the JobEntity and persists them correctly.
*/
try
{
Field field = ExecuteAsyncJobCmd.class.getDeclaredField("job");
ReflectionUtils.makeAccessible(field);
String jobId = ((JobEntity) ReflectionUtils.getField(field, executeAsyncJobCmd)).getId();
return Context.getCommandContext().getJobEntityManager().findJobById(jobId);
}
catch (NoSuchFieldException | SecurityException e)
{
/*
* This exception should not happen.
*/
throw new IllegalStateException(e);
}
}
示例8
protected void assertJobDetails(boolean locked) {
JobQuery jobQuery = managementService.createJobQuery();
if (locked) {
jobQuery.locked();
}
Job job = jobQuery.singleResult();
assertTrue(job instanceof JobEntity);
JobEntity jobEntity = (JobEntity) job;
if (locked) {
assertNotNull(jobEntity.getLockOwner());
assertNotNull(jobEntity.getLockExpirationTime());
} else {
assertNull(jobEntity.getLockOwner());
assertNull(jobEntity.getLockExpirationTime());
}
}
示例9
@Deployment(resources = "org/activiti/engine/test/bpmn/event/timer/BoundaryTimerEventTest.testTimerOnNestingOfSubprocesses.bpmn20.xml")
public void testActivityTimeOutEventInSubProcess() {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("timerOnNestedSubprocesses");
Job theJob = managementService.createTimerJobQuery().processInstanceId(processInstance.getId()).singleResult();
assertNotNull(theJob);
// Force timer to fire
Calendar timeToFire = Calendar.getInstance();
timeToFire.add(Calendar.HOUR, 2);
timeToFire.add(Calendar.SECOND, 5);
processEngineConfiguration.getClock().setCurrentTime(timeToFire.getTime());
waitForJobExecutorToProcessAllJobs(2000, 200);
// Check timeout-events have been dispatched
assertEquals(3, listener.getEventsReceived().size());
List<String> eventIdList = new ArrayList<String>();
for (FlowableEvent event : listener.getEventsReceived()) {
assertEquals(FlowableEngineEventType.ACTIVITY_CANCELLED, event.getType());
assertTrue("TIMER is the cause of the cancellation", ((FlowableActivityCancelledEvent) event).getCause() instanceof JobEntity);
eventIdList.add(((ActivitiActivityEventImpl) event).getActivityId());
}
assertTrue(eventIdList.indexOf("innerTask1") >= 0);
assertTrue(eventIdList.indexOf("innerTask2") >= 0);
assertTrue(eventIdList.indexOf("innerFork") >= 0);
}
示例10
protected void createTimerForDelayedExecution(CommandContext commandContext, List<ProcessDefinitionEntity> processDefinitions) {
for (ProcessDefinitionEntity processDefinition : processDefinitions) {
if (Activiti5Util.isActiviti5ProcessDefinition(commandContext, processDefinition)) continue;
TimerJobEntity timer = commandContext.getTimerJobEntityManager().create();
timer.setJobType(JobEntity.JOB_TYPE_TIMER);
timer.setProcessDefinitionId(processDefinition.getId());
// Inherit tenant identifier (if applicable)
if (processDefinition.getTenantId() != null) {
timer.setTenantId(processDefinition.getTenantId());
}
timer.setDuedate(executionDate);
timer.setJobHandlerType(getDelayedExecutionJobHandlerType());
timer.setJobHandlerConfiguration(TimerChangeProcessDefinitionSuspensionStateJobHandler.createJobHandlerConfiguration(includeProcessInstances));
commandContext.getJobManager().scheduleTimerJob(timer);
}
}
示例11
public JobEntity execute(CommandContext commandContext) {
if (jobId == null) {
throw new ActivitiIllegalArgumentException("jobId and job is null");
}
DeadLetterJobEntity job = commandContext.getDeadLetterJobEntityManager().findById(jobId);
if (job == null) {
throw new JobNotFoundException(jobId);
}
if (log.isDebugEnabled()) {
log.debug("Moving deadletter job to executable job table {}", job.getId());
}
return commandContext.getJobManager().moveDeadLetterJobToExecutableJob(job, retries);
}
示例12
@Deployment(resources = "org/activiti/engine/test/bpmn/event/timer/BoundaryTimerEventTest.testTimerOnNestingOfSubprocesses.bpmn20.xml")
public void testActivityTimeOutEventInSubProcess() {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("timerOnNestedSubprocesses");
Job theJob = managementService.createTimerJobQuery().processInstanceId(processInstance.getId()).singleResult();
assertNotNull(theJob);
// Force timer to fire
Calendar timeToFire = Calendar.getInstance();
timeToFire.add(Calendar.HOUR, 2);
timeToFire.add(Calendar.SECOND, 5);
processEngineConfiguration.getClock().setCurrentTime(timeToFire.getTime());
waitForJobExecutorToProcessAllJobs(2000, 200);
// Check timeout-events have been dispatched
assertEquals(4, listener.getEventsReceived().size());
List<String> eventIdList = new ArrayList<String>();
for (ActivitiEvent event : listener.getEventsReceived()) {
assertEquals(ActivitiEventType.ACTIVITY_CANCELLED, event.getType());
assertTrue("TIMER is the cause of the cancellation", ((ActivitiActivityCancelledEvent) event).getCause() instanceof JobEntity);
eventIdList.add(((ActivitiActivityEventImpl) event).getActivityId());
}
assertTrue(eventIdList.indexOf("innerTask1") >= 0);
assertTrue(eventIdList.indexOf("innerTask2") >= 0);
assertTrue(eventIdList.indexOf("subprocess") >= 0);
assertTrue(eventIdList.indexOf("innerSubprocess") >= 0);
}
示例13
@Test
public void testExecuteWithExceptionAndGetJobEntityWithSecurityException()
{
// Mock dependencies.
CommandConfig config = mock(CommandConfig.class);
JobEntity jobEntity = mock(JobEntity.class);
ExecuteAsyncJobCmd command = new ExecuteAsyncJobCmd(jobEntity);
doThrow(SecurityException.class).when(jobEntity).getId();
// Try to call the method under test.
try
{
herdCommandInvoker.execute(config, command);
fail();
}
catch (IllegalStateException e)
{
assertEquals(SecurityException.class.getName(), e.getMessage());
}
}
示例14
public void testJobCommandsWithMessage() {
CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutor();
String jobId = commandExecutor.execute(new Command<String>() {
public String execute(CommandContext commandContext) {
JobEntity message = createTweetMessage("i'm coding a test");
commandContext.getJobManager().scheduleAsyncJob(message);
return message.getId();
}
});
Job job = managementService.createJobQuery().singleResult();
assertNotNull(job);
assertEquals(jobId, job.getId());
assertEquals(0, tweetHandler.getMessages().size());
managementService.executeJob(job.getId());
assertEquals("i'm coding a test", tweetHandler.getMessages().get(0));
assertEquals(1, tweetHandler.getMessages().size());
}
示例15
@Override
public boolean executeAsyncJob(Job job) {
try {
taskExecutor.execute(new ExecuteAsyncRunnable((JobEntity) job, processEngineConfiguration));
return true;
} catch (RejectedExecutionException e) {
rejectedJobsHandler.jobRejected(this, job);
return false;
}
}
示例16
protected TimerJobEntity createTweetTimer(String msg, Date duedate) {
TimerJobEntity timer = new TimerJobEntityImpl();
timer.setJobType(JobEntity.JOB_TYPE_TIMER);
timer.setJobHandlerType("tweet");
timer.setJobHandlerConfiguration(msg);
timer.setDuedate(duedate);
return timer;
}
示例17
public void executeJobWithLockAndRetry(Job job) {
if (job == null) return;
final ProcessEngineConfigurationImpl processEngineConfig = (ProcessEngineConfigurationImpl) getProcessEngine().getProcessEngineConfiguration();
org.activiti5.engine.impl.persistence.entity.JobEntity activity5Job = null;
if (job instanceof org.activiti5.engine.impl.persistence.entity.JobEntity) {
activity5Job = (org.activiti5.engine.impl.persistence.entity.JobEntity) job;
} else {
activity5Job = convertToActiviti5JobEntity((JobEntity) job, processEngineConfig);
}
AsyncJobUtil.executeJob(activity5Job, processEngineConfig.getCommandExecutor());
}
示例18
protected JobEntity createTweetExceptionMessage() {
JobEntity message = processEngineConfiguration.getJobEntityManager().create();
message.setJobType(JobEntity.JOB_TYPE_MESSAGE);
message.setJobHandlerType("tweet-exception");
message.setRetries(3);
return message;
}
示例19
protected JobEntity createTweetExceptionMessage() {
JobEntity message = new JobEntityImpl();
message.setJobType(JobEntity.JOB_TYPE_MESSAGE);
message.setRetries(3);
message.setJobHandlerType("tweet-exception");
return message;
}
示例20
@Override
public JobEntity createAsyncJob(ExecutionEntity execution, boolean exclusive) {
JobEntity jobEntity = null;
// When the async executor is activated, the job is directly passed on to the async executor thread
if (isAsyncExecutorActive()) {
jobEntity = internalCreateLockedAsyncJob(execution, exclusive);
} else {
jobEntity = internalCreateAsyncJob(execution, exclusive);
}
return jobEntity;
}
示例21
@Override
public JobEntity moveTimerJobToExecutableJob(TimerJobEntity timerJob) {
if (timerJob == null) {
throw new ActivitiException("Empty timer job can not be scheduled");
}
JobEntity executableJob = createExecutableJobFromOtherJob(timerJob);
boolean insertSuccesful = processEngineConfiguration.getJobEntityManager().insertJobEntity(executableJob);
if (insertSuccesful) {
processEngineConfiguration.getTimerJobEntityManager().delete(timerJob);
triggerExecutorIfNeeded(executableJob);
return executableJob;
}
return null;
}
示例22
protected JobEntity createTweetMessage(String msg) {
JobEntity message = new JobEntityImpl();
message.setJobType(JobEntity.JOB_TYPE_MESSAGE);
message.setJobHandlerType("tweet");
message.setJobHandlerConfiguration(msg);
return message;
}
示例23
@Override
public DeadLetterJobEntity moveJobToDeadLetterJob(AbstractJobEntity job) {
DeadLetterJobEntity deadLetterJob = createDeadLetterJobFromOtherJob(job);
processEngineConfiguration.getDeadLetterJobEntityManager().insert(deadLetterJob);
if (job instanceof TimerJobEntity) {
processEngineConfiguration.getTimerJobEntityManager().delete((TimerJobEntity) job);
} else if (job instanceof JobEntity) {
processEngineConfiguration.getJobEntityManager().delete((JobEntity) job);
}
return deadLetterJob;
}
示例24
@Override
public void unacquire(Job job) {
// Deleting the old job and inserting it again with another id,
// will avoid that the job is immediately is picked up again (for example
// when doing lots of exclusive jobs for the same process instance)
if (job instanceof JobEntity) {
JobEntity jobEntity = (JobEntity) job;
processEngineConfiguration.getJobEntityManager().delete(jobEntity.getId());
JobEntity newJobEntity = processEngineConfiguration.getJobEntityManager().create();
copyJobInfo(newJobEntity, jobEntity);
newJobEntity.setId(null); // We want a new id to be assigned to this job
newJobEntity.setLockExpirationTime(null);
newJobEntity.setLockOwner(null);
processEngineConfiguration.getJobEntityManager().insert(newJobEntity);
// We're not calling triggerExecutorIfNeeded here after the inser. The unacquire happened
// for a reason (eg queue full or exclusive lock failure). No need to try it immediately again,
// as the chance of failure will be high.
} else {
// It could be a v5 job, so simply unlock it.
processEngineConfiguration.getJobEntityManager().resetExpiredJob(job.getId());
}
}
示例25
protected void executeTimerJob(JobEntity timerEntity) {
TimerJobEntityManager timerJobEntityManager = processEngineConfiguration.getTimerJobEntityManager();
VariableScope variableScope = null;
if (timerEntity.getExecutionId() != null) {
variableScope = getExecutionEntityManager().findById(timerEntity.getExecutionId());
}
if (variableScope == null) {
variableScope = NoExecutionVariableScope.getSharedInstance();
}
// set endDate if it was set to the definition
restoreExtraData(timerEntity, variableScope);
if (timerEntity.getDuedate() != null && !isValidTime(timerEntity, timerEntity.getDuedate(), variableScope)) {
if (logger.isDebugEnabled()) {
logger.debug("Timer {} fired. but the dueDate is after the endDate. Deleting timer.", timerEntity.getId());
}
processEngineConfiguration.getJobEntityManager().delete(timerEntity);
return;
}
executeJobHandler(timerEntity);
processEngineConfiguration.getJobEntityManager().delete(timerEntity);
if (logger.isDebugEnabled()) {
logger.debug("Timer {} fired. Deleting timer.", timerEntity.getId());
}
if (timerEntity.getRepeat() != null) {
TimerJobEntity newTimerJobEntity = timerJobEntityManager.createAndCalculateNextTimer(timerEntity, variableScope);
if (newTimerJobEntity != null) {
scheduleTimerJob(newTimerJobEntity);
}
}
}
示例26
protected void executeJobHandler(JobEntity jobEntity) {
ExecutionEntity execution = null;
if (jobEntity.getExecutionId() != null) {
execution = getExecutionEntityManager().findById(jobEntity.getExecutionId());
}
Map<String, JobHandler> jobHandlers = processEngineConfiguration.getJobHandlers();
JobHandler jobHandler = jobHandlers.get(jobEntity.getJobHandlerType());
jobHandler.execute(jobEntity, jobEntity.getJobHandlerConfiguration(), execution, getCommandContext());
}
示例27
@Override
public Object execute(CommandContext commandContext) {
JobEntity jobToDelete = getJobToDelete(commandContext);
jobToDelete.delete();
return null;
}
示例28
@Override
public Void execute(CommandContext commandContext) {
JobEntity jobToDelete = null;
for (String jobId : jobIds) {
jobToDelete = commandContext
.getJobEntityManager()
.findJobById(jobId);
if (jobToDelete != null) {
if (commandContext.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
ActivitiEventBuilder.createEntityEvent(FlowableEngineEventType.JOB_CANCELED, jobToDelete));
}
jobToDelete.delete();
} else {
TimerJobEntity timerJobToDelete = commandContext.getTimerJobEntityManager().findJobById(jobId);
if (timerJobToDelete != null) {
if (commandContext.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
ActivitiEventBuilder.createEntityEvent(FlowableEngineEventType.JOB_CANCELED, timerJobToDelete));
}
timerJobToDelete.delete();
}
}
}
return null;
}
示例29
protected JobEntity internalCreateLockedAsyncJob(ExecutionEntity execution, boolean exclusive) {
JobEntity asyncJob = processEngineConfiguration.getJobEntityManager().create();
fillDefaultAsyncJobInfo(asyncJob, execution, exclusive);
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTime(processEngineConfiguration.getClock().getCurrentTime());
gregorianCalendar.add(Calendar.MILLISECOND, getAsyncExecutor().getAsyncJobLockTimeInMillis());
asyncJob.setLockExpirationTime(gregorianCalendar.getTime());
asyncJob.setLockOwner(getAsyncExecutor().getLockOwner());
return asyncJob;
}
示例30
protected void fillDefaultAsyncJobInfo(JobEntity jobEntity, ExecutionEntity execution, boolean exclusive) {
jobEntity.setJobType(JobEntity.JOB_TYPE_MESSAGE);
jobEntity.setRevision(1);
jobEntity.setRetries(processEngineConfiguration.getAsyncExecutorNumberOfRetries());
jobEntity.setExecutionId(execution.getId());
jobEntity.setProcessInstanceId(execution.getProcessInstanceId());
jobEntity.setProcessDefinitionId(execution.getProcessDefinitionId());
jobEntity.setExclusive(exclusive);
jobEntity.setJobHandlerType(AsyncContinuationJobHandler.TYPE);
// Inherit tenant id (if applicable)
if (execution.getTenantId() != null) {
jobEntity.setTenantId(execution.getTenantId());
}
}