List of usage examples for org.springframework.batch.item ItemWriter ItemWriter
ItemWriter
From source file:de.codecentric.batch.TestBatchConfiguration.java
@Bean public ItemWriter<? super Object> writer() { return new ItemWriter<Object>() { @Override public void write(List<? extends Object> items) throws Exception { System.out.println(items); }//from w w w . ja va 2 s . com }; }
From source file:io.spring.marchmadness.configuration.JobConfiguration.java
@Bean public ItemWriter<Bracket> itemWriter(BracketRepository bracketRepository) { return new ItemWriter<Bracket>() { private int brackets = 0; @Override//w w w.j a v a 2 s. c o m public void write(List<? extends Bracket> items) throws Exception { brackets += items.size(); System.out.println("written " + brackets + " brackets"); for (Bracket item : items) { bracketRepository.save(item); } } }; }
From source file:configuration.JobConfiguration.java
@Bean public Step step2() { return stepBuilderFactory.get("step2").<String, String>chunk(3) .reader(new ListItemReader<>(Arrays.asList("1", "2", "3", "4", "5", "6"))) .processor(new ItemProcessor<String, String>() { @Override/* w w w.jav a 2 s. com*/ public String process(String item) throws Exception { return String.valueOf(Integer.parseInt((String) item) * -1); } }).writer(new ItemWriter<String>() { @Override public void write(List<? extends String> items) throws Exception { for (Object item : items) { System.out.println(">> " + item); } } }).build(); }
From source file:com.searchbox.collection.oppfin.TopicCollection.java
private ItemWriter<FieldMap> hashMapWriter() { return new ItemWriter<FieldMap>() { @Override//from w w w .j av a 2 s . co m public void write(List<? extends FieldMap> items) throws Exception { for (FieldMap item : items) { LOGGER.info("Adding call {} to callList", item.get("callFileName").get(0).toString()); // TODO: Find why the call filename is in bracket if we // don't put get(0) i.e: [h2020-msca-itn-2014] callList.put(item.get("callFileName").get(0).toString(), item); } } }; }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java
@Test public void testProcessAllItemsWhenErrorInWriterTransformationWhenReaderTransactional() throws Exception { final int RETRY_LIMIT = 3; final List<String> ITEM_LIST = TransactionAwareProxyFactory .createTransactionalList(Arrays.asList("1", "2", "3")); FaultTolerantStepFactoryBean<String, Integer> factory = new FaultTolerantStepFactoryBean<String, Integer>(); factory.setBeanName("step"); factory.setJobRepository(repository); factory.setTransactionManager(new ResourcelessTransactionManager()); ItemWriter<Integer> failingWriter = new ItemWriter<Integer>() { @Override//from w w w . j a v a 2s . co m public void write(List<? extends Integer> data) throws Exception { int count = 0; for (Integer item : data) { if (count++ == 2) { throw new Exception("Planned failure in writer"); } written.add(item); } } }; ItemProcessor<String, Integer> processor = new ItemProcessor<String, Integer>() { @Override public Integer process(String item) throws Exception { processed.add(item); return Integer.parseInt(item); } }; ItemReader<String> reader = new ListItemReader<String>( TransactionAwareProxyFactory.createTransactionalList(ITEM_LIST)); factory.setCommitInterval(3); factory.setRetryLimit(RETRY_LIMIT); factory.setSkipLimit(1); factory.setIsReaderTransactionalQueue(true); @SuppressWarnings("unchecked") Map<Class<? extends Throwable>, Boolean> exceptionMap = getExceptionMap(Exception.class); factory.setSkippableExceptionClasses(exceptionMap); factory.setRetryableExceptionClasses(exceptionMap); factory.setItemReader(reader); factory.setItemProcessor(processor); factory.setItemWriter(failingWriter); Step step = factory.getObject(); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); repository.add(stepExecution); step.execute(stepExecution); /* * Each chunk tried up to RETRY_LIMIT, then the scan processes each item * once, identifying the skip as it goes */ assertEquals((RETRY_LIMIT + 1) * ITEM_LIST.size(), processed.size()); }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java
@Test public void testProcessAllItemsWhenErrorInWriter() throws Exception { final int RETRY_LIMIT = 3; final List<String> ITEM_LIST = Arrays.asList("a", "b", "c"); ItemWriter<String> failingWriter = new ItemWriter<String>() { @Override//from w ww .j a va2 s. c o m public void write(List<? extends String> data) throws Exception { int count = 0; for (String item : data) { if (count++ == 2) { throw new Exception("Planned failure in writer"); } written.add(item); } } }; ItemProcessor<String, String> processor = new ItemProcessor<String, String>() { @Override public String process(String item) throws Exception { processed.add(item); return item; } }; ItemReader<String> reader = new ListItemReader<String>(ITEM_LIST); factory.setCommitInterval(3); factory.setRetryLimit(RETRY_LIMIT); factory.setSkipLimit(1); @SuppressWarnings("unchecked") Map<Class<? extends Throwable>, Boolean> exceptionMap = getExceptionMap(Exception.class); factory.setSkippableExceptionClasses(exceptionMap); factory.setItemReader(reader); factory.setItemProcessor(processor); factory.setItemWriter(failingWriter); Step step = factory.getObject(); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); repository.add(stepExecution); step.execute(stepExecution); assertEquals(ExitStatus.COMPLETED.getExitCode(), stepExecution.getExitStatus().getExitCode()); /* * Each chunk tried up to RETRY_LIMIT, then the scan processes each item * once, identifying the skip as it goes */ assertEquals((RETRY_LIMIT + 1) * ITEM_LIST.size(), processed.size()); }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java
@Test public void testNoItemsReprocessedWhenErrorInWriterAndProcessorNotTransactional() throws Exception { ItemWriter<String> failingWriter = new ItemWriter<String>() { @Override// w w w .jav a 2 s . c o m public void write(List<? extends String> data) throws Exception { int count = 0; for (String item : data) { if (count++ == 2) { throw new Exception("Planned failure in writer"); } written.add(item); } } }; ItemProcessor<String, String> processor = new ItemProcessor<String, String>() { @Override public String process(String item) throws Exception { processed.add(item); return item; } }; ItemReader<String> reader = new ListItemReader<String>(Arrays.asList("a", "b", "c")); factory.setProcessorTransactional(false); factory.setCommitInterval(3); factory.setRetryLimit(3); factory.setSkippableExceptionClasses(new HashMap<Class<? extends Throwable>, Boolean>()); factory.setItemReader(reader); factory.setItemProcessor(processor); factory.setItemWriter(failingWriter); Step step = factory.getObject(); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); repository.add(stepExecution); step.execute(stepExecution); assertEquals(3, processed.size()); // Initial try only, then cached }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java
@Test public void testRestartAfterFailedWrite() throws Exception { factory.setSkipLimit(0);/*from w w w . j a v a 2s .c o m*/ factory.setCommitInterval(3); AbstractItemCountingItemStreamItemReader<String> reader = new AbstractItemCountingItemStreamItemReader<String>() { private ItemReader<String> reader; @Override protected void doClose() throws Exception { reader = null; } @Override protected void doOpen() throws Exception { reader = new ListItemReader<String>(Arrays.asList("a", "b", "c", "d", "e", "f")); } @Override protected String doRead() throws Exception { return reader.read(); } }; // Need to set name or else reader will fail to open reader.setName("foo"); factory.setItemReader(reader); factory.setStreams(new ItemStream[] { reader }); factory.setItemWriter(new ItemWriter<String>() { @Override public void write(List<? extends String> items) throws Exception { if (fail && items.contains("e")) { throw new RuntimeException("Planned failure"); } processed.addAll(items); } }); factory.setRetryLimit(0); Step step = factory.getObject(); fail = true; StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); repository.add(stepExecution); step.execute(stepExecution); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); assertEquals(4, stepExecution.getWriteCount()); assertEquals(6, stepExecution.getReadCount()); fail = false; ExecutionContext executionContext = stepExecution.getExecutionContext(); stepExecution = new StepExecution(step.getName(), jobExecution); stepExecution.setExecutionContext(executionContext); repository.add(stepExecution); step.execute(stepExecution); assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus()); assertEquals(2, stepExecution.getWriteCount()); assertEquals(2, stepExecution.getReadCount()); }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java
@SuppressWarnings("unchecked") @Test/*from w w w. j a v a2 s . c o m*/ public void testSkipAndRetryWithWriteFailure() throws Exception { factory.setListeners(new StepListener[] { new SkipListenerSupport<String, String>() { @Override public void onSkipInWrite(String item, Throwable t) { recovered.add(item); assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); } } }); factory.setSkipLimit(2); ItemReader<String> provider = new ListItemReader<String>(Arrays.asList("a", "b", "c", "d", "e", "f")) { @Override public String read() { String item = super.read(); logger.debug("Read Called! Item: [" + item + "]"); provided.add(item); count++; return item; } }; ItemWriter<String> itemWriter = new ItemWriter<String>() { @Override public void write(List<? extends String> item) throws Exception { logger.debug("Write Called! Item: [" + item + "]"); processed.addAll(item); written.addAll(item); if (item.contains("b") || item.contains("d")) { throw new RuntimeException("Write error - planned but recoverable."); } } }; factory.setItemReader(provider); factory.setItemWriter(itemWriter); factory.setRetryLimit(5); factory.setRetryableExceptionClasses(getExceptionMap(RuntimeException.class)); AbstractStep step = (AbstractStep) factory.getObject(); step.setName("mytest"); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); repository.add(stepExecution); step.execute(stepExecution); assertEquals(2, recovered.size()); assertEquals(2, stepExecution.getSkipCount()); assertEquals(2, stepExecution.getWriteSkipCount()); List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("a,c,e,f")); assertEquals(expectedOutput, written); assertEquals("[a, b, c, d, e, f, null]", provided.toString()); assertEquals("[a, b, b, b, b, b, b, c, d, d, d, d, d, d, e, f]", processed.toString()); assertEquals("[b, d]", recovered.toString()); }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java
@SuppressWarnings("unchecked") @Test//w ww . ja va 2s.co m public void testSkipAndRetryWithWriteFailureAndNonTrivialCommitInterval() throws Exception { factory.setCommitInterval(3); factory.setListeners(new StepListener[] { new SkipListenerSupport<String, String>() { @Override public void onSkipInWrite(String item, Throwable t) { recovered.add(item); assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); } } }); factory.setSkipLimit(2); ItemReader<String> provider = new ListItemReader<String>(Arrays.asList("a", "b", "c", "d", "e", "f")) { @Override public String read() { String item = super.read(); logger.debug("Read Called! Item: [" + item + "]"); provided.add(item); count++; return item; } }; ItemWriter<String> itemWriter = new ItemWriter<String>() { @Override public void write(List<? extends String> item) throws Exception { logger.debug("Write Called! Item: [" + item + "]"); processed.addAll(item); written.addAll(item); if (item.contains("b") || item.contains("d")) { throw new RuntimeException("Write error - planned but recoverable."); } } }; factory.setItemReader(provider); factory.setItemWriter(itemWriter); factory.setRetryLimit(5); factory.setRetryableExceptionClasses(getExceptionMap(RuntimeException.class)); AbstractStep step = (AbstractStep) factory.getObject(); step.setName("mytest"); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); repository.add(stepExecution); step.execute(stepExecution); assertEquals(2, recovered.size()); assertEquals(2, stepExecution.getSkipCount()); assertEquals(2, stepExecution.getWriteSkipCount()); List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("a,c,e,f")); assertEquals(expectedOutput, written); // [a, b, c, d, e, f, null] assertEquals(7, provided.size()); // [a, b, c, a, b, c, a, b, c, a, b, c, a, b, c, a, b, c, d, e, f, d, // e, f, d, e, f, d, e, f, d, e, f, d, e, f] // System.err.println(processed); assertEquals(36, processed.size()); // [b, d] assertEquals(2, recovered.size()); }