Announcement Announcement Module
Collapse
No announcement yet.
Creating Jobs dynamically Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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!

    Nadav

  • #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.

    Comment


    • #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.
      Thanks,

      Comment


      • #4
        Hi,
        Here's some code I'd posted earlier:
        Code:
        	@Test
        	public void testProgramaticJob() throws Exception {
        	
        		JobParameters parameters = new JobParametersBuilder(jobLauncherTestUtils.getUniqueJobParameters()).toJobParameters();
        		
        		TaskletStep taskletStep = new TaskletStep(); 
        		taskletStep.setName("step1");
        		taskletStep.setJobRepository(jobRepository);
        		taskletStep.setTransactionManager(transactionManager);
        		Tasklet tasklet = new SplitFilesTasklet();
        		taskletStep.setTasklet(tasklet);
        		
        		SimpleJob job = new SimpleJob("test");
        		job.addStep(taskletStep); 
        		job.setJobRepository(jobRepository);
        
        		jobLauncherTestUtils.setJob(job);
        		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:
        http://forum.springsource.org/showth...m-within-a-Job

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

        HTH!

        Comment


        • #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 ..

          Thanks
          Parag Phatowali

          Comment

          Working...
          X