Results 1 to 7 of 7

Thread: How to monitor the progress of a Tasklet to send to the GUI ?

Threaded View

  1. #1

    Default How to monitor the progress of a Tasklet to send to the GUI ?

    Here is my spring batch config (relevant part) :
    Code:
        <batch:job id="fakeJob" parent="simpleJob">
            <batch:step id="fakeLongJobStep" parent="simpleStep">
                <batch:tasklet ref="mytasklet" />
            </batch:step>
        </batch:job>
    So this is just a job calling one step which is a tasklet.

    What I want to do is to report the progress inisde the tasklet.

    At first, I tried :
    Code:
    public class MyTasklet implements Tasklet {   
        @Override
        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
            JobExecution e = chunkContext.getStepContext().getStepExecution().getJobExecution();
            for (i = 0; i < 100; i++) {
                 e.getExecutionContext().put("progress", i+"/100");
                 Thread.sleep(2000); //simulate heavy operation
            }
        }
    }
    Then, from another thread (i.e another XHR request to my webapp), I ask for the current progress :
    Code:
       public String readProgress(int executionId) {
         return jobExplorer.getJobExcution(executionId).getExecutionContext().get("progress"); 
       }
    This always return null except when the job is finished (which then report 99/100)

    From what I understand, this is logical since the JobExecution is NOT persisted during the execution of a step (only persisted after).
    How can I do to track the progress ?

    The first thing I tried was to inject JobRepository into my tasklet and call jobRepository.updateExecutionContext(jobExecution) .
    I assume this does not work since the transaction is not commited (hence, other threads still do not see the progress).
    So how can I do ? Should I use a TransactionTemplate for each heavy operation or something like that ?

    Please, note that using the reader->writer paradigm is not possible in my case (ask details if you need to know why ).
    Last edited by cblin; Sep 20th, 2011 at 10:32 AM. Reason: minor bug fix in code

Posting Permissions

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