Search in sources :

Example 26 with JobOperator

use of javax.batch.operations.JobOperator in project wildfly by wildfly.

the class JobControlTestCase method testRestartOnExecutionResource.

@Test
public void testRestartOnExecutionResource() throws Exception {
    final ModelNode address = Operations.createAddress("deployment", DEPLOYMENT_NAME, "subsystem", "batch-jberet");
    ModelNode op = Operations.createOperation("start-job", address);
    op.get("job-xml-name").set("test-chunk");
    ModelNode properties = op.get("properties");
    properties.get("reader.end").set("20");
    // We're adding a long wait time to ensure we can stop, 1 seconds should be okay
    properties.get("writer.sleep.time").set(Integer.toString(TimeoutUtil.adjust(1000)));
    ModelNode result = executeOperation(op);
    long executionId = result.asLong();
    Assert.assertTrue("Execution id should be greater than 0", executionId > 0L);
    // Test the stop operation
    final ModelNode executionAddress = Operations.createAddress("deployment", DEPLOYMENT_NAME, "subsystem", "batch-jberet", "job", "test-chunk", "execution", Long.toString(executionId));
    executeOperation(Operations.createOperation("stop-job", executionAddress));
    // Validate that the job as executed
    final JobOperator jobOperator = BatchRuntime.getJobOperator();
    JobExecution execution = jobOperator.getJobExecution(executionId);
    Assert.assertNotNull(execution);
    // Wait for 1 seconds max for the execution to finish.
    waitForTermination(execution, 3);
    // Reset the counter as we're not sure how many were actually written
    currentCount = countingItemWriter.getWrittenItemSize();
    // Check that the status is stopped
    Assert.assertEquals(BatchStatus.STOPPED, execution.getBatchStatus());
    // Restart the execution
    op = Operations.createOperation("restart-job", executionAddress);
    properties = op.get("properties");
    properties.get("reader.end").set("10");
    properties.get("writer.sleep.time").set("0");
    result = executeOperation(op);
    executionId = result.asLong();
    Assert.assertTrue("Execution id should be greater than 0", executionId > 0L);
    execution = jobOperator.getJobExecution(executionId);
    Assert.assertNotNull(execution);
    // Wait for 3 seconds max for the execution to finish.
    waitForTermination(execution, 3);
    // Check that the status is stopped
    Assert.assertEquals(BatchStatus.COMPLETED, execution.getBatchStatus());
}
Also used : JobExecution(javax.batch.runtime.JobExecution) JobOperator(javax.batch.operations.JobOperator) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Example 27 with JobOperator

use of javax.batch.operations.JobOperator in project wildfly by wildfly.

the class BatchSubsystemSecurityTestCase method waitForJobEnd.

private void waitForJobEnd(Long id, int timeoutSeconds) throws TimeoutException {
    Long start = System.currentTimeMillis();
    final JobOperator operator = BatchRuntime.getJobOperator();
    while (System.currentTimeMillis() - start < (TimeoutUtil.adjust(timeoutSeconds) * 1000)) {
        if (operator.getJobExecution(id).getEndTime() != null)
            return;
    }
    throw new TimeoutException();
}
Also used : JobOperator(javax.batch.operations.JobOperator) TimeoutException(java.util.concurrent.TimeoutException)

Example 28 with JobOperator

use of javax.batch.operations.JobOperator in project Payara by payara.

the class BatchRuntimeHelper method event.

@Override
public void event(Event event) {
    try {
        if (event.is(EventTypes.SERVER_READY)) {
            for (String appName : applicationRegistry.getAllApplicationNames()) {
                ApplicationInfo applicationInfo = applicationRegistry.get(appName);
                registerIfBatchJobsDirExists(applicationInfo);
            }
        } else if (event.is(Deployment.APPLICATION_LOADED)) {
            if (event.hook() != null && event.hook() instanceof ApplicationInfo) {
                ApplicationInfo applicationInfo = (ApplicationInfo) event.hook();
                registerIfBatchJobsDirExists(applicationInfo);
            }
        }
        if (event.is(Deployment.UNDEPLOYMENT_SUCCESS)) {
            if (event.hook() != null && event.hook() instanceof DeploymentContextImpl) {
                DeploymentContextImpl deploymentContext = (DeploymentContextImpl) event.hook();
                Properties props = deploymentContext.getAppProps();
                String appName = props.getProperty("defaultAppName");
                if (!Boolean.parseBoolean(props.getProperty("retain-batch-jobs"))) {
                    String tagName = config.getName() + ":" + appName;
                    ClassLoader prevCL = Thread.currentThread().getContextClassLoader();
                    try {
                        // set TCCL to ensure loading of the Joboperator
                        Thread.currentThread().setContextClassLoader(BatchSPIManager.class.getClassLoader());
                        BatchSPIManager batchSPIManager = BatchSPIManager.getInstance();
                        if (batchSPIManager != null && batchSPIManager.getBatchJobUtil() != null) {
                            batchSPIManager.getBatchJobUtil().purgeOwnedRepositoryData(tagName);
                            tagNamesRequiringCleanup.remove(tagName);
                        } else if (tagNamesRequiringCleanup.contains(tagName)) {
                            // Force initialization of BatchRuntime
                            JobOperator jobOperator = BatchRuntime.getJobOperator();
                            if (batchSPIManager.getBatchJobUtil() != null) {
                                batchSPIManager.getBatchJobUtil().purgeOwnedRepositoryData(tagName);
                                tagNamesRequiringCleanup.remove(tagName);
                            }
                        }
                    } catch (Exception ex) {
                        logger.log(Level.FINE, "Error while purging jobs", ex);
                    } finally {
                        Thread.currentThread().setContextClassLoader(prevCL);
                    }
                }
            }
        }
    } catch (Exception ex) {
        logger.log(Level.FINE, "Exception while handling event: " + event, ex);
    }
}
Also used : ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) JobOperator(javax.batch.operations.JobOperator) NamingException(javax.naming.NamingException) DeploymentContextImpl(org.glassfish.deployment.common.DeploymentContextImpl)

