Search in sources :

Example 21 with JobOperator

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

the class JobControlTestCase method testRestart.

@Test
public void testRestart() 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(2000)));
    ModelNode result = executeOperation(op);
    long executionId = result.asLong();
    Assert.assertTrue("Execution id should be greater than 0", executionId > 0L);
    // Test the stop operation
    op = Operations.createOperation("stop-job", address);
    op.get("execution-id").set(executionId);
    executeOperation(op);
    // Validate that the job as executed
    final JobOperator jobOperator = BatchRuntime.getJobOperator();
    JobExecution execution = jobOperator.getJobExecution(executionId);
    Assert.assertNotNull(execution);
    // Wait for 5 seconds max for the execution to finish.
    waitForTermination(execution, 5);
    // 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", address);
    op.get("execution-id").set(executionId);
    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 5 seconds max for the execution to finish.
    waitForTermination(execution, 5);
    // 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 22 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 23 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 24 with JobOperator

use of javax.batch.operations.JobOperator in project javaee7-firstcup by ecabrerar.

the class SalesBean method runJob.

public void runJob() {
    try {
        JobOperator jo = BatchRuntime.getJobOperator();
        long jobId = jo.start("eod-sales", new Properties());
        System.out.println("Started job: with id: " + jobId);
    } catch (JobStartException ex) {
        ex.printStackTrace();
    }
}
Also used : JobStartException(javax.batch.operations.JobStartException) JobOperator(javax.batch.operations.JobOperator) Properties(java.util.Properties)

Aggregations

JobOperator (javax.batch.operations.JobOperator)24 Test (org.junit.Test)21 Properties (java.util.Properties)17 JobExecution (javax.batch.runtime.JobExecution)10 ModelNode (org.jboss.dmr.ModelNode)5 ArrayList (java.util.ArrayList)4 StepExecution (javax.batch.runtime.StepExecution)2 TimeoutException (java.util.concurrent.TimeoutException)1 JobStartException (javax.batch.operations.JobStartException)1 NoSuchJobExecutionException (javax.batch.operations.NoSuchJobExecutionException)1 Query (javax.persistence.Query)1