Skip to content

Incorrect invocation order of job execution listener in remote partitioning #4133

Closed

Description

@kianjavadi

Bug description
I'm using remote-partitioning with Kafka as the middleware. I have one manager and three workers. accordingly, one partition has been assigned for the manager's input topic and three partitions have been assigned for the worker's input.

the manager takes a job, creates multiples ExecutionContexts and sends those over Kafka. workers start processing the respective steps and send the message at the end of their process. manager will aggregate the worker's results and decide to complete the job if all workers are done. so far so good.

now assume first I run a long-running job that requires lots of time to finish and then I run a small job that finishes quickly. not surprisingly the second job finishes sooner and sends a completed signal, the manager consumes this message and continues the process. I even checked AggregatingMessageHandler, the completed message is related to the second job (short-running one) only, I checked the jobExecutionId

now the problem happens, I have a JobListener that has an afterJob method. this method will be run against the first job (the long-running one that is still being processed by workers), not the second one (the short-running one that a completed signal has been sent for it)! I can say this by looking at the jobExecutionId. it's really weird because I never saw in the logs that there's a completion signal for the first job.

after some time and whenever the first long-running job is finished, the final worker sends a completed message and the manager decides to finish the job, now the JobListener is run against the second job (short-running one)!

I couldn't understand what goes wrong? I would like to assume that probably it's a miss-configuration, but by debugging the code and checking AggregatingMessageHandler and TRACE logs in the workers and manager, I can clearly see that the messages are being sent fine and there's nothing wrong with the messages.

Environment
I'm using spring-batch along with spring-boot 2.5.6, Java 11, MySQL 8, Kafka 3

Steps to reproduce
here is a sample project that you can run, instructions on how to run a job are in the readme file
first, you can run a long job: POST http://localhost:28080/job?minId=10&maxId=28
and then you can run a short job: POST http://localhost:28080/job?minId=28&maxId=30
the shorter job is going to be finished sooner, but despite the older job still running in the workers, you can see in the manager's logs the JobListener is triggered for the older job.

Expected behavior
since the completion signal belongs to the second job, then the JobListener also must be triggered for the second job, not the older one

(I've also asked the same question on StackOverflow a couple of days ago. since there's no response on it, I decided to mention it here)

Activity

  1. added
    status: waiting-for-triage
    on Jun 16, 2022
  2. fmbenhassine commented on Jun 17, 2022

    @fmbenhassine
    Contributor

    Thank you for the complete example! Nicely done 👍 However, we prefer to keep the issue tracker exclusively for bug reports and feature requests, see Getting Help. Support requests like this will be handled on StackOverflow. I asked for more details on your initial description on SO and was planning to follow up once you give more details. Now that you provided a complete example, I will use it to debug your case and get back to you asap on SO.

    I am closing this issue for now, but if we validate this is bug in Spring Batch, I will re-open it.

  3. added
    for: stackoverflow
    and removed
    status: waiting-for-triage
    on Jun 17, 2022
  4. fmbenhassine commented on Jun 22, 2022

    @fmbenhassine
    Contributor

    This is a bug in Spring Batch. The listener is indeed called for the job that finishes earlier with the wrong JobExecution instance. Making the JobExecutionListener job-scoped does not solve the issue.

    Re-opening this issue for further investigation.


    As a side note, the commands used to create topics in the attached minimal complete example should be updated as follows:

    % ./kafka-topics.sh --bootstrap-server 127.0.0.1:29092 --topic requestForWorkers --create --partitions 3 --replication-factor 1
    Created topic requestForWorkers.
    % ./kafka-topics.sh --bootstrap-server 127.0.0.1:29092 --topic repliesFromWorkers --create --partitions 1 --replication-factor 1
    Created topic repliesFromWorkers.
    

    Minimal complete example:

    remote-partitioning-kafka.zip

  5. added and removed
    for: stackoverflow
    on Jun 22, 2022
  6. changed the title [-]while remote-partitioning with Kafka - master always continues the oldest job in the JobListener, although the completion signal might be for a different job[/-] [+]Incorrect invocation order of job execution listener in remote partitioning[/+] on Jun 23, 2022
  7. kianjavadi commented on Aug 16, 2022

    @kianjavadi
    Author

    hi,
    is there any update on this issue?
    and btw, it's not only about the listeners, even the next step (if any) will be invoked incorrectly.

  8. added
    has: minimal-example
    on Nov 17, 2022
  9. 11 remaining items

  10. modified the milestones: 6.0.0-RC2, 6.0.0 on Nov 5, 2025
  11. fmbenhassine commented on Nov 18, 2025

    @fmbenhassine
    Contributor

    Could you try to @stepscope a job bean so that each individual job has their own listener.

    That is not the issue, see my comment here: "Making the JobExecutionListener job-scoped does not solve the issue." (step scope won't work neither).

    There is a similar/related issue where events are getting received out of order, see #4945. It is probably related to the channel implementation that is used in MessageChannelPartitionHandler, which should be a SubscribableChannel instead of PollableChannel (but I need to verify that, and I will ask for help from the Spring Integration experts in the team to make sure to use the right implementation in Spring Batch).

  12. removed this from the 6.0.0 milestone on Nov 18, 2025
  13. fmbenhassine commented on Jun 3, 2026

    @fmbenhassine
    Contributor

    Using a SubscribableChannel fixes the problem. I tested the patch provided in #4945 and it fixes the attached reproducer.

    However, the fix requires a breaking change, so I will plan it for v6.1 (unfortunately I was not able to include it v6.0).

    gh4133.zip: contains a README with detailed instructions and logs.

  14. added and removed
    for: backport-to-5.2.x
    on Jun 3, 2026
  15. added this to the 6.1.0 milestone on Jun 3, 2026
  16. modified the milestones: 6.1.0, 6.1.0-M2 on Sep 10, 2026
  17. added
    api: breaking-change
    on Sep 10, 2026
  18. added a commit that references this issue on Sep 10, 2026
    8c2d5a4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: breaking-changehas: minimal-examplein: integrationtype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions