org.springframework.batch.item.ExecutionContext.getString()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(15.6k)|赞(0)|评价(0)|浏览(80)

本文整理了Java中org.springframework.batch.item.ExecutionContext.getString()方法的一些代码示例,展示了ExecutionContext.getString()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ExecutionContext.getString()方法的具体详情如下:
包路径:org.springframework.batch.item.ExecutionContext
类名称:ExecutionContext
方法名:getString

ExecutionContext.getString介绍

[英]Typesafe Getter for the String represented by the provided key.
[中]由提供的键表示的字符串的Typesafe Getter。

代码示例

代码示例来源:origin: spring-projects/spring-batch

/**
 * Typesafe Getter for the String represented by the provided key with
 * default value to return if key is not represented.
 *
 * @param key The key to get a value for
 * @param defaultString Default to return if key is not represented
 * @return The <code>String</code> value if key is represented, specified
 * default otherwise
 */
public String getString(String key, String defaultString) {
  if (!containsKey(key)) {
    return defaultString;
  }
  return getString(key);
}

代码示例来源:origin: spring-projects/spring-batch

@BeforeStep
public void createOutputNameFromInput(StepExecution stepExecution) {
  ExecutionContext executionContext = stepExecution.getExecutionContext();
  String inputName = stepExecution.getStepName().replace(":", "-");
  if (executionContext.containsKey(inputKeyName)) {
    inputName = executionContext.getString(inputKeyName);
  }
  if (!executionContext.containsKey(outputKeyName)) {
    executionContext.putString(outputKeyName, path + FilenameUtils.getBaseName(inputName)
        + ".csv");
  }
}

代码示例来源:origin: spring-projects/spring-batch

@Override
protected State nextState(String stateName, FlowExecutionStatus status, StepExecution stepExecution) throws FlowExecutionException {
  State nextState = findState(stateName, status, stepExecution);
  if(stepExecution != null) {
    ExecutionContext executionContext = stepExecution.getJobExecution().getExecutionContext();
    if(executionContext.containsKey("batch.stoppedStep")) {
      String stepName = executionContext.getString("batch.stoppedStep");
      if(stateName.endsWith(stepName)) {
        if(nextState != null && executionContext.containsKey("batch.restartStep") && StringUtils.hasText(executionContext.getString("batch.restartStep"))) {
          nextState = findState(stateName, new FlowExecutionStatus(status.getName() + ".RESTART"), stepExecution);
        }
      }
    }
  }
  return nextState;
}

代码示例来源:origin: spring-projects/spring-batch

private void saveAndAssert(final StepExecution stepExecution, final String value) {
  new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
    @Override
    public Void doInTransaction(TransactionStatus status) {
      stepExecution.getExecutionContext().put("foo", value);
      dao.saveExecutionContext(stepExecution);
      return null;
    }
  });
  ExecutionContext executionContext = dao.getExecutionContext(stepExecution);
  Assert.state(executionContext != null, "Lost insert: null executionContext at value=" + value);
  String foo = executionContext.getString("foo");
  Assert.state(value.equals(foo), "Lost update: wrong value=" + value + " (found " + foo + ") for id="
      + stepExecution.getId());
}

代码示例来源:origin: spring-projects/spring-batch

@Test
public void testReadFile() throws Exception {
  Map<String, ExecutionContext> partition = partitioner.partition(0);
  String url = partition.get("partition0").getString("fileName");
  assertTrue(new UrlResource(url).exists());
}

代码示例来源:origin: spring-projects/spring-batch

@Test
  public void testSetInputKeyName() {
    listener.setPath("");
    listener.setInputKeyName("spam");
    stepExecution.getExecutionContext().putString("spam", "bar");
    listener.createOutputNameFromInput(stepExecution);
    assertEquals("bar.csv", stepExecution.getExecutionContext().getString("outputFile"));
  }
}

代码示例来源:origin: spring-projects/spring-batch

@Test
public void testExecutionContext() throws Exception {
  assertNotNull(execution.getExecutionContext());
  ExecutionContext context = new ExecutionContext();
  context.putString("foo", "bar");
  execution.setExecutionContext(context);
  assertEquals("bar", execution.getExecutionContext().getString("foo"));
}

