Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Overriding write(List<? extends T>) method of StaxEventItemWriter

  1. #11

    Default

    Hi everybody,

    passing data through steps is OK.

    To avoid writing too many data in the execution context, I decided to use an holder bean.

    The holder bean

    Code:
    public class AuthorListHolder {
    	
    	private List<Author> authorList;
    
    	public List<Author> getAuthorList() {
    		
    		return authorList;
    	}
    
    	public void setAuthorList(List<Author> authorList) {
    		
    		this.authorList = authorList;
    	}
    }
    Config snippets

    Code:
    <bean id="authorListHolder" class="..."/>
    
    <bean id="savingAuthorListItemWriter" class="...">
    	<property name="authorListHolder" ref="authorListHolder"/>
    </bean>
    
    <bean id="retrievingAuthorItemReader" class="...">
    	<property name="authorListHolder" ref="authorListHolder"/>
    </bean>
    The write method of the writer

    Code:
    @Override
    public void write(List<? extends List<Author>> items)throws Exception {
    	
    	for(List<Author> authorList : items) {
    		
    		// my SavingAuthorListItemWriter.setAuthorListHolder contains: this.authorListHolder.setAuthorList(new ArrayList<Author>());
    		authorListHolder.getGroupList().addAll(authorList);
    	}
    }
    The read method of the reader

    Code:
    @Override
    public Author read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
    	
    	if(!authorListHolder.getAuthorList().isEmpty()) {
    		
    		return authorListHolder.getAuthorList().remove(0);
    	}
    	
    	return null;
    }
    I think the holder bean solution is ok, because it allowed me to restore DB job repository and to have a cleaner code.

    Thank you for your patience!

  2. #12
    Join Date
    Aug 2010
    Location
    Paris, France
    Posts
    13

    Thumbs up

    Nice !
    It looks a lot cleaner than passing things to the jobContext map !
    Thanks for the tip ;-)

  3. #13
    Join Date
    Nov 2011
    Posts
    1

    Default

    So, following this, I took a concrete example with a simple object called "Author", having 2 properties (first and last name).
    I define an XSD schema for it, assuming you already have one for your object, but this is just for the demo :
    HTML Code:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema targetNamespace="http://com.authors/batch"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://com.authors/batch"
    elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xsd:element name="Authors">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element ref="Author" maxOccurs="unbounded"/>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    <xsd:element name="Author">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="FirstName" type="xsd:string" />
    <xsd:element name="LastName" type="xsd:string" />
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>

    </xsd:schema>

    ________________
    virtual assistant

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •