Search in sources :

Example 6 with StepExecution

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

the class BatchListenersTest method testBatchListeners.

/**
 * 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 the +javax.batch.runtime.Metric+ object available in the step execution and
 * also verify if the listeners were executed correctly via a +CountDownLatch+ wait.
 *
 * The batch process itself will read and process 10 elements from numbers  1 to 10, but only write the odd
 * elements.
 *
 * * Each listener will decrement the total value of the +CountDownLatch+, until all the predicted events are
 * executed. The number of predicted events is 60:
 *
 * - +MyJobListener+ executes 2 times, 1 for +MyJobListener#beforeJob+ and 1 more for +MyJobListener#afterJob+.
 *
 * - +MyStepListener+ executes 2 times, 1 for +MyStepListener#beforeStep+ and 1 more for +MyStepListener#afterStep+.
 *
 * - +MyChunkListener+ executes 8 times, 4 for +MyChunkListener#beforeChunk+ and 4 more
 * for +MyChunkListener#afterChunk+. Chunk size is set to 3 and the total elements is 10, so 10/3 = 3 and 1 more
 * for the last element, means 4 for each chunk listener event.
 *
 * - +MyItemReader+ executes 22 times, 10 elements in total plus an empty read, so +MyItemReadListener#beforeRead+
 * executes 11 times and +MyItemReadListener#afterRead+ the other 11 times.
 *
 * - +MyItemProcessorListener+ executes 20 times, 10 elements read in total,
 * so +MyItemProcessorLister#beforeProcess+ executes 10 times
 * and +MyItemProcessorLister#afterProcess+ the other 10 times.
 *
 * - +MyItemWriterListener+ executed 6 times, 3 times for +MyItemWriterListener#beforeWrite+ and another 3 times
 * for +MyItemWriterListener#afterWrite+. This one is a bit more tricky, since not every element needs to be
 * written. Looking at +MyItemProcessor+, only even records are going to be written. We also need to take into
 * account the elements read per chunk, so: Chunk[1] read and process [1,2,3] and wrote [2,6], Chunk[2] read and
 * process [4,5,6] and wrote [10], Chunk[3] read and process [7,8,9] and wrote [14,18], Chunk[4] read and process
 * [10] and did not wrote anything, so only 3 writes for the full processing.
 *
 * - Total: 2 + 2 + 8 + 22 + 20 + 6 = 60
 *
 * @throws Exception an exception if the batch could not complete successfully.
 */
@Test
public void testBatchListeners() throws Exception {
    JobOperator jobOperator = getJobOperator();
    Long executionId = jobOperator.start("myJob", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
    List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
    for (StepExecution stepExecution : stepExecutions) {
        if (stepExecution.getStepName().equals("myStep")) {
            Map<Metric.MetricType, Long> metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics());
            assertEquals(10L, metricsMap.get(Metric.MetricType.READ_COUNT).longValue());
            assertEquals(10L / 2L, metricsMap.get(Metric.MetricType.WRITE_COUNT).longValue());
            assertEquals(10L / 3 + (10L % 3 > 0 ? 1 : 0), metricsMap.get(Metric.MetricType.COMMIT_COUNT).longValue());
        }
    }
    assertTrue(BatchListenerRecorder.batchListenersCountDownLatch.await(0, SECONDS));
    assertEquals(jobExecution.getBatchStatus(), COMPLETED);
}
Also used : JobExecution(javax.batch.runtime.JobExecution) JobOperator(javax.batch.operations.JobOperator) BatchRuntime.getJobOperator(javax.batch.runtime.BatchRuntime.getJobOperator) Properties(java.util.Properties) StepExecution(javax.batch.runtime.StepExecution) Test(org.junit.Test)

Example 7 with StepExecution

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

the class BatchChunkCheckpointTest method testBatchChunkCheckpoint.

/**
 * 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 the +javax.batch.runtime.Metric+ object available in the step execution.
 *
 * The batch process itself will read and process 10 elements from numbers  1 to 10, but only write the odd
 * elements. Commits are executed after 5 elements are read by the custom checkpoint algorithm.
 *
 * @throws Exception an exception if the batch could not complete successfully.
 */