Example 29 with JobOperator

use of javax.batch.operations.JobOperator in project quickstart by wildfly.

the class BatchController method getJobsExecution.

public Set<JobData> getJobsExecution() {
    Set<JobData> jobsData = new TreeSet<JobData>(new Comparator<JobData>() {

        @Override
        public int compare(JobData o1, JobData o2) {
            return o2.getJobInstanceId().compareTo(o1.getJobInstanceId());
        }
    });
    Map<Long, JobExecution> jobIntances = new HashMap<>();
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    try {
        List<JobInstance> instances = jobOperator.getJobInstances("import-file", 0, jobOperator.getJobInstanceCount("import-file"));
        for (JobInstance ji : instances) {
            List<JobExecution> executions = jobOperator.getJobExecutions(ji);
            for (JobExecution jobExecution : executions) {
                // initialize the map if null
                if (jobIntances.get(ji.getInstanceId()) == null) {
                    jobIntances.put(ji.getInstanceId(), jobExecution);
                }
                // Update the jobExecution if is newer
                JobExecution existing = jobIntances.get(ji.getInstanceId());
                if (jobExecution.getExecutionId() > existing.getExecutionId()) {
                    jobIntances.put(ji.getInstanceId(), jobExecution);
                }
            }
        }
        for (Long instaceId : jobIntances.keySet()) {
            JobExecution jobExecution = jobIntances.get(instaceId);
            JobInstance ji = jobOperator.getJobInstance(jobExecution.getExecutionId());
            Properties parameters = jobOperator.getParameters(jobExecution.getExecutionId());
            jobsData.add(new JobData(ji.getInstanceId(), jobExecution.getExecutionId(), ji.getJobName(), jobExecution.getCreateTime(), jobExecution.getEndTime(), jobExecution.getBatchStatus(), parameters, jobExecution.getExitStatus()));
        }
    } catch (NoSuchJobException e) {
    // It's ok if when doesn't have any jobs yet to show
    }
    return jobsData;
}
Also used : HashMap(java.util.HashMap) JobInstance(javax.batch.runtime.JobInstance) JobOperator(javax.batch.operations.JobOperator) Properties(java.util.Properties) JobExecution(javax.batch.runtime.JobExecution) NoSuchJobException(javax.batch.operations.NoSuchJobException) TreeSet(java.util.TreeSet)

Example 30 with JobOperator

use of javax.batch.operations.JobOperator in project quickstart by wildfly.

the class BatchController method restartJob.

public void restartJob(int executionId) {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Properties jobParameters = jobOperator.getParameters(executionId);
    jobParameters.setProperty("restartedOnce", "true");
    long newExecutionId = jobOperator.restart(executionId, jobParameters);
    facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Execution " + executionId + " restarted! New execution id: " + newExecutionId, null));
}
Also used : JobOperator(javax.batch.operations.JobOperator) Properties(java.util.Properties) FacesMessage(javax.faces.application.FacesMessage)

Aggregations

JobOperator (javax.batch.operations.JobOperator)31 Properties (java.util.Properties)22 Test (org.junit.Test)21 JobExecution (javax.batch.runtime.JobExecution)13 ModelNode (org.jboss.dmr.ModelNode)5 ArrayList (java.util.ArrayList)4 StepExecution (javax.batch.runtime.StepExecution)3 NoSuchJobExecutionException (javax.batch.operations.NoSuchJobExecutionException)2 FacesMessage (javax.faces.application.FacesMessage)2 TaggedJobExecution (com.ibm.jbatch.spi.TaggedJobExecution)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 TimeoutException (java.util.concurrent.TimeoutException)1 JobStartException (javax.batch.operations.JobStartException)1 NoSuchJobException (javax.batch.operations.NoSuchJobException)1 JobInstance (javax.batch.runtime.JobInstance)1 NamingException (javax.naming.NamingException)1 Query (javax.persistence.Query)1 DeploymentContextImpl (org.glassfish.deployment.common.DeploymentContextImpl)1 ApplicationInfo (org.glassfish.internal.data.ApplicationInfo)1