@Test
  @Ignore // FIXME
  public void testTransactionException() throws Exception {

    final SkipWriterStub<String> writer = new SkipWriterStub<String>();
    FaultTolerantStepFactoryBean<String, String> factory =
        new FaultTolerantStepFactoryBean<String, String>();
    factory.setItemWriter(writer);

    @SuppressWarnings("serial")
    DataSourceTransactionManager transactionManager =
        new DataSourceTransactionManager(dataSource) {
          private boolean failed = false;

          @Override
          protected void doCommit(DefaultTransactionStatus status) throws TransactionException {
            if (writer.getWritten().isEmpty()
                || failed
                || !isExistingTransaction(status.getTransaction())) {
              super.doCommit(status);
              return;
            }
            failed = true;
            status.setRollbackOnly();
            super.doRollback(status);
            throw new UnexpectedRollbackException("Planned");
          }
        };

    factory.setBeanName("stepName");
    factory.setTransactionManager(transactionManager);
    factory.setCommitInterval(2);

    ItemReader<String> reader = new ListItemReader<String>(Arrays.asList("1", "2"));
    factory.setItemReader(reader);

    JobRepositoryFactoryBean repositoryFactory = new JobRepositoryFactoryBean();
    repositoryFactory.setDataSource(dataSource);
    repositoryFactory.setTransactionManager(transactionManager);
    repositoryFactory.afterPropertiesSet();
    JobRepository repository = repositoryFactory.getObject();
    factory.setJobRepository(repository);

    JobExecution jobExecution = repository.createJobExecution("job", new JobParameters());
    StepExecution stepExecution = jobExecution.createStepExecution(factory.getName());
    repository.add(stepExecution);

    Step step = factory.getObject();

    step.execute(stepExecution);
    assertEquals(BatchStatus.FAILED, stepExecution.getStatus());

    assertEquals("[]", writer.getCommitted().toString());
  }
コード例 #2
0
  public StepExecution execute() {

    Step step =
        (Step) new ClassPathXmlApplicationContext(configLocation).getBean(stepName, Step.class);
    ;

    logger.info("Spring Version: " + SpringVersion.getVersion());

    try {
      step.execute(stepExecution);
    } catch (JobInterruptedException e) {
      stepExecution.getJobExecution().setStatus(BatchStatus.STOPPING);
      throw new UnexpectedJobExecutionException("TODO: this should result in a stop", e);
    }

    return stepExecution;
  }
コード例 #3
0
 @Override
 public void afterPropertiesSet() throws Exception {
   StepExecution stepExecution = new StepExecution("step", new JobExecution(1L), 0L);
   step.execute(stepExecution);
   // expect no errors
 }