In the simplest case, you can just go with Thread + an arbitrary Runnable.
If you want to block the call from the perspective of the caller, you can create a βserviceβ class that starts the worker thread and uses Thread. join (long) to wait for the operation to complete or to refuse it after the specified timeout (pay special attention to the correct handling of InterruptedException so that everything is not confused).
Thread.isAlive () will tell you whether Thread was completed or not.
Getting the result is a separate issue; I think you can handle this ...
[EDIT]
A quick and dirty example ( do not use in the production process as it is! ):
public V theServiceCall(final T param) { final MyResultBuffer<V> buffer = new MyResultBuffer<V>(); Runnable task = new Runnable() { public void run() { V result = ejb.process(param); buffer.putResult(result); } } Thread t = new Thread(task); t.setDaemon(true); t.start(); try { t.join(TASK_TIMEOUT_MILLIS); } catch (InterruptedException e) {
NOTE. Instead of Thread.setDaemon (), you can implement the shutdown flag in your Runnable, as that would be a better solution.
[/ EDIT]
source share