org.springframework.batch.item.ItemStreamException:文件不可写:Spring Batch [英] org.springframework.batch.item.ItemStreamException: File is not writable: Spring Batch

查看:36
本文介绍了org.springframework.batch.item.ItemStreamException:文件不可写:Spring Batch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Boot Batch 项目中,我收到以下错误,不知道这里出了什么问题?

In Spring Boot Batch project, I am getting the below error, not sure what is wrong going on here ?

我只是从 mysql 数据库中读取表并使用 FlatFileItemWriter 将其写入文件.还使用 Partioner 读取代码并将其写入平面文件.Spring Boot Parent 版本 2.0.2.RELEASE.

I am simply reading table from the mysql DB and writing it to the file using the FlatFileItemWriter. Also using the Partiontioner to read the code and write it to the Flat Files. Spring Boot Parent version 2.0.2.RELEASE.

org.springframework.batch.item.ItemStreamException: File is not writable: [C:\Spring_Learning\spring-batch\spring-batch-classic-db\csv\outputs\users.processed201-250.csv]
    at org.springframework.batch.item.util.FileUtils.setUpOutputFile(FileUtils.java:88) ~[spring-batch-infrastructure-4.0.1.RELEASE.jar:4.0.1.RELEASE]
    at org.springframework.batch.item.file.FlatFileItemWriter$OutputState.initializeBufferedWriter(FlatFileItemWriter.java:572) ~[spring-batch-infrastructure-4.0.1.RELEASE.jar:4.0.1.RELEASE]
    at org.springframework.batch.item.file.FlatFileItemWriter$OutputState.access$000(FlatFileItemWriter.java:414) ~[spring-batch-infrastructure-4.0.1.RELEASE.jar:4.0.1.RELEASE]
    at org.springframework.batch.item.file.FlatFileItemWriter.doOpen(FlatFileItemWriter.java:348) ~[spring-batch-infrastructure-4.0.1.RELEASE.jar:4.0.1.RELEASE]
    at org.springframework.batch.item.file.FlatFileItemWriter.open(FlatFileItemWriter.java:338) ~[spring-batch-infrastructure-4.0.1.RELEASE.jar:4.0.1.RELEASE]
    at org.springframework.batch.item.file.FlatFileItemWriter$$FastClassBySpringCGLIB$$bd919dcd.invoke(<generated>) ~[spring-batch-infrastructure-4.0.1.RELEASE.jar:4.0.1.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) ~[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:136) ~[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:124) ~[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.batch.item.file.FlatFileItemWriter$$EnhancerBySpringCGLIB$$d3ba3d4f.open(<generated>) ~[spring-batch-infrastructure-4.0.1.RELEASE.jar:4.0.1.RELEASE]
    at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:103) ~[spring-batch-infrastructure-4.0.1.RELEASE.jar:4.0.1.RELEASE]
    at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:310) ~[spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE]
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:197) ~[spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE]
    at org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler$1.call(TaskExecutorPartitionHandler.java:139) [spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE]
    at org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler$1.call(TaskExecutorPartitionHandler.java:136) [spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE]
    at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_171]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_171]

代码

@Bean
    @StepScope
    public FlatFileItemWriter<Payments> slaveWriter(@Value("#{stepExecutionContext[fromId]}") final String fromId,
            @Value("#{stepExecutionContext[toId]}") final String toId) {

        FlatFileItemWriter<Payments> reader = new FlatFileItemWriter<>();
        reader.setResource(new FileSystemResource("csv/outputs/users.processed" + fromId + "-" + toId + ".csv"));
        reader.setAppendAllowed(true);

        reader.setLineAggregator(new DelimitedLineAggregator<Payments>() {
            {
                setDelimiter(",");
                setFieldExtractor(new BeanWrapperFieldExtractor<Payments>() {
                    {
                        setNames(new String[] { "customerNumber", "checkNumber", "paymentDate", "amount"});
                    }
                });
            }
        });
        return reader;
    }

推荐答案

尝试更新您的代码:

FlatFileItemWriter<Payments> writer = new FlatFileItemWriter<>();
        writer.setResource(new FileSystemResource("csv/outputs/users.processed" + fromId + "-" + toId + ".csv"));
        writer.setAppendAllowed(true);

致:

FlatFileItemWriter<Payments> writer = new FlatFileItemWriter<>();
        writer.setResource(new FileSystemResource("csv/outputs/users.processed" + fromId + "-" + toId + ".csv"));
        writer.setAppendAllowed(true);
        writer.setShouldDeleteIfEmpty(true);
        writer.setShouldDeleteIfExists(true);

我遇到了同样的错误,它对我有用.

I had the same error, it works for me.

具体是因为FileUtils.setUpOutputFile(File file, boolean restarted, boolean append, boolean overwriteOutputFile)restarted参数可以设置为true 当作业重新启动时.文件是从旧上下文中恢复的,并且不可写...我不知道为什么.

For the details, it was because of the restartedargument of FileUtils.setUpOutputFile(File file, boolean restarted, boolean append, boolean overwriteOutputFile) which can be set to true when the job is restarted. A file is recovered from an old context and it's not writable... I don't know why precisely.

这篇关于org.springframework.batch.item.ItemStreamException:文件不可写:Spring Batch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