Search in sources :

Example 1 with StaxEventItemWriter

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;
}
Also used : StaxEventItemWriter(org.springframework.batch.item.xml.StaxEventItemWriter) Transaction(org.baeldung.batch.model.Transaction) FileSystemResource(org.springframework.core.io.FileSystemResource) StepScope(org.springframework.batch.core.configuration.annotation.StepScope) JobRepositoryFactoryBean(org.springframework.batch.core.repository.support.JobRepositoryFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with StaxEventItemWriter

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());
}
Also used : StaxEventItemWriter(org.springframework.batch.item.xml.StaxEventItemWriter) ItemStreamException(org.springframework.batch.item.ItemStreamException) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Marshaller(org.springframework.oxm.Marshaller) ExecutionContext(org.springframework.batch.item.ExecutionContext) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) Before(org.junit.Before) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) File(java.io.File) List(java.util.List) TransactionAwareBufferedWriter(org.springframework.batch.support.transaction.TransactionAwareBufferedWriter) Assert.assertFalse(org.junit.Assert.assertFalse) XmlRootElement(jakarta.xml.bind.annotation.XmlRootElement) Collections(java.util.Collections) XMLEventFactory(javax.xml.stream.XMLEventFactory) Assert.assertEquals(org.junit.Assert.assertEquals) ExecutionContext(org.springframework.batch.item.ExecutionContext) XMLStreamException(javax.xml.stream.XMLStreamException) HashMap(java.util.HashMap) XMLEventFactory(javax.xml.stream.XMLEventFactory) Test(org.junit.Test)

Aggregations

StaxEventItemWriter (org.springframework.batch.item.xml.StaxEventItemWriter)2 FileSystemResource (org.springframework.core.io.FileSystemResource)2 XmlRootElement (jakarta.xml.bind.annotation.XmlRootElement)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 XMLEventFactory (javax.xml.stream.XMLEventFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 FileUtils (org.apache.commons.io.FileUtils)1 Transaction (org.baeldung.batch.model.Transaction)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Before (org.junit.Before)1 Test (org.junit.Test)1 StepScope (org.springframework.batch.core.configuration.annotation.StepScope)1