Announcement Announcement Module
No announcement yet.
Creating Jobs dynamically Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating Jobs dynamically

    Hi all,

    We're currently evaluating Spring Batch for use in our Spring-based application, and I wanted to get your feedback regarding one of the features we're trying to implement.

    The requirement is pretty simple. The user needs to be able to define a custom job with the ItemReader/configuration of his choice. Now - since there are many optional IteamReaders, I'm not sure it would make much sense to create "prototype" <jobs/> for each option in the Spring context.

    I was thinking it would make sense to programatically create jobs in the same way they're created by the <job/> tag. Is that the way you would go about this? If yes, then how would you create those beans?

    Thanks in advance!


  • #2
    Programmatically creating a Job is not hard (SimpleJob is the easiest to use), but I would recommend a different approach. What I would do is write a composite or dispatching version of the component that you need to vary (the ItemReader in your example, but the same technique applies to everything up to and including the Job itself in principle). Use a JobParameter or ExecutionContext entry to dispatch the current execution to the correct target. There is some support for this pattern in Spring AOP (TargetSource implementations), but it's quite straightforward to implement for a specific component type, like ItemReader without using AOP.


    • #3
      Creating Jobs Dynamically

      Hi Dave,
      In our current requirements, we also plan to use Spring Batch for our application in a way that dynamically compose a job with steps that was already configured (statically) in application job context (Spring IOC) file. Is there any Spring Batch sample code that demonstrates such approach.


      • #4
        Here's some code I'd posted earlier:
        	public void testProgramaticJob() throws Exception {
        		JobParameters parameters = new JobParametersBuilder(jobLauncherTestUtils.getUniqueJobParameters()).toJobParameters();
        		TaskletStep taskletStep = new TaskletStep(); 
        		Tasklet tasklet = new SplitFilesTasklet();
        		SimpleJob job = new SimpleJob("test");
        		JobExecution jobExecution = jobLauncherTestUtils.launchJob(parameters);
        		assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
        If you would like to create the step statically, just inject the Tasklet into the class that launches the job.

        Now the problem that I've not been able to solve is how to run a job from within a job (which is what you'd probably end up with in this situation).
        I've posted about it here:

        If you find a way to get around this, please post.



        • #5
          Hi Friends,

          This thread really help me a lot regarding how to create sequential job dynamically.

          Now I am trying to create a SPLIT job dynamically, but could not identify the classes that used to create SPLIT job dynamically. If any one have created SPLIT job dynamically, please share the sample code.

          It will be very helpful to us to develop our project ..

          Parag Phatowali