代码示例来源:origin: spring-projects/spring-batch

@Test
public void testNormalUsage() {
  context.putString("1", "testString1");
  context.putString("2", "testString2");
  context.putLong("3", 3);
  context.putDouble("4", 4.4);
  context.putInt("5", 5);
  assertEquals("testString1", context.getString("1"));
  assertEquals("testString2", context.getString("2"));
  assertEquals("defaultString", context.getString("55", "defaultString"));
  assertEquals(4.4, context.getDouble("4"), 0);
  assertEquals(5.5, context.getDouble("55", 5.5), 0);
  assertEquals(3, context.getLong("3"));
  assertEquals(5, context.getLong("55", 5));
  assertEquals(5, context.getInt("5"));
  assertEquals(6, context.getInt("55", 6));
}

代码示例来源:origin: spring-projects/spring-batch

@Override
  public Collection<StepExecution> handle(StepExecutionSplitter stepSplitter, StepExecution stepExecution)
      throws Exception {
    Set<StepExecution> executions = stepSplitter.split(stepExecution, 2);
    if (!started.get()) {
      started.set(true);
      for (StepExecution execution : executions) {
        execution.setStatus(BatchStatus.FAILED);
        execution.setExitStatus(ExitStatus.FAILED);
        execution.getExecutionContext().putString("foo", execution.getStepName());
      }
    }
    else {
      for (StepExecution execution : executions) {
        // On restart the execution context should have been restored
        assertEquals(execution.getStepName(), execution.getExecutionContext().getString("foo"));
      }
    }
    for (StepExecution execution : executions) {
      jobRepository.update(execution);
      jobRepository.updateExecutionContext(execution);
    }
    return executions;
  }
});

代码示例来源:origin: spring-projects/spring-batch

@Test
public void testLaunchJob() throws Exception {
  JobExecution jobExecution = jobLauncherTestUtils.launchJob(new JobParametersBuilder().addString("value", "foo")
      .toJobParameters());
  assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
  assertEquals("yes", jobExecution.getExecutionContext().getString("done"));
}

代码示例来源:origin: spring-projects/spring-batch

/**
 * CONDITION: keys = {key}. key is already in job but not in step.
 * 
 * EXPECTED: key is not erased.
 */
@Test
public void promoteEntriesKeyNotFoundInStep() throws Exception {
  ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener();
  JobExecution jobExecution = new JobExecution(1L);
  StepExecution stepExecution = jobExecution.createStepExecution("step1");
  stepExecution.setExitStatus(ExitStatus.COMPLETED);
  Assert.state(jobExecution.getExecutionContext().isEmpty(), "Job ExecutionContext is not empty");
  Assert.state(stepExecution.getExecutionContext().isEmpty(), "Step ExecutionContext is not empty");
  jobExecution.getExecutionContext().putString(key, value);
  listener.setKeys(new String[] { key });
  listener.afterPropertiesSet();
  listener.afterStep(stepExecution);
  assertEquals(value, jobExecution.getExecutionContext().getString(key));
}

代码示例来源:origin: spring-projects/spring-batch

/**
 * Execution context must not be left empty even if job failed before
 * committing first chunk - otherwise ItemStreams won't recognize it is
 * restart scenario on next run.
 */
@Test
public void testRestartAfterFailureInFirstChunk() throws Exception {
  MockRestartableItemReader reader = new MockRestartableItemReader() {
    @Override
    public String read() throws RuntimeException {
      // fail on the very first item
      throw new RuntimeException("CRASH!");
    }
  };
  step.setTasklet(new TestingChunkOrientedTasklet<>(reader, itemWriter));
  step.registerStream(reader);
  StepExecution stepExecution = new StepExecution(step.getName(), new JobExecution(jobInstance, jobParameters));
  step.execute(stepExecution);
  assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
  Throwable expected = stepExecution.getFailureExceptions().get(0);
  assertEquals("CRASH!", expected.getMessage());
  assertFalse(stepExecution.getExecutionContext().isEmpty());
  assertTrue(stepExecution.getExecutionContext().getString("spam").equals("bucket"));
}

