Пример #1
0
  public void afterJob(JobExecution jobExecution) {
    StringBuilder protocol = new StringBuilder();
    protocol.append("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n");
    protocol.append("Protocol for " + jobExecution.getJobInstance().getJobName() + " \n");
    protocol.append("  Started     : " + jobExecution.getStartTime() + "\n");
    protocol.append("  Finished    : " + jobExecution.getEndTime() + "\n");
    protocol.append("  Exit-Code   : " + jobExecution.getExitStatus().getExitCode() + "\n");
    protocol.append("  Exit-Descr. : " + jobExecution.getExitStatus().getExitDescription() + "\n");
    protocol.append("  Status      : " + jobExecution.getStatus() + "\n");
    protocol.append("+++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n");

    protocol.append("Job-Parameter: \n");
    JobParameters jp = jobExecution.getJobParameters();
    for (Iterator<Entry<String, JobParameter>> iter = jp.getParameters().entrySet().iterator();
        iter.hasNext(); ) {
      Entry<String, JobParameter> entry = iter.next();
      protocol.append("  " + entry.getKey() + "=" + entry.getValue() + "\n");
    }
    protocol.append("+++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n");

    for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
      protocol.append("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n");
      protocol.append("Step " + stepExecution.getStepName() + " \n");
      protocol.append("WriteCount: " + stepExecution.getWriteCount() + "\n");
      protocol.append("Commits: " + stepExecution.getCommitCount() + "\n");
      protocol.append("SkipCount: " + stepExecution.getSkipCount() + "\n");
      protocol.append("Rollbacks: " + stepExecution.getRollbackCount() + "\n");
      protocol.append("Filter: " + stepExecution.getFilterCount() + "\n");
      protocol.append("+++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n");
    }
    LOGGER.info(protocol.toString());
  }
  /**
   * @throws IOException if a temporary file cannot be created.
   * @throws NoSuchJobException if SpeciesPageHarvestingJob cannot be located
   * @throws JobParametersInvalidException if the job parameters are invalid
   * @throws JobInstanceAlreadyCompleteException if the job has already completed
   * @throws JobRestartException if the job cannot be restarted
   * @throws JobExecutionAlreadyRunningException if the job is already running
   */
  @Test
  public final void testNotModifiedResponse()
      throws IOException, NoSuchJobException, JobExecutionAlreadyRunningException,
          JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
    Map<String, JobParameter> parameters = new HashMap<String, JobParameter>();
    parameters.put("query.string", new JobParameter("select i from Image i"));

    JobParameters jobParameters = new JobParameters(parameters);

    Job job = jobLocator.getJob("ImageProcessing");
    assertNotNull("ImageProcessing must not be null", job);
    JobExecution jobExecution = jobLauncher.run(job, jobParameters);
    assertEquals(
        "The job should complete successfully",
        jobExecution.getExitStatus().getExitCode(),
        "COMPLETED");

    for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
      logger.info(
          stepExecution.getStepName()
              + " "
              + stepExecution.getReadCount()
              + " "
              + stepExecution.getFilterCount()
              + " "
              + stepExecution.getWriteCount());
    }
  }
 private List<Object[]> buildStepExecutionParameters(StepExecution stepExecution) {
   Assert.isNull(
       stepExecution.getId(),
       "to-be-saved (not updated) StepExecution can't already have an id assigned");
   Assert.isNull(
       stepExecution.getVersion(),
       "to-be-saved (not updated) StepExecution can't already have a version assigned");
   validateStepExecution(stepExecution);
   stepExecution.setId(stepExecutionIncrementer.nextLongValue());
   stepExecution.incrementVersion(); // Should be 0
   List<Object[]> parameters = new ArrayList<Object[]>();
   String exitDescription =
       truncateExitDescription(stepExecution.getExitStatus().getExitDescription());
   Object[] parameterValues =
       new Object[] {
         stepExecution.getId(),
         stepExecution.getVersion(),
         stepExecution.getStepName(),
         stepExecution.getJobExecutionId(),
         stepExecution.getStartTime(),
         stepExecution.getEndTime(),
         stepExecution.getStatus().toString(),
         stepExecution.getCommitCount(),
         stepExecution.getReadCount(),
         stepExecution.getFilterCount(),
         stepExecution.getWriteCount(),
         stepExecution.getExitStatus().getExitCode(),
         exitDescription,
         stepExecution.getReadSkipCount(),
         stepExecution.getWriteSkipCount(),
         stepExecution.getProcessSkipCount(),
         stepExecution.getRollbackCount(),
         stepExecution.getLastUpdated()
       };
   Integer[] parameterTypes =
       new Integer[] {
         Types.BIGINT,
         Types.INTEGER,
         Types.VARCHAR,
         Types.BIGINT,
         Types.TIMESTAMP,
         Types.TIMESTAMP,
         Types.VARCHAR,
         Types.INTEGER,
         Types.INTEGER,
         Types.INTEGER,
         Types.INTEGER,
         Types.VARCHAR,
         Types.VARCHAR,
         Types.INTEGER,
         Types.INTEGER,
         Types.INTEGER,
         Types.INTEGER,
         Types.TIMESTAMP
       };
   parameters.add(0, Arrays.copyOf(parameterValues, parameterValues.length));
   parameters.add(1, Arrays.copyOf(parameterTypes, parameterTypes.length));
   return parameters;
 }
 @Test
 public void testSunnyDayFaultTolerant() throws Exception {
   JobExecution jobExecution =
       jobLauncher.run(
           job, new JobParameters(Collections.singletonMap("item.three", new JobParameter("3"))));
   assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
   StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next();
   assertEquals(9, stepExecution.getReadCount());
   assertEquals(9, stepExecution.getWriteCount());
 }
 /* (non-Javadoc)
  * @see org.springframework.batch.item.xml.StaxWriterCallback#write(javax.xml.stream.XMLEventWriter)
  */
 @Override
 public void write(XMLEventWriter writer) throws IOException {
   XMLEventFactory factory = XMLEventFactory.newInstance();
   try {
     writer.add(
         factory.createComment(
             "Total write count = " + stepExecution.getWriteCount() + ";credit 201310 end."));
   } catch (XMLStreamException e) {
     e.printStackTrace();
   }
 }
 @Test
 public void testFailedStepOnError() throws Exception {
   JobExecution jobExecution =
       jobLauncher.run(
           job,
           new JobParameters(Collections.singletonMap("item.three", new JobParameter("error"))));
   assertEquals(BatchStatus.FAILED, jobExecution.getStatus());
   StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next();
   assertEquals(9, stepExecution.getReadCount());
   // In principle the write count could be more than 2 and less than 9...
   assertEquals(7, stepExecution.getWriteCount());
 }
 @Test
 public void testSkipsInWriter() throws Exception {
   JobExecution jobExecution =
       jobLauncher.run(
           job,
           new JobParametersBuilder()
               .addString("item.three", "fail")
               .addLong("run.id", 1L)
               .toJobParameters());
   assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
   StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next();
   assertEquals(9, stepExecution.getReadCount());
   assertEquals(7, stepExecution.getWriteCount());
   // The whole chunk gets skipped...
   assertEquals(2, stepExecution.getWriteSkipCount());
 }
  @Override
  public void updateStepExecution(StepExecution stepExecution) {

    validateStepExecution(stepExecution);
    Assert.notNull(
        stepExecution.getId(),
        "StepExecution Id cannot be null. StepExecution must saved" + " before it can be updated.");

    // Do not check for existence of step execution considering
    // it is saved at every commit point.

    String exitDescription =
        truncateExitDescription(stepExecution.getExitStatus().getExitDescription());

    // Attempt to prevent concurrent modification errors by blocking here if
    // someone is already trying to do it.
    synchronized (stepExecution) {
      Integer version = stepExecution.getVersion() + 1;
      Object[] parameters =
          new Object[] {
            stepExecution.getStartTime(),
            stepExecution.getEndTime(),
            stepExecution.getStatus().toString(),
            stepExecution.getCommitCount(),
            stepExecution.getReadCount(),
            stepExecution.getFilterCount(),
            stepExecution.getWriteCount(),
            stepExecution.getExitStatus().getExitCode(),
            exitDescription,
            version,
            stepExecution.getReadSkipCount(),
            stepExecution.getProcessSkipCount(),
            stepExecution.getWriteSkipCount(),
            stepExecution.getRollbackCount(),
            stepExecution.getLastUpdated(),
            stepExecution.getId(),
            stepExecution.getVersion()
          };
      int count =
          getJdbcTemplate()
              .update(
                  getQuery(UPDATE_STEP_EXECUTION),
                  parameters,
                  new int[] {
                    Types.TIMESTAMP,
                    Types.TIMESTAMP,
                    Types.VARCHAR,
                    Types.INTEGER,
                    Types.INTEGER,
                    Types.INTEGER,
                    Types.INTEGER,
                    Types.VARCHAR,
                    Types.VARCHAR,
                    Types.INTEGER,
                    Types.INTEGER,
                    Types.INTEGER,
                    Types.INTEGER,
                    Types.INTEGER,
                    Types.TIMESTAMP,
                    Types.BIGINT,
                    Types.INTEGER
                  });

      // Avoid concurrent modifications...
      if (count == 0) {
        int curentVersion =
            getJdbcTemplate()
                .queryForObject(
                    getQuery(CURRENT_VERSION_STEP_EXECUTION),
                    new Object[] {stepExecution.getId()},
                    Integer.class);
        throw new OptimisticLockingFailureException(
            "Attempt to update step execution id="
                + stepExecution.getId()
                + " with wrong version ("
                + stepExecution.getVersion()
                + "), where current version is "
                + curentVersion);
      }

      stepExecution.incrementVersion();
    }
  }