0

I am extending StaxEventItemWriter to write a custom ItemWriter so that I can create the file name dynamically at runtime. The filename is getting created fine however I am getting below issue when its trying to write the data.

org.springframework.batch.core.step.tasklet.UncheckedTransactionException: java.lang.reflect.InvocationTargetException
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:486)
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
    at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:272)
    at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81)
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374)
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
    at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
    at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
    at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
    at com.santanderuk.carde.gassaudittransform.GassAuditTransformApplication.main(GassAuditTransformApplication.java:24)
Caused by: java.lang.reflect.InvocationTargetException: null
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.batch.item.xml.StaxUtils.getResult(StaxUtils.java:116)
    at org.springframework.batch.item.xml.StaxEventItemWriter.createStaxResult(StaxEventItemWriter.java:532)
    at org.springframework.batch.item.xml.StaxEventItemWriter.write(StaxEventItemWriter.java:739)
    at com.santanderuk.carde.gassaudittransform.writer.IncomeExpenditureStaxWriter.write(IncomeExpenditureStaxWriter.java:67)
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.writeItems(SimpleChunkProcessor.java:175)
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.doWrite(SimpleChunkProcessor.java:151)
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.write(SimpleChunkProcessor.java:274)
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:199)
    at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75)
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
    ... 20 common frames omitted
Caused by: java.lang.IllegalArgumentException: StAXResult(XMLEventWriter) with XMLEventWriter == null
    at javax.xml.transform.stax.StAXResult.<init>(StAXResult.java:90)
    at org.springframework.util.xml.StaxUtils.createStaxResult(StaxUtils.java:145)
    ... 34 common frames omitted

Added below code but its not working.

@Override
public void write(List<? extends Employee> items) throws Exception {
StaxEventItemWriter<AuditRecordsData> xmlFileWriter = new StaxEventItemWriter<>();
    xmlFileWriter.setResource(new FileSystemResource(filepath));
xmlFileWriter.setRootTagName("employee");
   Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
  Result res = createStaxResult();
  jaxb2Marshaller.setClassesToBeBound(Employee.class);
    jaxb2Marshaller.marshal(data, res);
    xmlFileWriter.setMarshaller(jaxb2Marshaller);
    xmlFileWriter.write(items);
alex252
  • 1
  • 1
  • Remove `axb2Marshaller.marshal` and with that the `Result res = createStaxResult()`. Why on earth is it even there. – M. Deinum Nov 07 '19 at 12:14
  • @M.Deinum.. so i dont have to set the Marshaller for writer?? – alex252 Nov 07 '19 at 12:22
  • Yes you have, but those 2 lines make no sense. I also not get why you seem to be enclosing a writer in a writer? What is that write method? – M. Deinum Nov 07 '19 at 12:23
  • if i remove xmlFileWriter.write(items);, the process gets completed and the filename is file:output/xml/Employee_20191107 but i cannot find this xml file anywhere. – alex252 Nov 07 '19 at 12:32
  • Your code is weird, you seem to wrap a writer in a writer. Leaving out the write makes it a noop and nothing happens. I don't see why you are wrapping this. – M. Deinum Nov 07 '19 at 12:36
  • @M.Deinum.. I am new to XML parsing. Do i really to create a new StaxEventItemWriter insatnce in the write method ?? I just need to inform the writer about the dynamic file name. – alex252 Nov 07 '19 at 12:38
  • Again, why on earth are you even having a write method in which you are creating a `StaxEventItemWriter`. Again your code doesn't really make sense. What you should do, is make your `StaxEventWriter` (which you for some reason choose to extend, stepscoped or job scoped and set the filename in the `@Bean` method that creates that instance. You are making things overly complex. – M. Deinum Nov 07 '19 at 12:39
  • ok will try that..i am overcomplicating things i feel... – alex252 Nov 07 '19 at 12:48
  • @M.Deinum.. i was thinking that i can add jobParameters and set the value there as only date value that is dynamic here. – alex252 Nov 07 '19 at 12:58
  • adding jobParameters resolved the issue – alex252 Nov 12 '19 at 11:26

0 Answers0