代码示例来源:origin: spring-projects/spring-batch

@Test
public void testDirectlyInjectedItemStream() throws Exception {
  step.setStreams(new ItemStream[] { new ItemStreamSupport() {
    @Override
    public void update(ExecutionContext executionContext) {
              super.update(executionContext);
      executionContext.putString("foo", "bar");
    }
  } });
  JobExecution jobExecution = new JobExecution(jobInstance, jobParameters);
  StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
  assertEquals(false, stepExecution.getExecutionContext().containsKey("foo"));
  step.execute(stepExecution);
  assertEquals("bar", stepExecution.getExecutionContext().getString("foo"));
}

代码示例来源:origin: spring-projects/spring-batch

/**
 * CONDITION: keys = {key, key2}. Only {key} exists in the ExecutionContext.
 * 
 * EXPECTED: key is promoted. key2 is not.
 */
@Test
public void promoteEntriesKeyNotFound() throws Exception {
  ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener();
  JobExecution jobExecution = new JobExecution(1L);
  StepExecution stepExecution = jobExecution.createStepExecution("step1");
  stepExecution.setExitStatus(ExitStatus.COMPLETED);
  Assert.state(jobExecution.getExecutionContext().isEmpty(), "Job ExecutionContext is not empty");
  Assert.state(stepExecution.getExecutionContext().isEmpty(), "Step ExecutionContext is not empty");
  stepExecution.getExecutionContext().putString(key, value);
  listener.setKeys(new String[] { key, key2 });
  listener.afterPropertiesSet();
  listener.afterStep(stepExecution);
  assertEquals(value, jobExecution.getExecutionContext().getString(key));
  assertFalse(jobExecution.getExecutionContext().containsKey(key2));
}

代码示例来源:origin: spring-projects/spring-batch

/**
 * CONDITION: ExecutionContext contains {key, key2}. keys = {key}. statuses
 * is not set (defaults to {COMPLETED}).
 * 
 * EXPECTED: key is promoted. key2 is not.
 */
@Test
public void promoteEntryNullStatuses() throws Exception {
  ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener();
  JobExecution jobExecution = new JobExecution(1L);
  StepExecution stepExecution = jobExecution.createStepExecution("step1");
  stepExecution.setExitStatus(ExitStatus.COMPLETED);
  Assert.state(jobExecution.getExecutionContext().isEmpty(), "Job ExecutionContext is not empty");
  Assert.state(stepExecution.getExecutionContext().isEmpty(), "Step ExecutionContext is not empty");
  stepExecution.getExecutionContext().putString(key, value);
  stepExecution.getExecutionContext().putString(key2, value2);
  listener.setKeys(new String[] { key });
  listener.afterPropertiesSet();
  listener.afterStep(stepExecution);
  assertEquals(value, jobExecution.getExecutionContext().getString(key));
  assertFalse(jobExecution.getExecutionContext().containsKey(key2));
}

代码示例来源:origin: spring-projects/spring-batch

/**
 * CONDITION: strict = true. keys = {key, key2}. Only {key} exists in the
 * ExecutionContext.
 * 
 * EXPECTED: IllegalArgumentException
 */
@Test(expected = IllegalArgumentException.class)
public void promoteEntriesKeyNotFoundStrict() throws Exception {
  ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener();
  listener.setStrict(true);
  JobExecution jobExecution = new JobExecution(1L);
  StepExecution stepExecution = jobExecution.createStepExecution("step1");
  stepExecution.setExitStatus(ExitStatus.COMPLETED);
  Assert.state(jobExecution.getExecutionContext().isEmpty(), "Job ExecutionContext is not empty");
  Assert.state(stepExecution.getExecutionContext().isEmpty(), "Step ExecutionContext is not empty");
  stepExecution.getExecutionContext().putString(key, value);
  listener.setKeys(new String[] { key, key2 });
  listener.afterPropertiesSet();
  listener.afterStep(stepExecution);
  assertEquals(value, jobExecution.getExecutionContext().getString(key));
  assertFalse(jobExecution.getExecutionContext().containsKey(key2));
}

代码示例来源:origin: spring-projects/spring-batch

