Skip to content

Instantly share code, notes, and snippets.

@jeffbicca
Last active December 28, 2015 23:59
Show Gist options
  • Save jeffbicca/7582694 to your computer and use it in GitHub Desktop.
Save jeffbicca/7582694 to your computer and use it in GitHub Desktop.
Keeping a long-running process executing in an asynchronous fashion using CDI, EJB and JSF 2.
<a4j:poll id="poll" interval="5000"
execute="@this"
render="@form" enabled="#{not (asyncProcessManager.isDone(selectedProcess))}"/>
@ApplicationScoped
public class AsyncProcessManager implements Serializable {
private Map<AsyncProcess, Future<Object>> asyncProcesses = new ConcurrentHashMap<AsyncProcess, Future<Object>>();
public void addProcess(AsyncProcess process) {
// put the process into the map.
}
public void removeProcess(AsyncProcess process) {
// remove the process from the map.
}
public boolean existsProcessFor(User user) {
// check if there is any process for the user.
}
public boolean isDone(AsyncProcess process) {
Future<Object> theAsyncExecution = findAsyncExecutionFor(process);
return theAsyncExecution != null && theAsyncExecution.isDone();
}
}
@Singleton
@TransactionAttribute(TransactionAttributeType.NEVER) // useful when you need to turn off the EJB transaction control.
public class AsyncService implements Serializable {
private Repository repository;
@Asynchronous
@Lock(LockType.READ)
@AccessTimeout(-1)
public Future<Object> operation1() {
// implementation
return new AsyncResult<Object>(null);
}
@Asynchronous
@Lock(LockType.READ)
@AccessTimeout(-1)
public Future<Object> operation2() {
// implementation
return new AsyncResult<Object>(null);
}
@Asynchronous
@Lock(LockType.READ)
@AccessTimeout(-1)
public Future<Object> operationN() {
// implementation
return new AsyncResult<Object>(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment