Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Java Code after a Method Return?

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:

  1. Call a specific operation (method) of my web service
  2. Start processing
  3. Sent an ack that the processing has been started (this is done be a return value)
  4. Continue processing

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.

like image 414
thomelicious Avatar asked Feb 14 '12 11:02

thomelicious


3 Answers

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.

like image 168
Elias Vasylenko Avatar answered Oct 26 '22 22:10

Elias Vasylenko


How about invoking a callback method to the client. See the workflow

  1. Call a specific operation (method) of my web service
  2. Start processing
  3. Call a method on the client, sending an acknowledgement.
  4. Continue processing
like image 39
Sajan Chandran Avatar answered Oct 26 '22 23:10

Sajan Chandran


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.

like image 30
Gilthans Avatar answered Oct 27 '22 00:10

Gilthans