Thread Use in Listeners

As you look at the methods in the listener interfaces, you might wonder what role threads play in the invocation of the listener methods. There is indeed a sequence to the listener method calls, as you can imagine by looking at the names. During the life cycle of a job execution, the order of calls to the listener is generally fixed. Figure 7.2 illustrates the order in which the listener methods are called and how the worker threads are involved.

Figure 7.2. Listener methods are called in a particular sequence.

The sequence of listener method calls is fixed. As Figure 7.2 shows, the same thread that calls the job's execute() method is used to call the JobListener and triggerListener methods before and after the execution. This is important to know if you are using any sort of third-party thread-management tools or have decided to implement your own thread-pool management. It can also have a negative impact on performance if you implement long-running logic in the listener methods. Because the thread that invokes the listener methods is the same worker thread executing jobs, you should not implement listener methods that are complicated and that take a long time to complete. Keep their execution times to a minimum.

Категории