Results 1 to 5 of 5

Thread: Creating Jobs dynamically

  1. #1
    Join Date
    Dec 2010
    Posts
    3

    Default 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. #2
    Join Date
    Jun 2005
    Posts
    4,232

    Default

    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. #3
    Join Date
    Feb 2006
    Posts
    7

    Default 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,

  4. #4
    Join Date
    Jun 2011
    Posts
    25

    Default

    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!

  5. #5
    Join Date
    Dec 2011
    Posts
    25

    Default

    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

Posting Permissions

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