Results 1 to 10 of 12

Thread: Set itemReaders dynamically?

Hybrid View

  1. #1
    Join Date
    May 2010
    Posts
    6

    Default Set itemReaders dynamically?

    Is it possible to set itemReaders dynamically? Something like below is what I am looking to do.

    <job id="myJob" restartable="true">
    <step id="step1" >
    <tasklet task-executor="taskExecutor">
    <chunk reader="#{jobParameters[customReader]}"
    processor="#{jobParameters[customProcessor]}"
    writer="#{jobParameters[customWriter]}"
    commit-interval="1000"/>
    <listeners>
    <listener ref=" myJobStepListener" />
    </listeners>
    </tasklet>
    </step>
    <listeners>
    <listener ref="myJobListener"/>
    </listeners>
    </job>

    As shown above, I would like to ‘wire-in’ readers, processors and writers for a step, by passing spring bean references as job parameters. We require this (or we think we require this!) as we deal with different types of file formats (multiline, nested and other custom formats over which we have no control) received on different (Mule) file inbound endpoints, which are dynamically setup with configuration details coming from a database. We will need to keep adding these custom readers on an on-going basis, and we are essentially trying to minimize the overall code changes.

    If the above is not possible, what other alternatives are there? I came close to an alternative, where a reader is ‘wired-in’ with one level of abstraction (or via a wrapper), but it gave me a ‘ReaderNotOpenException’. I didn’t want to go down that path where I will have to explicitly code open, close etc. on the reader.

    Any help is appreciated. Thank you.

  2. #2
    Join Date
    May 2010
    Posts
    6

    Default

    Never mind, I figured out a way to resolve this via spring factory-bean/factory-method. Thanks.

  3. #3
    Join Date
    Jun 2010
    Posts
    23

    Question

    hey hi
    just can you guide me how did you achieve it ,
    can you paste a sample code !!
    i am very much in need of that functionality .

  4. #4
    Join Date
    May 2010
    Posts
    6

    Default

    In our implementation, Job is described as:

    Code:
    <job id="myJob" restartable="true">
    	<step id="step1">
    		<tasklet task-executor="taskExecutor">
    			<chunk	reader="readerFactory" 
    				processor="processorFactory"
    				writer="writerFactory"
    				commit-interval="1000"/>
    			<listeners>
    				<listener ref="stepListener" />
    			</listeners>
    		</tasklet>
    	</step>
    	<listeners>
    		<listener ref="jobListener"/>
    	</listeners>
    </job>
    and the 'readerFactory' above is described as:

    Code:
    <bean id="ReaderFactory" class="com.mycompany.batch.ReaderFactory" scope="step">
    	<!--	
    		pass in parameters required to construct and initialize the 
    		required reader. In my code I passed in the id of a spring 
    		bean defined elsewhere, and parameters required to initialize 
    		that bean.
    	-->
    	<property name="reader" value="#{jobParameters[input.itemReader]}"/>
    	<property name="resource" value="#{jobParameters[input.file.name]}"/>
    	<property name="format" value="#{jobParameters[job.input.format]}" />	
    </bean>
    and 'ReaderFactory' is a Spring FactoryBean class, defined as:

    Code:
    public class ReaderFactory implements ApplicationContextAware, FactoryBean {
    	protected ApplicationContext applicationContext;
    	protected String reader;
    	protected String resource;
    	protected Format format; //provides format (fixedWidth, delimited etc.,
    			//and other detailed mapping information such as
    			//column names and ranges, also if header/footer is 
    			//present, wether multi-line etc.
    
    	public Class getObjectType() {
    		return AbstractItemCountingItemStreamItemReader.class; 
    	}
    
    	public boolean isSingleton() {
    		return false;
    	}
    
    	public Object getObject() {
    		// Construct, initialize and return the reader
    	}
    }
    In our implementation, 'reader' above is the id of a spring bean defined elsewhere. Using 'applicationContext', the reader bean is obtained and is initialized using the parameters provided.

    I hope this is helpful. Thanks.

  5. #5
    Join Date
    Jun 2010
    Posts
    23

    Default Thanks a Ton Mate

    Thanks a ton mate ,
    I was running short of ideas will implement it and then will let you know whether I was successfull in achivieng it.

  6. #6
    Join Date
    Jun 2010
    Posts
    23

    Default

    <bean id="EDIJob" parent="simpleJob" >
    <property name="steps" >
    <bean id="step1" parent="simpleStep" >
    <property name="streams" >
    <list>

    <ref bean="i9FileItemReader" />
    <ref bean="ansiFileWriter" />
    <ref bean="neutralizeWriter" />
    <ref bean="reportsWriter" />
    </list>

    </property>

    <property name="itemReader" ref="itemReader" />
    <property name="itemWriter" ref="itemWriter" />


    </bean>
    </property>
    </bean>



    <bean id="itemReader" class="com.abcbs.EDI837.reader.EDI837ItemReader" >

    <property name="itemReader" ref="someOtherItemReader" />
    <property name="resource" ref="inputResource" />
    <property name="i9FileReader" ref="i9FileItemReader" />
    <property name="additionalSegmentFlag" value="#{jobParameters['addSeg']}"></property>
    <property name="replaceHISegment" value="jobParameters['hiSeg']}"></property>

    </bean>



    There so many other readers !!!
    So I need to read the file on the basis of I/P file Format ..


    I am not getting any idea how to intialize my own reader in the ReaderFactory class you have mentioned and apart form that how am i supposed to instatiate the application Context .

    Sorry for the trouble,i am newbee to the spring batch... so Detailed explanation would surely help me .

    Thanks
    Sandeep

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
  •