asynchronousjakarta-eeejbresteasywildfly-11

Why is my RestEasy WebService blocked when my @Asynchronous method is working?


I am trying to run heavy tasks asynchronously. The client then polls the server to know when the job is done. This seemed to work, but I noticed that my WebService that responds to the polling is blocked when I put a breakpoint in my @Asynchronous Method.

This is what I did:

JobWS.java // Used to start a job

@RequestScoped
@Path("/job")
@Produces(MediaType.APPLICATION_JSON)
public class JobWS {

    @POST
    @Path("/run/create")
    public Response startJob(MyDTO dto) {
        return ResponseUtil.ok(jobService.createJob(dto));
    }

}

JobService.java // Creates the job in the DB, starts it and returns its ID

@Stateless
public class JobService {

    @Inject
    private AsyncJobService asyncJobService;

    @Inject
    private Worker worker;

        public AsyncJob createJob(MyDTO dto) {
            AsyncJob asyncJob = asyncJobService.create();
            worker.doWork(asyncJob.getId(), dto);
            return asyncJob; // With this, the client can poll the job with its ID
        }

}

Worker.java // Working hard

@Stateless
public class Worker {

    @Asynchronous
    public void doWork(UUID asyncJobId, MyDTO dto) {
        // Do work
        // ...
        // Eventually update the AsyncJob and mark it as finished
    }
}

Finally, my Polling Webservice, which is the one being blocked

@RequestScoped
@Path("/polling")
@Produces(MediaType.APPLICATION_JSON)
public class PollingWS {

    @Inject
    AsyncJobService asyncJobService;

    @GET
    @Path("/{id}")
    public Response loadAsyncJob(@PathParam("id") @NotNull UUID id) {
        return ResponseUtil.ok(asyncJobService.loadAsyncJob(id));
    }
}

If I put a breakpoint somwhere in doWork(), the PollingWS does not respond to HTTP requests anymore. When I debug through doWork(), occasionally I get a response, but only when jumping from one breakpoint to another, never when waiting at a breakpoint.

What am I missing here ? Why is my doWork() method blocking my Webservice, despite it running asynchronously ?


Solution

  • I found the culprit. A breakpoint suspends all threads by default. In IntelliJ, a right click on it will open the following dialog:

    Breakpoint properties

    When changing the "Suspend" property to "Thread", my WS is not blocked anymore and everything works as expected. In retrospect, I feel a bit stupid for asking this. But hey... maybe it will help others :)