Announcement Announcement Module
Collapse
No announcement yet.
Sync/Async job questions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sync/Async job questions

    Hi all,
    we're trying SpringBatch to see if we adopt it for batch processing. We have to make a job runnable standalone and from a web application with time slots, so if we're in the "online" time slot only one thread should be executed or n-threads if we're in the "batch" slot.

    Our first approach was to create a custom launcher and inside it use org.springframework.core.task.SyncTaskExecutor or org.springframework.core.task.SimpleAsyncTaskExecu tor depending on the time slot we get from the lancher bean's properties.

    For testing we're using CommandLineJobRunner.main(args) but although the async task executor is correctly inyected nothing happens, almost nothing. In the BATCH_JOB_EXECUTION table we get the following row

    3 0 3 17/08/10 14:37:11,972000000 STARTING UNKNOWN 17/08/10 14:37:11,972000000

    but as said, nothing is running...

    Context excerpt:
    Code:
    <bean id="jobLauncher" class="gnf.ada.batch.launcher.BatchLauncher">
    	<property name="jobRepository" ref="jobRepository" />
    	<property name="taskExecutorOnline" ref="taskExecutorOnline" />
    	<property name="taskExecutorBatch" ref="taskExecutorBatch" />
    	<property name="hourBatch" value="${batch.time.batch}" />
    	<property name="hourOnline" value="${batch.time.online}" />
    </bean>
    
    <bean id="taskExecutorOnline" class="org.springframework.core.task.SyncTaskExecutor" />
    
    <bean id="taskExecutorBatch" class="org.springframework.core.task.SimpleAsyncTaskExecutor" >
        <property name="concurrencyLimit" value="3" /> <!--this should be configurable-->
    </bean>
    and our joblauncher excerpt:
    Code:
    public class BatchLauncher implements JobLauncher, InitializingBean {
    	
    	protected Logger logger = Logger.getLogger(this.getClass());
    	
    	private TaskExecutor taskExecutorOnline;
    	private TaskExecutor taskExecutorBatch;
    	private String hourOnline;
    	private String hourBatch;
    	
    	private JobRepository jobRepository;
    
    ....
    ....
    
    public JobExecution run(final Job job, final JobParameters jobParameters)
    		throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException,
    		JobParametersInvalidException {
    		
    		Assert.notNull(job, "The Job must not be null.");
    		Assert.notNull(jobParameters, "The JobParameters must not be null.");
    
    		final JobExecution jobExecution;
    		JobExecution lastExecution = jobRepository.getLastJobExecution(job.getName(), jobParameters);
    		if (lastExecution != null) {
    			if (!job.isRestartable()) {
    				throw new JobRestartException("JobInstance already exists and is not restartable");
    			}
    		}
    
    		// Check the validity of the parameters before doing creating anything in the repository...
    		job.getJobParametersValidator().validate(jobParameters);
    
    		/*
    		 * There is a very small probability that a non-restartable job can be
    		 * restarted, but only if another process or thread manages to launch
    		 * <i>and</i> fail a job execution for this instance between the last
    		 * assertion and the next method returning successfully.
    		 */
    		jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
    		
    		
    		TaskExecutor taskExecutor = getTaskExecutorFromTimeOfDay();
    
    		try {
    			taskExecutor.execute(new Runnable() {
    
    				public void run() {
    As you can see, is a modified copy of the SimpleJobLauncher in which we select one of the task executors.

    Questions:

    Is this correct? or we should use multithreading in the Job instead the Launcher?

    Is any way to change a task executor in runtime/job definition/somewhere?

    Thanks in advice.

  • #2
    > Is any way to change a task executor in runtime/job definition/somewhere?

    As far as I know you can create e.g. SmartTaskExecutor which will be configurable and will delegate work to appropriate TaskExecutor implementation based on window. Remember to define the bean as prototype and keep choosen executor for all time running particular job.

    Job hierary of objects is created on fly every time when you try to get a been from application context so at the moment task executor should be choosen.

    In the term of AsyncTaskExecutor, it is workinf as fire-and-forget so probably you are not waiting for job finish after launch so main program finishes and kills child process with job but I'm not sure

    HTH,
    Jul

    Comment


    • #3
      Thanks Jul,
      I'll try that way.

      All the best.

      Comment

      Working...
      X