Java Code Examples for org.flowable.job.service.impl.asyncexecutor.AsyncExecutor#start()

The following examples show how to use org.flowable.job.service.impl.asyncexecutor.AsyncExecutor#start() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JobTestHelper.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
public static void executeJobExecutorForTime(ProcessEngineConfiguration processEngineConfiguration, long maxMillisToWait, long intervalMillis) {
    AsyncExecutor asyncExecutor = processEngineConfiguration.getAsyncExecutor();
    asyncExecutor.start();

    try {
        Timer timer = new Timer();
        InterruptTask task = new InterruptTask(Thread.currentThread());
        timer.schedule(task, maxMillisToWait);
        try {
            while (!task.isTimeLimitExceeded()) {
                Thread.sleep(intervalMillis);
            }
        } catch (InterruptedException e) {
            // ignore
        } finally {
            timer.cancel();
        }

    } finally {
        asyncExecutor.shutdown();
    }
}
 
Example 2
Source File: BaseSpringRestTestCase.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
public void waitForJobExecutorToProcessAllJobs(long maxMillisToWait, long intervalMillis) {
    AsyncExecutor asyncExecutor = processEngineConfiguration.getAsyncExecutor();
    asyncExecutor.start();

    try {
        Timer timer = new Timer();
        InterruptTask task = new InterruptTask(Thread.currentThread());
        timer.schedule(task, maxMillisToWait);
        boolean areJobsAvailable = true;
        try {
            while (areJobsAvailable && !task.isTimeLimitExceeded()) {
                Thread.sleep(intervalMillis);
                areJobsAvailable = areJobsAvailable();
            }
        } catch (InterruptedException e) {
        } finally {
            timer.cancel();
        }
        if (areJobsAvailable) {
            throw new FlowableException("time limit of " + maxMillisToWait + " was exceeded");
        }

    } finally {
        asyncExecutor.shutdown();
    }
}
 
Example 3
Source File: CmmnJobTestHelper.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
public static void executeJobExecutorForTime(CmmnEngineConfiguration cmmnEngineConfiguration, long maxMillisToWait, long intervalMillis) {
    AsyncExecutor asyncExecutor = cmmnEngineConfiguration.getAsyncExecutor();
    asyncExecutor.start();

    try {
        Timer timer = new Timer();
        InterruptTask task = new InterruptTask(Thread.currentThread());
        timer.schedule(task, maxMillisToWait);
        try {
            while (!task.isTimeLimitExceeded()) {
                Thread.sleep(intervalMillis);
            }
        } catch (InterruptedException e) {
            // ignore
        } finally {
            timer.cancel();
        }

    } finally {
        asyncExecutor.shutdown();
    }
}
 
Example 4
Source File: JobTestHelper.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
public static void executeJobExecutorForTime(ProcessEngineConfiguration processEngineConfiguration, long maxMillisToWait, long intervalMillis) {
    AsyncExecutor asyncExecutor = processEngineConfiguration.getAsyncExecutor();
    asyncExecutor.start();

    try {
        Timer timer = new Timer();
        InterruptTask task = new InterruptTask(Thread.currentThread());
        timer.schedule(task, maxMillisToWait);
        try {
            while (!task.isTimeLimitExceeded()) {
                Thread.sleep(intervalMillis);
            }
        } catch (InterruptedException e) {
            // ignore
        } finally {
            timer.cancel();
        }

    } finally {
        asyncExecutor.shutdown();
    }
}
 
Example 5
Source File: CdiFlowableTestCase.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
public void waitForJobExecutorToProcessAllJobs(long maxMillisToWait, long intervalMillis) {
    AsyncExecutor asyncExecutor = processEngineConfiguration.getAsyncExecutor();
    asyncExecutor.start();

    try {
        Timer timer = new Timer();
        InterruptTask task = new InterruptTask(Thread.currentThread());
        timer.schedule(task, maxMillisToWait);
        boolean areJobsAvailable = true;
        try {
            while (areJobsAvailable && !task.isTimeLimitExceeded()) {
                Thread.sleep(intervalMillis);
                areJobsAvailable = areJobsAvailable();
            }
        } catch (InterruptedException e) {
        } finally {
            timer.cancel();
        }
        if (areJobsAvailable) {
            throw new FlowableException("time limit of " + maxMillisToWait + " was exceeded");
        }

    } finally {
        asyncExecutor.shutdown();
    }
}
 
