List of usage examples for org.springframework.batch.item ExecutionContext getLong
public long getLong(String key)
From source file:batch.demo.job.MultiThreadedFlatFileItemReader.java
@Override public void open(ExecutionContext executionContext) throws ItemStreamException { if (isSaveState()) { Assert.notNull(executionContext, "ExecutionContext must not be null"); if (executionContext.containsKey(getExecutionContextUserSupport().getKey(START_AT_KEY))) { startAt = executionContext.getLong(getExecutionContextUserSupport().getKey(START_AT_KEY)); }/*from w w w.ja va 2 s.com*/ } // replace the DefaultBufferedReaderFactory with an implementation that seeks to the start before reading setBufferedReaderFactory(new BufferedFileReaderFactory(this.startAt)); super.open(executionContext); }
From source file:org.opensourcebank.batch.reader.HazelcastMapItemReader.java
public void open(ExecutionContext executionContext) throws ItemStreamException { // staring Hazelcast to have this node join the cluster // and getting a reference to an items map itemMap = Hazelcast.getMap(mapName); // possibly restarted, but all items were completed / evicted by another node // or just a wrong map name if (itemMap.size() == 0) { logger.warn("Map [ " + mapName + " ] is empty, no items to read"); }//from w w w . j av a 2 s .c om // is this step restarted? if (executionContext.containsKey(CURRENT_ITEM_ID)) { currentItemId = new Long(executionContext.getLong(CURRENT_ITEM_ID)).intValue(); } else { currentItemId = fromId; } }
From source file:org.beanio.spring.SpringTest.java
/** * Test BeanIO flat file writer for XML. *///from www.j av a2 s.c o m @Test @SuppressWarnings("unchecked") public void testRestarbleXmlItemWriter() throws Exception { ExecutionContext ec = new ExecutionContext(); File tempFile = File.createTempFile("beanio-", "xml"); tempFile.deleteOnExit(); BeanIOFlatFileItemWriter<Human> writer = (BeanIOFlatFileItemWriter<Human>) context .getBean("itemWriter-xml"); writer.setResource(new FileSystemResource(tempFile)); writer.open(ec); List<Human> list = new ArrayList<Human>(); list.add(new Human(Human.FRIEND, "John", 'M')); writer.write(list); writer.update(ec); long position = ec.getLong("BeanIOFlatFileItemWriter.current.count"); assertTrue(position > 0); list.clear(); list.add(new Human(Human.COWORKER, "Mike", 'M')); list.add(new Human(Human.NEIGHBOR, "Steve", 'M')); writer.write(list); writer.close(); assertFileMatches("xout1.xml", tempFile); // open for restart writer = (BeanIOFlatFileItemWriter<Human>) context.getBean("itemWriter-xml"); writer.setResource(new FileSystemResource(tempFile)); writer.open(ec); list.clear(); list.add(new Human(Human.FRIEND, "Jen", 'F')); writer.write(list); writer.update(ec); writer.close(); assertFileMatches("xout2.xml", tempFile); }
From source file:org.beanio.spring.SpringTest.java
/** * Test BeanIO flat file writer./*from w ww.j a v a 2 s.c o m*/ */ @Test @SuppressWarnings("unchecked") public void testItemWriter() throws Exception { ExecutionContext ec = new ExecutionContext(); File tempFile = File.createTempFile("beanio-", "xml"); tempFile.deleteOnExit(); BeanIOFlatFileItemWriter<Map<String, Object>> writer = (BeanIOFlatFileItemWriter<Map<String, Object>>) context .getBean("itemWriter-standalone"); writer.setResource(new FileSystemResource(tempFile)); assertNotNull(writer); writer.open(ec); Map<String, Object> record = new HashMap<String, Object>(); record.put("id", 1); record.put("name", "John"); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); list.add(record); writer.write(list); writer.update(ec); long position = ec.getLong("BeanIOFlatFileItemWriter.current.count"); assertTrue(position > 0); writer.close(); assertFileMatches("out1.txt", tempFile); // test appendAllowed = true, and saveState = false writer = (BeanIOFlatFileItemWriter<Map<String, Object>>) context.getBean("itemWriter-append"); writer.setResource(new FileSystemResource(tempFile)); assertNotNull(writer); writer.open(ec); record.put("id", 2); record.put("name", "Joe"); writer.write(list); writer.update(ec); assertEquals(position, ec.getLong("BeanIOFlatFileItemWriter.current.count")); writer.close(); assertFileMatches("out2.txt", tempFile); // test restart writer = (BeanIOFlatFileItemWriter<Map<String, Object>>) context.getBean("itemWriter-standalone"); writer.setResource(new FileSystemResource(tempFile)); assertNotNull(writer); writer.open(ec); record.put("id", 3); record.put("name", "Kevin"); writer.write(list); writer.update(ec); assertTrue(ec.getLong("BeanIOFlatFileItemWriter.current.count") > position); writer.close(); assertFileMatches("out3.txt", tempFile); }
From source file:org.emonocot.job.io.StaxEventItemWriter.java
/** * Open the output source./*ww w. j a v a2s .c om*/ * @param newExecutionContext Set the execution context * @see org.springframework.batch.item.ItemStream#open(ExecutionContext) */ public final void open(final ExecutionContext newExecutionContext) { Assert.notNull(resource, "The resource must be set"); long startAtPosition = 0; boolean restarted = false; // if restart data is provided, restart from provided offset // otherwise start from beginning if (newExecutionContext.containsKey(getKey(RESTART_DATA_NAME))) { startAtPosition = newExecutionContext.getLong(getKey(RESTART_DATA_NAME)); restarted = true; } open(startAtPosition, restarted); if (startAtPosition == 0) { try { if (headerCallback != null) { headerCallback.write(delegateEventWriter); } } catch (IOException e) { throw new ItemStreamException("Failed to write headerItems", e); } } }
From source file:org.springframework.batch.item.xml.StaxEventItemWriter.java
/** * Open the output source//from w w w.ja va2 s. com * * @see org.springframework.batch.item.ItemStream#open(ExecutionContext) */ @SuppressWarnings("unchecked") @Override public void open(ExecutionContext executionContext) { super.open(executionContext); Assert.notNull(resource, "The resource must be set"); long startAtPosition = 0; // if restart data is provided, restart from provided offset // otherwise start from beginning if (executionContext.containsKey(getExecutionContextKey(RESTART_DATA_NAME))) { startAtPosition = executionContext.getLong(getExecutionContextKey(RESTART_DATA_NAME)); currentRecordCount = executionContext.getLong(getExecutionContextKey(WRITE_STATISTICS_NAME)); if (executionContext.containsKey(getExecutionContextKey(UNCLOSED_HEADER_CALLBACK_ELEMENTS_NAME))) { unclosedHeaderCallbackElements = (List<QName>) executionContext .get(getExecutionContextKey(UNCLOSED_HEADER_CALLBACK_ELEMENTS_NAME)); } restarted = true; if (shouldDeleteIfEmpty && currentRecordCount == 0) { // previous execution deleted the output file because no items were written restarted = false; startAtPosition = 0; } else { restarted = true; } } else { currentRecordCount = 0; restarted = false; } open(startAtPosition); if (startAtPosition == 0) { try { if (headerCallback != null) { UnclosedElementCollectingEventWriter headerCallbackWriter = new UnclosedElementCollectingEventWriter( delegateEventWriter); headerCallback.write(headerCallbackWriter); unclosedHeaderCallbackElements = headerCallbackWriter.getUnclosedElements(); } } catch (IOException e) { throw new ItemStreamException("Failed to write headerItems", e); } } this.initialized = true; }