Results 1 to 4 of 4

Thread: Passing some objects from controller to batch-step

  1. #1

    Default Passing some objects from controller to batch-step

    Hello I am a currently new to Spring Batch.And i am facing a difficulty which I searched alot but can't find the solution.My requirement is to process N folders through N policy templates.For example first step will check if the Folder Name is according to my policy or not ,second step will check analyse the files mime type inside the folder according to policy template etc etc...I have configured my steps properly. Upto this point the day was having sunshine for me but I am stuck at point where i need to pass values from my controller method to spring batch.Like selected folders by user and the policy templates from database.So my question is how to pass the objects from controller to my first step of batch process.

    Following is my configuration regarding batch job.

    applicationContext.xml


    Code:
    <!--Batch job configuration starts here-->
        <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"
              p:jobRepository-ref="jobRepository" 
              p:taskExecutor-ref="taskExecutor"/>
    
        <batch:job-repository id="jobRepository"
                              data-source="safeJNDI"
                              transaction-manager="transactionManager"
                              isolation-level-for-create="SERIALIZABLE"
                              max-varchar-length="1000"
        />
    
        <bean id="jobOperator" class="org.springframework.batch.core.launch.support.SimpleJobOperator">
            <property name="jobExplorer">
                <bean class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
                    <property name="dataSource" ref="safeJNDI" />
                </bean>
            </property>
            <property name="jobRepository" ref="jobRepository" />
            <property name="jobLauncher" ref="jobLauncher" />
            <property name="jobRegistry" ref="jobRegistry"/>
        </bean>
        <bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry"/>
    
    
        <batch:job id="templateMatchingJob" restartable="true">
            <batch:listeners>
                <batch:listener ref="templateMatchingJobListener"></batch:listener>
            </batch:listeners>
    
           <batch:step id="basicRuleCheckerStep">
    
                <batch:tasklet ref="basicRuleCheckerTasklet" />
                <batch:next on="COMPLETED" to="checkMainObjectStep" />
                <batch:next on="FAILED" to="logAndShowErrorMessage" />
                <batch:listeners>
                    <batch:listener ref="basicRuleCheckerListener"></batch:listener>
                </batch:listeners>
    
            </batch:step>
    
            <batch:step id="checkMainObjectStep">
                <batch:tasklet ref="mainObjectCheckerTasklet" />
                <batch:next on="COMPLETED" to="checkDBXML" />
                <batch:next on="FAILED" to="logAndShowErrorMessage" />
    
                <batch:listeners>
                    <batch:listener ref="mainObjectCheckerListener"></batch:listener>
                </batch:listeners>
            </batch:step>
            <batch:step id="checkDBXML">
                <batch:tasklet ref="dbXMLCheckerTasklet" />
                <batch:next on="COMPLETED" to="checkPDIXML" />
                <batch:next on="FAILED" to="logAndShowErrorMessage" />
                <batch:listeners>
                    <batch:listener ref="dbXMLCheckerListener"></batch:listener>
                </batch:listeners>
            </batch:step>
            <batch:step id="checkPDIXML">
                <batch:tasklet ref="pdiXMLCheckerTasklet" />
                <!-- the next is enclosure checking -->
                <batch:next on="COMPLETED" to="logAndShowErrorMessage" />
                <batch:next on="FAILED" to="logAndShowErrorMessage" />
    
                <batch:listeners>
                    <batch:listener ref="pdiXMLCheckerListener"></batch:listener>
                </batch:listeners>
            </batch:step>
            <batch:step id="logAndShowErrorMessage">
                <batch:tasklet ref="logAndShowErrorTasklet" />
                <batch:listeners>
                    <batch:listener ref="logAndShowErrorListener"></batch:listener>
                </batch:listeners>
            </batch:step>
        </batch:job>
    
       <!--Tasklets for template Matching starts here-->
        <bean id="basicRuleCheckerTasklet" class="com.hcdc.coedp.safe.domain.batch.templateMatching.BasicRuleCheckerTasklet"/>
        <bean id="mainObjectCheckerTasklet" class="com.hcdc.coedp.safe.domain.batch.templateMatching.MainObjectCheckerTasklet">
    
            </bean>
        <bean id="pdiXMLCheckerTasklet" class="com.hcdc.coedp.safe.domain.batch.templateMatching.PDIXMLCheckerTasklet"/>
        <bean id="dbXMLCheckerTasklet" class="com.hcdc.coedp.safe.domain.batch.templateMatching.DBXMLCheckerTasklet"/>
        <bean id="logAndShowErrorTasklet" class="com.hcdc.coedp.safe.domain.batch.templateMatching.LogAndShowErrorTasklet"/>
       <!--Tasklets for template Matching ends here-->
    
       <!--Listeners for each step in Batch Template Matching starts here-->
        <bean id="basicRuleCheckerListener" class="com.hcdc.coedp.safe.domain.batch.templateMatching.listener.BasicRuleCheckerListener"/>
        <bean id="mainObjectCheckerListener" class="com.hcdc.coedp.safe.domain.batch.templateMatching.listener.MainObjectCheckerListener"/>
        <bean id="pdiXMLCheckerListener" class="com.hcdc.coedp.safe.domain.batch.templateMatching.listener.PDIXMLCheckerListener"/>
        <bean id="dbXMLCheckerListener" class="com.hcdc.coedp.safe.domain.batch.templateMatching.listener.DBXMLCheckerListener"/>
        <bean id="logAndShowErrorListener" class="com.hcdc.coedp.safe.domain.batch.templateMatching.listener.LogAndShowErrorListener"/>
        <bean id="templateMatchingJobListener" class="com.hcdc.coedp.safe.domain.batch.templateMatching.listener.TemplateMatchingJobListener" />
       <!--Listeners for each step in Batch Template Matching ends here-->
    BatchJobController.java

    Code:
    @RequestMapping(value = "/decision/batch", method = RequestMethod.POST)
        @ExceptionHandler(value = {GenericException.class})
        public ModelAndView batchMoveFolder(@ModelAttribute(KEY_LOGGED_IN_USER) User whoLoggedIn,      @RequestParam String action, ModelMap model, HttpServletRequest request) 
      {
         //Getting the selected folder list
    
    
             /** Details omitted for clarity**/
    
        //Getting the templates available in database
    
             /** Details omitted for clarity**/
    
        //Starting the job
    
        jobLauncher.run(job,new JobParametersBuilder().addDate(new Date()));   
       }
    I am sorry for my bad English but I was not getting any better way of putting this.Any help is appreciated.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Pass them as JobParameters just like the date... You can only pass simple objects so you will need to do some conversion in your batch job (unless the properties are simple strings)...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Thanks for replying....This is what i have done passing the folder names as parameter.....Is there any better way of achieving this.Suggest me if there's any...Thank you.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    The folder names are job parameters so no there isn't a better way this is the way to pass arguments to the batch job.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

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
  •