uCosminexus Application Server, EJB Container Functionality Guide

[Contents][Glossary][Index][Back][Next]

2.17.6 Operation for execution status and execution result of an asynchronous method based on Future<V> object

If you specify Future<V> as the return value of the asynchronous method, you can execute the following processes using the method of Future<V>. You cannot execute these processes when the return value is void.
Organization of this subsection
(1) Cancelling asynchronous invocation processing
(2) Acquiring the execution result of the asynchronous invocation processing
(3) Confirming the execution status of asynchronous invocation processing
(4) Acquiring causes of the exception occurrence in asynchronous invocation processing

(1) Cancelling asynchronous invocation processing

You can cancel the processing by using the cancel method of the Future<V> object.

If the cancellation is successful, true is returned as the return value of the method. If the cancellation fails, false is returned as the return value.

If the asynchronous invocation fails, whether to interrupt the processing or execute it as it is, is decided depending on the specification in the mayInterruptIfRunning parameter of the cancel method.

(2) Acquiring the execution result of the asynchronous invocation processing

You use the get method of the Future<V> object to acquire the execution result.

The processing is complete when the processing is successfully executed and the processing result is acquired as the return value or when ExecutionException occurs. Hereafter, you can acquire the same result if you acquire the execution result using the same Future<V> object.

Note that if a timeout occurs during the method invocation of the Future<V> object in Application Server, EJBException is thrown for the client by the EJB container.

(3) Confirming the execution status of asynchronous invocation processing

You can confirm whether the execution of the asynchronous processing is complete or cancelled. You can check the status using the following methods of the Future<V> object:

In the asynchronous method, you can confirm whether the cancel method was invoked from the client machine, using the following methods:

(4) Acquiring causes of the exception occurrence in asynchronous invocation processing

If a system exception occurs in the method of the business interface, java.rmi.RemoteException is returned instead of javax.ejb.EJBException to the invocation source client machine. If a system exception is received during the asynchronous invocation processing, you can determine that the asynchronous method was not executed on the client machine. In such cases, you can invoke the asynchronous method again.

If an exception other than a system exception, such as an application exception occurs, the client can determine that the exception has occurred during the asynchronous processing after executing the asynchronous processing by the EJB container. In such cases, you can acquire the cause of the exception occurrence by using the exception object.

An example of coding to obtain the cause of the exception that occurs at the time of asynchronous invocation, by invoking the getCause method of an exception object, is as follows:

// Bean Client
Future future = asynSessionBean.performCalculation();
while (!future.isDone()) {
    Thread.currentThread().sleep(10000);
    future.cancel(true);
    break; 
}
if (future.isCancelled() == false) {
    Integer answer = null;
    try{
        answer  = (Integer)future.get();
    }  catch(ExecutionException e){
        System.out.println("caught exception: " +e.getCause()); 
    }
    System.out.println("Answer=" + answer.toString());  
}
asynSessionBean.performAddition();