Results 1 to 10 of 10

Thread: Sample Spring batch Web Application

  1. #1
    Join Date
    Sep 2008
    Location
    Ahmedabad [India]
    Posts
    11

    Default 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
    Rajesh Gajipara
    Ahmedabad (India)

  2. #2
    Join Date
    Jul 2008
    Posts
    16

    Default 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

  3. #3
    Join Date
    Jul 2008
    Posts
    16

    Default 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

  4. #4
    Join Date
    Jan 2008
    Location
    San Diego
    Posts
    780

    Default

    Quote 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.

  5. #5
    Join Date
    Jul 2008
    Posts
    16

    Default

    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.

  6. #6
    Join Date
    Jan 2008
    Location
    San Diego
    Posts
    780

    Default

    Quote 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...

  7. #7
    Join Date
    Dec 2006
    Posts
    1,061

    Default

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

  8. #8
    Join Date
    Jan 2008
    Location
    San Diego
    Posts
    780

    Default

    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());

  9. #9

    Default

    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?

  10. #10
    Join Date
    Jan 2008
    Location
    San Diego
    Posts
    780

    Default

    Quote 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.

Posting Permissions

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