StaxEventItemWriter错误 - org.springframework.batch.item.ItemStreamException:文件已存在:

时间:2016-11-03 01:57:23

标签: spring-boot spring-batch

我试图创建一个springboot批处理作业,它必须从队列中读取数据,然后写入xml格式。我正在使用StaxEventItemWriter进行编写,在运行应用程序时,我遇到了以下错误。我坚信错误发生在itemwriter部分,因为使用相同的阅读器,我可以使用Filefileitemwriter写入平面文件。我无法找出我做错了什么。我非常感谢你的帮助。

  

[org.springframework.batch.core.step.AbstractStep.execute]遇到   在job importUserJob中执行步骤step1时出错   org.springframework.batch.item.ItemStreamException:文件已经存在   存在:[C:\ folder \ Sync_0.xml] at   org.springframework.batch.item.util.FileUtils.setUpOutputFile(FileUtils.java:61)     在   org.springframework.batch.item.xml.StaxEventItemWriter.open(StaxEventItemWriter.java:428)     在   org.springframework.batch.item.xml.StaxEventItemWriter.open(StaxEventItemWriter.java:400)     在   org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96)     在   org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:310)     在   org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:197)     在   org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)     在   org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)     在   org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)     在   org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)     在   org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)     在   org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)     在   org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306)     在   org.springframework.batch.core.launch.support.SimpleJobLauncher $ 1.run(SimpleJobLauncher.java:135)     在   org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)     在   org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)     在   org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration $ PassthruAdvice.invoke(SimpleBatchConfiguration.java:127)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)     在   org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)     在com.sun.proxy。$ Proxy40.run(未知来源)at   org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:216)     在   org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:233)     在   org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:125)     在   org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:119)     在   org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:806)     在   org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:790)     在   org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:777)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:308)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)

下面提供了项目撰写人的代码。

@Bean
public ItemWriter<WorkerXML> staxEventItemWriter() throws XmlMappingException, Exception{
  StaxEventItemWriter<WorkerXML> staxEventItemWriter = new StaxEventItemWriter<WorkerXML>();

  try{

    XStreamMarshaller marshaller = new XStreamMarshaller(); 

    staxEventItemWriter.setName("StaxEventItemWriter");
          staxEventItemWriter.setResource("C://folder//Sync_0.xml"));

    staxEventItemWriter.setRootTagName("WorkerXML");
    staxEventItemWriter.setOverwriteOutput(false);
    staxEventItemWriter.setMarshaller(marshaller);
    staxEventItemWriter.setEncoding(StandardCharsets.UTF_8.toString());

    staxEventItemWriter.setVersion("XML");//    

    staxEventItemWriter.setSaveState(true);        //                             staxEventItemWriter.open(new ExecutionContext());

    ExecutionContext executionContext = new ExecutionContext();
    staxEventItemWriter.open(executionContext);

    }
    catch(Exception e){
        System.out.println("staxEventItemWriter " + e);
    }
    return staxEventItemWriter;
  }

  @Bean 
  public Marshaller marshaller() 
  { 
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(QVCWorkerXML.class);
    return marshaller;
  }

0 个答案:

没有答案