Announcement Announcement Module
Collapse
No announcement yet.
Sample Spring batch Web Application Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sample Spring batch Web Application

    Hi,
    I have downloaded Spring batch application and its working fine with Test Cases,but now i want to integrate Spring batch in my Web application, but i dont have any idea to start execution like "CommandLineJobRunner" do in Test Cases.
    I glad if i get any suggession regarding Spring batch integration with Spring Web application or i get hello world Application as SPRING BATCH WEB APPLICATION.

    Waiting for reply.
    Thanx

  • #2
    Some ways/hints to resolve your issue

    Hi

    Someone has already posted a code to start a batch from tomcat :

    http://forum.springframework.org/showthread.php?t=51084


    Basically, i would say that launching and running a batch inside the code of a web application is not a good idea. If the batch is long, it can runs to a timeout of your request.
    I think you should launch it outside of the web app. The web application will only be in charge of triggering the launch of the webApp.
    For example your webapp can post a JMS message that will be intercepted by another program you write that will be in charge of running the job.


    Anymway, if you want to start and runs the job you need the following (in a servlet for example): (look at it in CommandJobLineRunner.java, method start)
    - Get your context (read the xml file with beans definitions)
    - Initialise the parameters of your job (if needed)
    - Lauch the job with the launcher (you need of course a job name)

    The code will be something like :
    Code:
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("the path to your xml context file");
    context.getAutowireCapableBeanFactory().autowireBeanProperties(this,
    				AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    	Job job;
    	job = (Job) context.getBean(jobName);
    
    // create your parameters for the job
    // you can use the JobParameterBuilders
    JobParametersBuilder builder = new JobParametersBuilder();
    //builder.addDate( ... );
    //builder.addString(...)
    JobParameters jobParameters = builder.toJobParameters();
    JobLauncher launcher = new SimpleJobLauncher();
    JobExecution jobExecution = launcher.run(job, jobParameters);
    
    //dont forget to manage the exit status...


    Hope this helps.


    Laurent

    Comment


    • #3
      A link which will be usefull

      Hi again,

      tbone21 has already open a thread on how to start a job from an online sytem:

      It is at http://forum.springframework.org/showthread.php?t=60421

      Hope this helps

      Comment


      • #4
        Originally posted by lbo View Post
        Basically, i would say that launching and running a batch inside the code of a web application is not a good idea. If the batch is long, it can runs to a timeout of your request.
        I think you should launch it outside of the web app. The web application will only be in charge of triggering the launch of the webApp.
        I'm using my web application to allow a user to upload a file that is then used as the input for my batch job(s). Uploading the file kicks of the batch (which runs asynchronously). The browser thread returns immediately--the user is not kept waiting while the batch job runs.

        If there is a batch error, an email is sent to a well known mailing list indicating the problem.

        Note that this web app that I've created is not for general public consumption--it is only for internal administrators (e.g. batch operators).

        See the link Ibo posted above. I've provided more extensive configuration examples there from my application.

        Comment


        • #5
          Thanks chudak for the reply.
          I did not see that there was an asychronous way of launching a batch. It can be usefull in a web app just like the example you provided.

          Laurent.

          Comment


          • #6
            Originally posted by lbo View Post
            Thanks chudak for the reply.
            I did not see that there was an asychronous way of launching a batch. It can be usefull in a web app just like the example you provided.

            Laurent.
            You use an async executor with your job launcher:

            Code:
                    <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
            		<property name="jobRepository" ref="jobRepository" />
            		<property name="taskExecutor">
            		  <bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
            		</property>
            	</bean>
            This is how I launch the job in a handler coupled with my MVC layer:

            Code:
                        Job batchJob = jobFactory.createJob();
                        Map<String,String> map = new HashMap<String,String>();
                        map.put(Constants.GUID, batch.getGuid());
                        map.put(Constants.UPLOAD_FILENAME, fileName);
                        JobParameters parameters = new JobParameters(map, new HashMap<String,Long>(), new HashMap<String,Double>(), new HashMap<String,Date>());
                       
                        // Now, kick off the job
                        jobLauncher.run(batchJob, parameters);
            Because I'm using an async executor, the call to run(...) returns immediately and I can resume the http request processing...

            Comment


            • #7
              You can also use the JobParametersBuilder class to help with the creation of JobParameters.

              Comment


              • #8
                Thanks for the tip Lucas. That makes things MUCH cleaner

                Code:
                            Job batchJob = jobFactory.createJob();
                            JobParametersBuilder parameters = new JobParametersBuilder();
                            parameters.addString(Constants.GUID, batch.getGuid());
                            parameters.addString(Constants.UPLOAD_FILENAME, fileName);
                            
                            // Now, kick off the job
                            jobLauncher.run(batchJob, parameters.toJobParameters());

                Comment


                • #9
                  how are you supplying the uploaded file to the job. Do you save it to a particular location and e.g. the item reader picks it up there? Or have you found a way of passing in a ByteArrayResource to your job?

                  Comment


                  • #10
                    Originally posted by nedge View Post
                    how are you supplying the uploaded file to the job. Do you save it to a particular location and e.g. the item reader picks it up there? Or have you found a way of passing in a ByteArrayResource to your job?
                    I'm using the commons-fileupload module to handle the upload and I serialize the file to the temp directory.

                    Comment

                    Working...
                    X