0 Replies Latest reply on Apr 26, 2017 1:31 PM by Cheng Fang

    Introduce ArrayItemReader

    Cheng Fang Master

      I recently added a new reader, arrayItemReader, to jberet-support module.  The primary purpose of arrayItemReader is for test apps or prototypes to quickly set up a batch item reader to read array of scalar data.  For example, the following snippet is for reading array of integer numbers:

       

      <reader ref="arrayItemReader">
        <properties>
         <!-- read inlined Integer array -->
         <property name="resource" value="[1, 2, 3]"/>
        <property name="beanType" value="java.lang.Integer"/>
        </properties>
      </reader>

       

      In the above job.xml, the input resource is an inlined array of integer numbers.  For the same number array above, beanType property can also be configured as other numeric types (java.lang.Long, java.lang.Byte, java.lang.Double, etc).

       

       

      To read a string array:

      <reader ref="arrayItemReader">
        <properties>
         <!-- read inlined string array -->
         <property name="resource" value='["a", "b", "c"]'/>
        <property name="beanType" value="java.lang.String"/>
        </properties>
      </reader>

       

      Since string is the default beanType for arrayItemReader, the beanType property can be omitted in the above example.

       

       

      In addition to string and java primitive wrapper types, arrayItemReader can also read custom POJO types.  For instance,

      <reader ref="arrayItemReader">
        <properties>
         <!-- read inlined Movie data -->
         <property name="beanType" value="org.jberet.support.io.Movie"/>
         <property name="resource" value='[
        {"rank" : 1, "tit" : "Number One", "grs" : 1000, "opn" : "2017-01-01"},
        {"rank" : 2, "tit" : "Number Two", "grs" : 2000, "opn" : "2017-02-02"},
        {"rank" : 3, "tit" : "Number Three", "grs" : 3000, "opn" : "2017-03-03"},
        {"rank" : 4, "tit" : "Number Four", "grs" : 4000, "opn" : "2017-04-04"},
        {"rank" : 5, "tit" : "Number Five", "grs" : 5000, "opn" : "2017-05-05"}
        ]'/>
        </properties>
      </reader>

       

       

      The input resource content doesn't have to be inlined;  it's just convenient, especially for tests, to be able to keep data inside job.xml.  It works as well to keep input data in external file or URL.  The resource content should be a valid JSON array.  For instance,

      <reader ref="arrayItemReader">
        <properties>
        <property name="beanType" value="org.jberet.support.io.Movie"/>
        <property name="resource" value='movies-2012.json'/>
        </properties>
      </reader>

       

       

      For more details, see

      1. JBERET-321

      Implement ItemReader that reads from array or list of inlined data