I have a web service that is based on Java classes. Is it possible to run code after an acknowledge message has been returned within Java method of the called operation.
To understand better, this is the workflow:
Threads do not work, since the threads need to be terminated before the return of the method.
Does anybody has an idea how to implement this scenario or an alternative to it?
Thanks in advance.
You will need to use Threads for this, as you are splitting program flow into two; the returning path and the processing path are separating and running alongside each other.
If you need the acknowledgement for the method start to be sent by the return of the method then why can't your service providing methods look as simple as something like this?
public Acknowledgement someService() {
new Thread(new SomeServiceRunnable()).start();
return new Acknowledgement();
}
The service will be started, then the method will return (thus notifying that the process has started) whilst the processing continues until the thread ends...
Am I missing something here about what you need to achieve?
Hope this helps.
EDIT:
It seems that some answers have been engineered to address problems which I did not see as being presented as part of the question. Here are some assumptions I made when making my answer, presented so that anyone reading this answer might have a better idea of when it might not apply to their particular situation:
This is for a situation where you want only to acknowledge that a service has conceptually speaking begun execution. The acknowledgement can give no information about the success of any part of this execution or its initialisation, only that it has conceptually started, that is to say that the Associated Runnables run() will be called at some point.
Of course if you want the execution to start and the caller to return, then the significance of the acknowledgement will necessarily be limited by just exactly how much of the task waited to be executed before returning, here no waiting is done and the acknowledgement is returned immediately, and so no extra information can be given.
How about invoking a callback method to the client. See the workflow
The answer to your problem is creating a thread:
public boolean beginWorkAndSendAck(){
Thread t = new Thread(){
public void run(){
//Do something here
}
}
t.start();
return true;
}
The thread will carry on after the 'main' thread dies away. The problem you may be referring to, is if the web-service shuts down the process after the ack is sent. The thread should keep going, as java doesn't shut down its process unless all threads are done.
If it isn't working, it might the web service is doing some voodoo - closing the java process or somesuch. I'd suggest looking into creating a new process with java, here: http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html which would require a separate application to perform the job you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With