Polling Java EE background task status - how to?

1k views Asked by At

I'm currently working on a web application which needs to import data and do some processing. This can take some time (probably in the "several minutes" range, once the data sets grow), so we're running it in the background - and now the time has come to show status in the frontend, instead of tailing log files :)

The frontend is using Angular, hooked up to REST endpoints (JAX-RS) calling services in EJBs that do persistance via JPA. Running in JBoss EAP 6.4 / AS 7.5 (EE6). Standard stuff, but this is the first time I'm dealing with Java EE.

With regards to querying status, polling a REST endpoint periodically is fine - we don't need fancy stuff like websockets. We do need to support multiple background jobs, though, and information consisting of runstate (running/done/error), progress and list of errors.

So, I current have two questions:

1) Is there a more suitable way of launching a background task than calling a @Asynchronous EJB method?

2) Which options do I have for keeping track of the background tasks, and which is most suitable?

My first idea was to keep a HashMap, but that quckly ended up looking like too much manual (and fragile-looking) code with concurrency and lifetime concerns - and I prefer not reinventing the wheel. The safe choice seems to be JPA persisting it, but that seems somewhat clumsy for volatile status information.

I'm obviously not the first person facing these issues, but my google-fu seems to be lacking at the moment :)

1

There are 1 answers

5
Aksel Willgert On BEST ANSWER

The tasks could be launched using @Asynchronous or by using JMS @MessageDriven

From java-ee-7 ManagedExecutorService is also an option.

The tasks would then update their own state that is stored in a ConcurrentHashMap inside a @Singleton EJB.

If you are in a clustered environment, state of tasks is better stored using JPA, as @Singleton is not for whole cluster