Example 6
Source File: JobTestHelper.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
public static void waitForJobExecutorToProcessAllJobs(ProcessEngineConfiguration processEngineConfiguration,
        ManagementService managementService, long maxMillisToWait, long intervalMillis, boolean shutdownExecutorWhenFinished) {

    AsyncExecutor asyncExecutor = processEngineConfiguration.getAsyncExecutor();
    asyncExecutor.start();

    try {
        Timer timer = new Timer();
        InterruptTask task = new InterruptTask(Thread.currentThread());
        timer.schedule(task, maxMillisToWait);
        boolean areJobsAvailable = true;
        try {
            while (areJobsAvailable && !task.isTimeLimitExceeded()) {
                Thread.sleep(intervalMillis);
                try {
                    areJobsAvailable = areJobsAvailable(managementService);
                } catch (Throwable t) {
                    // Ignore, possible that exception occurs due to locking/updating of table on MSSQL when
                    // isolation level doesn't allow READ of the table
                }
            }
        } catch (InterruptedException e) {
            // ignore
        } finally {
            timer.cancel();
        }
        if (areJobsAvailable) {
            throw new FlowableException("time limit of " + maxMillisToWait + " was exceeded");
        }

    } finally {
        if (shutdownExecutorWhenFinished) {
            asyncExecutor.shutdown();
        }
    }
}
 
Example 7
Source File: JobTestHelper.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
public static void waitForJobExecutorToProcessAllJobsAndExecutableTimerJobs(ProcessEngineConfiguration processEngineConfiguration, ManagementService managementService, long maxMillisToWait, long intervalMillis,
        boolean shutdownExecutorWhenFinished) {

    AsyncExecutor asyncExecutor = processEngineConfiguration.getAsyncExecutor();
    asyncExecutor.start();
    processEngineConfiguration.setAsyncExecutorActivate(true);

    try {
        Timer timer = new Timer();
        InterruptTask task = new InterruptTask(Thread.currentThread());
        timer.schedule(task, maxMillisToWait);
        boolean areJobsAvailable = true;
        try {
            while (areJobsAvailable && !task.isTimeLimitExceeded()) {
                Thread.sleep(intervalMillis);
                try {
                    areJobsAvailable = areJobsOrExecutableTimersAvailable(managementService);
                } catch (Throwable t) {
                    // Ignore, possible that exception occurs due to locking/updating of table on MSSQL when
                    // isolation level doesn't allow READ of the table
                }
            }
        } catch (InterruptedException e) {
            // ignore
        } finally {
            timer.cancel();
        }
        if (areJobsAvailable) {
            throw new FlowableException("time limit of " + maxMillisToWait + " was exceeded");
        }

    } finally {
        if (shutdownExecutorWhenFinished) {
            processEngineConfiguration.setAsyncExecutorActivate(false);
            asyncExecutor.shutdown();
        }
    }
}
 