@Test
public void testBatchChunkCheckpoint() throws Exception {
    JobOperator jobOperator = getJobOperator();
    Long executionId = jobOperator.start("myJob", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
    for (StepExecution stepExecution : jobOperator.getStepExecutions(executionId)) {
        if (stepExecution.getStepName().equals("myStep")) {
            Map<Metric.MetricType, Long> metricsMap = getMetricsMap(stepExecution.getMetrics());
            // <1> The read count should be 10 elements. Check +MyItemReader+.
            assertEquals(10L, metricsMap.get(READ_COUNT).longValue());
            // <2> The write count should be 5. Only half of the elements read are processed to be written.
            assertEquals(10L / 2L, metricsMap.get(WRITE_COUNT).longValue());
            // <3> The commit count should be 3. Checkpoint is on every 5th read, plus one final read-commit.
            assertEquals(10L / 5L + 1, metricsMap.get(COMMIT_COUNT).longValue());
        }
    }
    // <4> The checkpoint algorithm should be checked 10 times. One for each element read.
    assertTrue(checkpointCountDownLatch.await(0, SECONDS));
    // <5> Job should be completed.
    assertEquals(jobExecution.getBatchStatus(), COMPLETED);
}
Also used : JobExecution(javax.batch.runtime.JobExecution) JobOperator(javax.batch.operations.JobOperator) BatchRuntime.getJobOperator(javax.batch.runtime.BatchRuntime.getJobOperator) Properties(java.util.Properties) StepExecution(javax.batch.runtime.StepExecution) Test(org.junit.Test)

Example 8 with StepExecution

use of javax.batch.runtime.StepExecution in project Payara by payara.

the class ListBatchJobSteps method executeCommand.

@Override
protected void executeCommand(AdminCommandContext context, Properties extraProps) throws Exception {
    ColumnFormatter columnFormatter = new ColumnFormatter(getDisplayHeaders());
    List<Map<String, Object>> jobExecutions = new ArrayList<>();
    extraProps.put(LIST_BATCH_JOBS_STEPS, jobExecutions);
    for (StepExecution je : findStepExecutions()) {
        try {
            jobExecutions.add(handleJob(je, columnFormatter));
        } catch (Exception ex) {
            logger.log(Level.WARNING, "Exception while getting jobExecution details: " + ex);
            logger.log(Level.FINE, "Exception while getting jobExecution details ", ex);
        }
    }
    context.getActionReport().setMessage(columnFormatter.toString());
}
Also used : StepExecution(javax.batch.runtime.StepExecution) ColumnFormatter(com.sun.enterprise.util.ColumnFormatter) NoSuchJobExecutionException(javax.batch.operations.NoSuchJobExecutionException) JobSecurityException(javax.batch.operations.JobSecurityException)

Example 9 with StepExecution

use of javax.batch.runtime.StepExecution in project Payara by payara.

the class ListBatchJobSteps method findStepExecutions.

private List<StepExecution> findStepExecutions() throws JobSecurityException, NoSuchJobExecutionException {
    JobOperator jobOperator = AbstractListCommand.getJobOperatorFromBatchRuntime();
    JobExecution je = jobOperator.getJobExecution(Long.parseLong(executionId));
    if (!glassFishBatchSecurityHelper.isVisibleToThisInstance(((TaggedJobExecution) je).getTagName()))
        throw new NoSuchJobExecutionException("No job execution exists for job execution id: " + executionId);
    List<StepExecution> stepExecutions = jobOperator.getStepExecutions(Long.parseLong(executionId));
    if (stepExecutions == null || stepExecutions.size() == 0)
        throw new NoSuchJobExecutionException("No job execution exists for job execution id: " + executionId);
    return stepExecutions;
}
Also used : JobExecution(javax.batch.runtime.JobExecution) TaggedJobExecution(com.ibm.jbatch.spi.TaggedJobExecution) TaggedJobExecution(com.ibm.jbatch.spi.TaggedJobExecution) JobOperator(javax.batch.operations.JobOperator) NoSuchJobExecutionException(javax.batch.operations.NoSuchJobExecutionException) StepExecution(javax.batch.runtime.StepExecution)

Example 10 with StepExecution

use of javax.batch.runtime.StepExecution in project Payara by payara.

the class JBatchJDBCPersistenceManager method getStepExecutionsForJobExecution.

@Override
public List<StepExecution> getStepExecutionsForJobExecution(long execid) {
    List<StepExecution> data = new ArrayList<>();
    try (Connection conn = getConnection();
        PreparedStatement statement = conn.prepareStatement(queryStrings.get(STEP_EXECUTIONS_FOR_JOB_EXECUTION))) {
        statement.setLong(1, execid);
        try (ResultSet rs = statement.executeQuery()) {
            while (rs.next()) {
                long jobexecid = rs.getLong("jobexecid");
                long stepexecid = rs.getLong("stepexecid");
                String stepname = rs.getString("stepname");
                String batchstatus = rs.getString("batchstatus");
                String exitstatus = rs.getString("exitstatus");
                long readCount = rs.getLong("readcount");
                long writeCount = rs.getLong("writecount");
                long commitCount = rs.getLong("commitcount");
                long rollbackCount = rs.getLong("rollbackcount");
                long readSkipCount = rs.getLong("readskipcount");
                long processSkipCount = rs.getLong("processskipcount");
                long filterCount = rs.getLong("filtercount");
                long writeSkipCount = rs.getLong("writeSkipCount");
                Timestamp startTS = rs.getTimestamp("startTime");
                Timestamp endTS = rs.getTimestamp("endTime");
                // get the object based data
                Serializable persistentData = null;
                byte[] pDataBytes = rs.getBytes("persistentData");
                if (pDataBytes != null) {
                    try (ObjectInputStream objectIn = new TCCLObjectInputStream(new ByteArrayInputStream(pDataBytes))) {
                        persistentData = (Serializable) objectIn.readObject();
                    }
                }
                StepExecutionImpl stepEx = new StepExecutionImpl(jobexecid, stepexecid);
                stepEx.setBatchStatus(BatchStatus.valueOf(batchstatus));
                stepEx.setExitStatus(exitstatus);
                stepEx.setStepName(stepname);
                stepEx.setReadCount(readCount);
                stepEx.setWriteCount(writeCount);
                stepEx.setCommitCount(commitCount);
                stepEx.setRollbackCount(rollbackCount);
                stepEx.setReadSkipCount(readSkipCount);
                stepEx.setProcessSkipCount(processSkipCount);
                stepEx.setFilterCount(filterCount);
                stepEx.setWriteSkipCount(writeSkipCount);
                stepEx.setStartTime(startTS);
                stepEx.setEndTime(endTS);
                stepEx.setPersistentUserData(persistentData);
                logger.log(Level.FINE, "BatchStatus: {0} | StepName: {1} | JobExecID: {2} | StepExecID: {3}", new Object[] { batchstatus, stepname, jobexecid, stepexecid });
                data.add(stepEx);
            }
        }
    } catch (SQLException | IOException | ClassNotFoundException e) {
        throw new PersistenceException(e);
    }
    return data;
}
Also used : Serializable(java.io.Serializable) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) StepExecution(javax.batch.runtime.StepExecution) Timestamp(java.sql.Timestamp) TCCLObjectInputStream(com.ibm.jbatch.container.util.TCCLObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ResultSet(java.sql.ResultSet) StepExecutionImpl(com.ibm.jbatch.container.jobinstance.StepExecutionImpl) PersistenceException(com.ibm.jbatch.container.exception.PersistenceException) ObjectInputStream(java.io.ObjectInputStream) TCCLObjectInputStream(com.ibm.jbatch.container.util.TCCLObjectInputStream)

Aggregations

StepExecution (javax.batch.runtime.StepExecution)19 JobOperator (javax.batch.operations.JobOperator)16 Properties (java.util.Properties)14 JobExecution (javax.batch.runtime.JobExecution)14 BatchRuntime.getJobOperator (javax.batch.runtime.BatchRuntime.getJobOperator)13 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)5 PersistenceException (com.ibm.jbatch.container.exception.PersistenceException)2 StepExecutionImpl (com.ibm.jbatch.container.jobinstance.StepExecutionImpl)2 TCCLObjectInputStream (com.ibm.jbatch.container.util.TCCLObjectInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 ObjectInputStream (java.io.ObjectInputStream)2 Serializable (java.io.Serializable)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Timestamp (java.sql.Timestamp)2 HashMap (java.util.HashMap)2