Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation in regards to the return type of reader/writer bean definition methods #1502

Open
spring-issuemaster opened this issue Sep 8, 2013 · 5 comments

Comments

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Sep 8, 2013

Simon Wong commented

If JavaConfig being used (no such issue for using xml config), adding @StepScope to ItemWriter for FlatFileItemWriter will throw the Exception "org.springframework.batch.item.WriterNotOpenException: Writer must be open before it can be written to" during job execution.

    @Bean
    @StepScope
    public ItemWriter<String> writer() {
        FlatFileItemWriter<String> itemWriter = new FlatFileItemWriter<String>();

        itemWriter.setResource(new FileSystemResource("c:/some_output.csv"));
        itemWriter.setLineAggregator(new PassThroughLineAggregator<String>());

        return itemWriter;
    }
@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Sep 10, 2013

Tobias Flohre commented

It will work when the method writer returns the FlatFileItemWriter as type. JavaConfig does not recognize that the writer is an ItemStream as well if the return type is ItemWriter. But anyway, it would be nice if it would work without that workaround.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Sep 10, 2013

Michael Minella commented

Per Tobias Flohre, ItemWriter doesn't have an open method...ItemStream does so there is nothing to open. If you return an ItemStream, or an ItemStreamWriter, it would should fine.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Sep 10, 2013

Tobias Flohre commented

One more comment to make it clearer: when a bean is step-scoped, it does not get instantiated on startup, instead a cglib proxy based on the return type of the @Bean method is created. If the return type is ItemWriter, the proxy does not implement ItemStream and therefore the writer doesn't get registered as one. That's why the open method never gets called.
There's no way around it: the return type has to implement ItemStream.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Sep 10, 2013

Simon Wong commented

Thanks both of you.
It would be nice to add a caveat in the reference document.

@benas benas changed the title FileItemWriter throws WriterNotOpenException exception if JavaConfig and @StepScope is used for writer [BATCH-2097] Improve documentation in regards to the return type of reader/writer bean definition methods Jan 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.