Example 8
Source File: CmmnJobTestHelper.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
public static void waitForExecutorToProcessAllJobs(final AsyncExecutor asyncExecutor, Callable<Boolean> callable,
        final long maxMillisToWait, final long intervalMillis, final boolean shutdownExecutorWhenFinished) {

    if (asyncExecutor == null) {
        throw new FlowableException("No async executor set. Check the cmmn engine configuration.");
    }
    
    asyncExecutor.start();

    try {
        Timer timer = new Timer();
        InterruptTask interruptTask = new InterruptTask(Thread.currentThread());
        timer.schedule(interruptTask, maxMillisToWait);
        
        boolean areJobsAvailable = true;
        try {
            while (areJobsAvailable && !interruptTask.isTimeLimitExceeded()) {
                Thread.sleep(intervalMillis);
                try {
                    areJobsAvailable = callable.call();
                } catch (Throwable t) { 
                    // ignore
                }
            }
        } catch (InterruptedException e) {
            // ignore
        } finally {
            timer.cancel();
        }
        if (areJobsAvailable) {
            throw new FlowableException("Time limit of " + maxMillisToWait + " was exceeded");
        }

    } finally {
        if (shutdownExecutorWhenFinished) {
            asyncExecutor.shutdown();
        }
    }
}
 
Example 9
Source File: JobTestHelper.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected static void internalWaitForJobs(ProcessEngineConfiguration processEngineConfiguration, ManagementService managementService,
    Predicate<ManagementService> jobsAvailablePredicate, long maxMillisToWait, long intervalMillis, boolean shutdownExecutorWhenFinished) {
    AsyncExecutor asyncExecutor = processEngineConfiguration.getAsyncExecutor();
    asyncExecutor.start();
    processEngineConfiguration.setAsyncExecutorActivate(true);

    try {
        Timer timer = new Timer();
        InterruptTask task = new InterruptTask(Thread.currentThread());
        timer.schedule(task, maxMillisToWait);
        boolean areJobsAvailable = true;
        try {
            while (areJobsAvailable && !task.isTimeLimitExceeded()) {
                Thread.sleep(intervalMillis);
                try {
                    areJobsAvailable = jobsAvailablePredicate.test(managementService);
                } catch (Throwable t) {
                    // Ignore, possible that exception occurs due to locking/updating of table on MSSQL when
                    // isolation level doesn't allow READ of the table
                }
            }
        } catch (InterruptedException e) {
            // ignore
        } finally {
            timer.cancel();
        }
        if (areJobsAvailable) {
            throw new FlowableException("time limit of " + maxMillisToWait + " was exceeded");
        }

    } finally {
        if (shutdownExecutorWhenFinished) {
            processEngineConfiguration.setAsyncExecutorActivate(false);
            asyncExecutor.shutdown();
        }
    }
}
 
Example 10
Source File: HistoryTestHelper.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
public static void waitForJobExecutorToProcessAllHistoryJobs(ProcessEngineConfiguration processEngineConfiguration, ManagementService managementService, 
        long maxMillisToWait, long intervalMillis, boolean shutdownExecutorWhenFinished) {

    ProcessEngineConfigurationImpl processEngineConfigurationImpl = (ProcessEngineConfigurationImpl) processEngineConfiguration;
    if (processEngineConfigurationImpl.isAsyncHistoryEnabled()) {
        AsyncExecutor asyncHistoryExecutor = processEngineConfiguration.getAsyncHistoryExecutor();
        
        if (!asyncHistoryExecutor.isActive()) {
            asyncHistoryExecutor.start();
        }

        try {
            Timer timer = new Timer();
            InterruptTask task = new InterruptTask(Thread.currentThread());
            timer.schedule(task, maxMillisToWait);
            boolean areJobsAvailable = true;
            try {
                while (areJobsAvailable && !task.isTimeLimitExceeded()) {
                    Thread.sleep(intervalMillis);
                    try {
                        areJobsAvailable = areHistoryJobsAvailable(managementService);
                    } catch (Throwable t) {
                        // Ignore, possible that exception occurs due to locking/updating of table on MSSQL when
                        // isolation level doesn't allow READ of the table
                    }
                }
            } catch (InterruptedException e) {
                // ignore
            } finally {
                timer.cancel();
            }
            if (areJobsAvailable) {
                throw new FlowableException("time limit of " + maxMillisToWait + " was exceeded");
            }

        } finally {
            if (shutdownExecutorWhenFinished) {
                asyncHistoryExecutor.shutdown();
            }
        }
    }
}
 
