Is there any way to know the progress of an asynchronous EJB process?

I am trying to get the percentage of progress from an asynchronous EJB process. Is it possible?

Does anyone have an idea how I can do this?

+4
source share
3 answers

It’s always difficult to find out about the progress of asynchronous processes, especially if you don’t know if they really started.

The best way I've found is to write another function that just gets progress, so if you have a unique identifier for each call, update hashmap using the current process. You can look at Concurrent Hashmap ( http://download-llnw.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ConcurrentHashMap.html )

Then this other search function will simply accept a unique identifier and return the move back to the client.

If it has not been started, you can also return it, and ideally you would also want to return all error messages that occurred during processing.

Then, when it completes, and you returned an error or success message, then delete it from the hash card, the client received the information, and this information will not change, so you should not store it.

UPDATE:

Create a new function in your interface

 String progressDone(String id); 

Then you will access this synchronously, since it just goes out and comes back, so it can look for the id in hashmap and return either a percentage of the done or an error message.

But that means that your actual working function needs everyone to put the information in the hash map so often where it is, so I suggested using a parallel hash file, so you don’t have to worry about parallel writing, and thus considerations blocking.

+2
source

The solution I found is a context object shared by the asynchronous method and the main thread. Here is an example:

Asynchronous job:

 @Stateless public class AsyncRunner implements AsyncRunnerLocal { @Asynchronous public Future<ResultObject> doWorkAsynchronous(WorkContext context) { context.setRunning(true); for (int i = 0; i < 100; i++) { //Do the next iteration of your work here context.setProgress(i); } context.setRunning(false); return new AsyncResult(new ResultObject()); } } 

The general context object. Important here is the volatile keyword. Field values ​​will be locally cached in each stream without it, and progress will not be visible in the main stream:

 public class WorkContext { //volatile is important! private volatile Integer progress = 0; private volatile boolean running = false; //getters and setters are omitted } 

Usage example:

 public class ProgressChecker { @EJB private AsyncRunnerLocal asyncRunner; private WorkContext context; private Future<ResultObject> future; public void startJob() { this.context = new WorkContext(); future = asyncRunner.doWorkAsynchronous(this.context); //the job is running now while (!future.isDone()) { System.out.println("Progress: " + this.context.getProgress()); Thread.sleep(1000); //try catch is omitted } } } 
+1
source

In EJB3.1 @Asynchronous method calls can return java.util.concurrent.Future , this interface provides information to boolean isCancelled () or boolean isDone () , but there is no information if the start has started. From my point of view, there is no way to get information if the process started its execution through the EJB-Container in standard ways.

0
source

All Articles