Search in sources :

Example 1 with JobExecution

use of javax.batch.runtime.JobExecution in project javaee7-samples by javaee-samples.

the class BatchSplitTest method testBatchSplit.

/**
     * In the test, we're just going to invoke the batch execution and wait for completion. To validate the test
     * expected behaviour we need to query +javax.batch.operations.JobOperator#getStepExecutions+.
     *
     * @throws Exception an exception if the batch could not complete successfully.
     */
@Test
public void testBatchSplit() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("myJob", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
    List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
    List<String> executedSteps = new ArrayList<>();
    for (StepExecution stepExecution : stepExecutions) {
        executedSteps.add(stepExecution.getStepName());
    }
    // <1> Make sure all the steps were executed.
    assertEquals(3, stepExecutions.size());
    assertTrue(executedSteps.contains("step1"));
    assertTrue(executedSteps.contains("step2"));
    assertTrue(executedSteps.contains("step3"));
    // <2> Steps 'step1' and 'step2' can appear in any order, since they were executed in parallel.
    assertTrue(executedSteps.get(0).equals("step1") || executedSteps.get(0).equals("step2"));
    assertTrue(executedSteps.get(1).equals("step1") || executedSteps.get(1).equals("step2"));
    // <3> Step 'step3' is always the last to be executed.
    assertTrue(executedSteps.get(2).equals("step3"));
    // <4> Job should be completed.
    assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus());
}
Also used : JobExecution(javax.batch.runtime.JobExecution) ArrayList(java.util.ArrayList) JobOperator(javax.batch.operations.JobOperator) Properties(java.util.Properties) StepExecution(javax.batch.runtime.StepExecution) Test(org.junit.Test)

Example 2 with JobExecution

use of javax.batch.runtime.JobExecution in project javaee7-samples by javaee-samples.

the class JmsItemReaderTest method runJob.

private void runJob() throws InterruptedException {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("jms-job", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    BatchTestHelper.keepTestAlive(jobExecution);
}
Also used : JobExecution(javax.batch.runtime.JobExecution) JobOperator(javax.batch.operations.JobOperator) Properties(java.util.Properties)

Example 3 with JobExecution

use of javax.batch.runtime.JobExecution in project wildfly by wildfly.

the class JobControlTestCase method testStop.

@Test
public void testStop() 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");
    final 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)));
    final ModelNode result = executeOperation(op);
    final 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();
    final 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());
}
Also used : JobExecution(javax.batch.runtime.JobExecution) JobOperator(javax.batch.operations.JobOperator) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Example 4 with JobExecution

use of javax.batch.runtime.JobExecution in project wildfly by wildfly.

the class JobControlTestCase method testStart.

@Test
public void testStart() throws Exception {
    final ModelNode address = Operations.createAddress("deployment", DEPLOYMENT_NAME, "subsystem", "batch-jberet");
    final ModelNode op = Operations.createOperation("start-job", address);
    op.get("job-xml-name").set("test-chunk");
    final ModelNode properties = op.get("properties");
    properties.get("reader.end").set("5");
    final ModelNode result = executeOperation(op);
    currentCount += 5;
    final long executionId = result.asLong();
    Assert.assertTrue("Execution id should be greater than 0", executionId > 0L);
    // Validate that the job as executed
    final JobOperator jobOperator = BatchRuntime.getJobOperator();
    final JobExecution execution = jobOperator.getJobExecution(executionId);
    Assert.assertNotNull(execution);
    // Wait for 3 seconds max for the execution to finish.
    waitForTermination(execution, 3);
    // Check that we have 5 items
    Assert.assertEquals(currentCount, countingItemWriter.getWrittenItemSize());
}
Also used : JobExecution(javax.batch.runtime.JobExecution) JobOperator(javax.batch.operations.JobOperator) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Example 5 with JobExecution

use of javax.batch.runtime.JobExecution in project wildfly by wildfly.

the class JobControlTestCase method testStopOnExecutionResource.

@Test
public void testStopOnExecutionResource() 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");
    final 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)));
    final ModelNode result = executeOperation(op);
    final 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();
    final 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());
}
Also used : JobExecution(javax.batch.runtime.JobExecution) JobOperator(javax.batch.operations.JobOperator) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Aggregations

JobExecution (javax.batch.runtime.JobExecution)17 Test (org.junit.Test)12 JobOperator (javax.batch.operations.JobOperator)10 Properties (java.util.Properties)6 ModelNode (org.jboss.dmr.ModelNode)5 ArrayList (java.util.ArrayList)3 StepExecution (javax.batch.runtime.StepExecution)2 RunAsClient (org.jboss.arquillian.container.test.api.RunAsClient)2 List (java.util.List)1 NoSuchJobExecutionException (javax.batch.operations.NoSuchJobExecutionException)1 JobInstance (javax.batch.runtime.JobInstance)1