Example 11
Source File: CmmnEngineConfiguratorAsyncJobTest.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Test
public void testSharedAsyncExecutor() throws Exception {
    // The async executor should be the same instance
    AsyncExecutor processEngineAsyncExecutor = processEngine.getProcessEngineConfiguration().getAsyncExecutor();
    AsyncExecutor cmmnEngineAsyncExecutor = cmmnEngine.getCmmnEngineConfiguration().getAsyncExecutor();
    assertThat(processEngineAsyncExecutor).isNotNull();
    assertThat(cmmnEngineAsyncExecutor).isNotNull();

    // Contrary to the asyncHistoryExecutor, the async executors are not shared between the engines (by default)
    assertThat(cmmnEngineAsyncExecutor).isNotSameAs(processEngineAsyncExecutor);

    assertThat(processEngineAsyncExecutor.getJobServiceConfiguration().getJobExecutionScope()).isNull();
    assertThat(cmmnEngineAsyncExecutor.getJobServiceConfiguration().getJobExecutionScope()).isEqualTo(JobServiceConfiguration.JOB_EXECUTION_SCOPE_CMMN);

    // Deploy and start test processes/cases
    // Trigger one plan item instance to start the process
    processEngine.getRepositoryService().createDeployment()
            .addClasspathResource("org/flowable/cmmn/test/CmmnEngineConfiguratorAsyncJobTest.taskAndTimer.bpmn20.xml").deploy();
    cmmnEngine.getCmmnRepositoryService().createDeployment()
            .addClasspathResource("org/flowable/cmmn/test/CmmnEngineConfiguratorAsyncJobTest.processAndTimer.cmmn.xml").deploy();

    // Starting the case instance starts the process task. The process has an async job at the beginning
    cmmnEngine.getCmmnRuntimeService().createCaseInstanceBuilder().caseDefinitionKey("timerAndProcess").start();
    Job job = processEngine.getManagementService().createJobQuery().singleResult();
    assertThat(job.getScopeType()).isNull();
    JobTestHelper.waitForJobExecutorToProcessAllJobs(processEngine.getProcessEngineConfiguration(), processEngine.getManagementService(), 10000L, 100L);

    // There should now be two timers, one for the case and one for the process
    List<Job> timerJobs = processEngine.getManagementService().createTimerJobQuery().list();
    timerJobs.forEach(timerJob -> {
        if (timerJob.getScopeId() != null) { // cmmn
            assertThat(timerJob.getScopeType()).isEqualTo(JobServiceConfiguration.JOB_EXECUTION_SCOPE_CMMN);
        } else {
            assertThat(timerJob.getScopeType()).isNull();
        }

        processEngine.getManagementService().moveTimerToExecutableJob(timerJob.getId());
    });

    // Can't use the JobTestHelper's, so manually starting the executors
    processEngineAsyncExecutor.start();
    cmmnEngineAsyncExecutor.start();

    try {
        long startTime = new Date().getTime();
        while (processEngine.getManagementService().createJobQuery().count() > 0
                && (new Date().getTime() - startTime > 10000)) {
            Thread.sleep(100L);
        }
    } finally {
        processEngineAsyncExecutor.shutdown();
        cmmnEngineAsyncExecutor.shutdown();
    }

    // There should be one user task which is async (from the case)
    job = processEngine.getManagementService().createJobQuery().singleResult();
    assertThat(job.getScopeType()).isEqualTo(JobServiceConfiguration.JOB_EXECUTION_SCOPE_CMMN);
    CmmnJobTestHelper.waitForJobExecutorToProcessAllJobs(cmmnEngine, 10000L, 100L, true);

    // There should be two user tasks now: one after the timer of the case and one after the timer of the process
    assertThat(processEngine.getTaskService().createTaskQuery().count()).isEqualTo(2);
    assertThat(cmmnEngine.getCmmnTaskService().createTaskQuery().count()).isEqualTo(2);

}