/**
 * CONDITION: keys = {key, key2}. statuses = {statusWildcard}. ExitStatus =
 * status
 * 
 * EXPECTED: key is promoted. key2 is not.
 */
@Test
public void promoteEntryStatusWildcardFound() throws Exception {
  ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener();
  JobExecution jobExecution = new JobExecution(1L);
  StepExecution stepExecution = jobExecution.createStepExecution("step1");
  stepExecution.setExitStatus(new ExitStatus(status));
  Assert.state(jobExecution.getExecutionContext().isEmpty(), "Job ExecutionContext is not empty");
  Assert.state(stepExecution.getExecutionContext().isEmpty(), "Step ExecutionContext is not empty");
  stepExecution.getExecutionContext().putString(key, value);
  stepExecution.getExecutionContext().putString(key2, value2);
  listener.setKeys(new String[] { key });
  listener.setStatuses(new String[] { statusWildcard });
  listener.afterPropertiesSet();
  listener.afterStep(stepExecution);
  assertEquals(value, jobExecution.getExecutionContext().getString(key));
  assertFalse(jobExecution.getExecutionContext().containsKey(key2));
}

代码示例来源:origin: spring-projects/spring-batch

/**
 * CONDITION: ExecutionContext contains {key, key2}. keys = {key, key2}.
 * statuses = {status}. ExitStatus = status
 * 
 * EXPECTED: key is promoted. key2 is not.
 */
@Test
public void promoteEntryStatusFound() throws Exception {
  ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener();
  listener.setStrict(true);
  JobExecution jobExecution = new JobExecution(1L);
  StepExecution stepExecution = jobExecution.createStepExecution("step1");
  stepExecution.setExitStatus(new ExitStatus(status));
  Assert.state(jobExecution.getExecutionContext().isEmpty(), "Job ExecutionContext is not empty");
  Assert.state(stepExecution.getExecutionContext().isEmpty(), "Step ExecutionContext is not empty");
  stepExecution.getExecutionContext().putString(key, value);
  stepExecution.getExecutionContext().putString(key2, value2);
  listener.setKeys(new String[] { key });
  listener.setStatuses(new String[] { status });
  listener.afterPropertiesSet();
  listener.afterStep(stepExecution);
  assertEquals(value, jobExecution.getExecutionContext().getString(key));
  assertFalse(jobExecution.getExecutionContext().containsKey(key2));
}

代码示例来源:origin: spring-projects/spring-batch

@Test
public void testStreamManager() throws Exception {
  MockRestartableItemReader reader = new MockRestartableItemReader() {
    @Override
    public String read() {
      return "foo";
    }
    @Override
    public void update(ExecutionContext executionContext) {
              super.update(executionContext);
      executionContext.putString("foo", "bar");
    }
  };
  step.setTasklet(new TestingChunkOrientedTasklet<>(reader, itemWriter));
  step.registerStream(reader);
  JobExecution jobExecution = new JobExecution(jobInstance, jobParameters);
  StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
  assertEquals(false, stepExecution.getExecutionContext().containsKey("foo"));
  step.execute(stepExecution);
  // At least once in that process the statistics service was asked for
  // statistics...
  assertEquals("bar", stepExecution.getExecutionContext().getString("foo"));
}

代码示例来源:origin: spring-projects/spring-batch

@Test
public void testDirectlyInjectedStreamWhichIsAlsoReader() throws Exception {
  MockRestartableItemReader reader = new MockRestartableItemReader() {
    @Override
    public String read() {
      return "foo";
    }
    @Override
    public void update(ExecutionContext executionContext) {
              super.update(executionContext);
      executionContext.putString("foo", "bar");
    }
  };
  step.setTasklet(new TestingChunkOrientedTasklet<>(reader, itemWriter));
  step.setStreams(new ItemStream[] { reader });
  JobExecution jobExecution = new JobExecution(jobInstance, jobParameters);
  StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
  assertEquals(false, stepExecution.getExecutionContext().containsKey("foo"));
  step.execute(stepExecution);
  // At least once in that process the statistics service was asked for
  // statistics...
  assertEquals("bar", stepExecution.getExecutionContext().getString("foo"));
}

相关文章