use of org.springframework.batch.item.xml.StaxEventItemWriter in project tutorials by eugenp.
the class SpringbatchPartitionConfig method itemWriter.
@Bean(destroyMethod = "")
@StepScope
public StaxEventItemWriter<Transaction> itemWriter(Marshaller marshaller, @Value("#{stepExecutionContext[opFileName]}") String filename) throws MalformedURLException {
StaxEventItemWriter<Transaction> itemWriter = new StaxEventItemWriter<>();
itemWriter.setMarshaller(marshaller);
itemWriter.setRootTagName("transactionRecord");
itemWriter.setResource(new FileSystemResource("src/main/resources/output/" + filename));
return itemWriter;
}
use of org.springframework.batch.item.xml.StaxEventItemWriter in project spring-batch by spring-projects.
the class StaxEventItemWriterBuilderTests method testConfiguration.
@Test
public void testConfiguration() throws Exception {
Map<String, String> rootElementAttributes = new HashMap<>();
rootElementAttributes.put("baz", "quix");
StaxEventItemWriter<Foo> staxEventItemWriter = new StaxEventItemWriterBuilder<Foo>().name("fooWriter").marshaller(marshaller).encoding("UTF-16").footerCallback(writer -> {
XMLEventFactory factory = XMLEventFactory.newInstance();
try {
writer.add(factory.createEndElement("ns", "https://www.springframework.org/test", "group"));
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}).headerCallback(writer -> {
XMLEventFactory factory = XMLEventFactory.newInstance();
try {
writer.add(factory.createStartElement("ns", "https://www.springframework.org/test", "group"));
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}).resource(this.resource).rootTagName("foobarred").rootElementAttributes(rootElementAttributes).saveState(false).version("1.1").build();
staxEventItemWriter.afterPropertiesSet();
ExecutionContext executionContext = new ExecutionContext();
staxEventItemWriter.open(executionContext);
staxEventItemWriter.write(this.items);
staxEventItemWriter.update(executionContext);
staxEventItemWriter.close();
assertEquals(FULL_OUTPUT, getOutputFileContent("UTF-16"));
assertEquals(0, executionContext.size());
}
Aggregations