Announcement Announcement Module
Collapse
No announcement yet.
How to access item data (e.g. total # of records) in headerCallBack's write method? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to access item data (e.g. total # of records) in headerCallBack's write method?

    My Job is as below:

    Code:
    <job id="helloWorldJob">
        <step id="step1">
            <tasklet>
                <chunk reader="empReader" processor="empProcessor" writer="empHeaderFooterWriter" commit-interval="1000"/>
            </tasklet>
        </step>
    </job>
    My writer is as below:
    Code:
    <beans:bean id="empWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
        <beans:property name="resource" value="file:target/output_data.txt" />
        <beans:property name="lineAggregator">
            <beans:bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
                <beans:property name="delimiter" value=","/>
                <beans:property name="fieldExtractor">
                    <beans:bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                        <beans:property name="names" value="empId,lastName,title,salary,rank"/>
                    </beans:bean>
                </beans:property>
            </beans:bean>
        </beans:property>
        <beans:property name="footerCallback" ref="empHeaderFooterWriter" />
        <beans:property name="headerCallback" ref="empHeaderFooterWriter" />
    </beans:bean>
    
    <beans:bean id="empHeaderFooterWriter" class="com.test.EmployeeItemWriter">
        <beans:property name="delegate" ref="empWriter"/>
    </beans:bean>
    Now what I want to do is, I want to put the total number of records in the file in the Header (and not in footer).

    How can I do that ??

    Thanks,
    Nik
    Last edited by nikunj_c; Oct 19th, 2011, 07:43 AM. Reason: wrong writer name in the job

  • #2
    You could try and change the header of the file in an after step with Replace first line of a text file in Java with RandomAccessFile

    Comment


    • #3
      Thanks a lot Michael!! that helped!!

      Comment


      • #4
        Hi Michel

        how to set the item data(e.g total # of records from db) in headercallback's write method..

        Here config file is as below
        Code:
        <bean id="itemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
        			<property name="resource" ref="outputResource" />
        	      	<property name="marshaller" ref="LedgerMarshaller" />    
               		<property name="rootTagName" value="BatchJob" />
               		 <property name="headerCallback" ref="headerCallback" />     		
               		     
         </bean>
        and classes for as below

        Code:
        public class ProductHeaderStaxCallback implements StaxWriterCallback {
        
            @Override
            public void write(XMLEventWriter writer) throws IOException {
                try {
                	
                	
                	
                	
                    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        
                    XMLEvent event = eventFactory.createStartElement("", "", "source");
                    writer.add(event);
                   // event = eventFactory.createAttribute("generated", DateFormat.getDateTimeInstance().format(new Date()));
                   // writer.add(event);
                    Characters studentNameChars = eventFactory.createCharacters("scan");
                    writer.add(studentNameChars); 
                    event = eventFactory.createEndElement("", "", "source");
                    writer.add(event);
                    XMLEvent event1 = eventFactory.createStartElement("", "", "Datetime");
                    writer.add(event1);
                    //event1 = eventFactory.createAttribute("generated", DateFormat.getDateTimeInstance().format(new Date()));
                  //  writer.add(event1);
                    Characters studentNameChars1 = eventFactory.createCharacters(DateFormat.getDateTimeInstance().format(new Date()));
                    writer.add(studentNameChars1); 
                    event1 = eventFactory.createEndElement("", "", "Datetime");
                    writer.add(event1);
                    Ledger ledger = Ledger.getInstance();
                	ledger.getRowcount();        	System.out.println("RowCount!!!"+ledger.getRowcount());
                } catch (XMLStreamException e) {
                }
        
            }

        I have tried with above, getting null value as rowcount .

        Comment


        • #5
          i do not understand the code :-)

          Code:
          Ledger ledger = Ledger.getInstance();
          ledger.getRowcount();
          what happens inside Ledget.getInstance ?

          anyways

          Code:
          how to set the item data(e.g total # of records from db) in headercallback's write method..
          from above

          You could try and change the header of the file in an after step with Replace first line of a text file in Java with RandomAccessFile

          Comment

          Working...
          X