0
votes

I am trying to add a header and footer in a custom writer by implementing the header callback and footercallback in my custom writer call .

Write method is successful . But write header and writefooter are not called .

public class CustomOAFileItemWriter extends StepExecutionListenerSupport implements  ItemWriter<OAExtract>,FlatFileHeaderCallback,FlatFileFooterCallback{


public void write(List<? extends OAExtract> oaExtractList) throws Exception {
        FileOutputStream fs = new FileOutputStream("C:\\archivedFiles\\out.bin");

}

public void writeHeader(Writer writer) throws IOException {

        System.out.println("Writing Header record");

}

public void writeFooter(Writer writer) throws IOException {

        System.out.println("Writing Footerrecord");
}

Can someone with Spring batch experience help me with this?

Thanks, Rai

2
Check the code for FlatFileItemWriter and look when these callbacks are called. I guess, they are called from the open and close method (ItemStream interface). So, this is a feature of the FlatFileItemWriter class itself and not a general Framework feature. This means, you have to implement yourself inside your own writer. - Hansjoerg Wingeier
I tried to modify the implementation of FlatFileItemWriter to write bytes . But the outputBufferWriter only writes String, int and char[] . I need to write a byte{} . Can you help with this? public void write(byte[] bs) throws IOException { if (!initialized) { initializeBufferedWriter(); } outputBufferedWriter.write(bs); outputBufferedWriter.flush(); } - R Rai
It is actually a problem with the spring TransactionAwareBufferedWriter . This does not contain a method that can write bytes. - R Rai
Are you sure that you don't have just an encoding problem? If you are sure that you really have to write bytes directly, then write your own writer. First, make your own TransactionAwareBufferedWriter, which supports writing bytes and has a bindResource(bufferKey) that is not just a StringBuffer but something that can handle bytes. Use this Writer in your own copy of FlatFileItemWriter. These are only two classes which have to be copied and adapted. - Hansjoerg Wingeier

2 Answers

0
votes

You solution is opposite to SB philosophy: reuse and delegation. And you are using none of them.
You don't need a custom ItemWriter but

  1. Create a FlatFileItemWriter with your custom header/footer callback
  2. Create the listener you want (I see you extend StepExecutionListenerSupport) and attach to your step.
0
votes

If you look at the source code for FlatFileItemWriter you will see that it calls the header callback method at doOpen() and footer callback method at doClose(). Since your not making use of the standard FlatFileItemWriter you will have write explicit code just like that in the FlatFileItemWriter.

http://grepcode.com/file/repo1.maven.org/maven2/org.springframework.batch/spring-batch-infrastructure/3.0.1.RELEASE/org/springframework/batch/item/file/FlatFileItemWriter.java#FlatFileItemWriter