Skip to content

listeners

Mahmoud Ben Hassine edited this page Jun 5, 2017 · 3 revisions

Listeners allow you to plugin custom behavior in the workflow in a AOP style. You can register listeners to do pre/post processing before/after the entire job or a specific step.

Job listener

To listen to job related events, you need to register an implementation of the JobListener interface:

public interface JobListener {

    void beforeJobStart(JobParameters parameters);

    void afterJobEnd(JobReport report);

}

The next figure shows when each method of the JobListener is called in the processing workflow:

You can register a custom job listener using the JobBuilder API:

Job job = new JobBuilder()
    .jobListener(new MyJobListener())
    .build();

Several use cases can be implemented using this listener:

  • Setup/cleanup resources before/after the job
  • Lock/Unlock working directory at job start/stop
  • Archive log files at the end of Job
  • Send job report by email at the end of execution
  • etc

Record reader/writer listeners

The RecordReaderListener and RecordWriterListener allow you to get notified before/after reading/writing each record:

public interface RecordReaderListener {

    void beforeRecordReading();

    void afterRecordReading(Record record);

    void onRecordReadingException(final Throwable throwable);

}

public interface RecordWriterListener {

    void beforeRecordWriting(Batch batch);

    void afterRecordWriting(Batch batch);

    void onRecordWritingException(Batch batch, final Throwable throwable);

}

Here is when each method is called in the processing workflow:

You can register a custom record reader/writer listener using the JobBuilder API:

Job job = new JobBuilder()
    .readerListener(new MyReaderListener())
    .writerListener(new MyWriterListener())
    .build();

Processing pipeline listener

The PipelineListener allows you to get notified before/after processing each record:

public interface PipelineListener {

    Record beforeRecordProcessing(final Record record);

    void afterRecordProcessing(final Record inputRecord, final Record outputRecord);

    void onRecordProcessingException(final Record record, final Throwable throwable);

}

Here is when each method is called in the processing workflow:

You can register a custom pipeline listener using the JobBuilder API:

Job job = new JobBuilder()
    .pipelineListener(new MyPipelineListener())
    .build();

Here are some use cases:

  • Calculate the processing time for each record
  • Custom Pre/Post processing for each record
  • etc

Batch listener

The BatchListener allows you to get notified before/after processing each batch:

public interface BatchListener {

    void beforeBatchReading();

    void afterBatchProcessing(final Batch batch);

    void afterBatchWriting(final Batch batch);

    void onBatchWritingException(final Batch batch, Throwable throwable);

}

Here is when each method is called in the processing workflow:

You can register a custom batch listener using the JobBuilder API:

Job job = new JobBuilder()
    .batchListener(new MyBatchListener())
    .build();

Here are some use cases:

  • Define transaction boundaries
  • Custom Pre/Post processing for each batch
  • etc
Clone this wiki locally