Run java code after method returns?

I have a web service based on Java classes Whether the code can be run after the confirmation message is returned in the Java method of the called operation

For better understanding, this is the workflow:

>Call the specific operation (method) of my Web Service > start processing > Send a confirmation that processing has started (this is a return value) > continue processing

The thread does not work because the thread needs to terminate before returning the method

Does anyone know how to achieve this situation or replace it?

Thank you in advance

Solution

You need to use threads because you divide the program flow into two parts; The return path and the processing path are separated and run side by side with each other

If you need to confirm the method to start sending through the return method, why doesn't your service provision method look so simple?

public AckNowledgement someService() {
  new Thread(new SomeServiceRunnable()).start();
  return new AckNowledgement();
}

The service will start, and then the method will return (notifying the process that it has started), while processing continues until the thread ends

Did I miss some goals you need to achieve here?

I hope this will help

Edit:

It seems that some answers have been designed to solve problems that I don't see presented as part of the problem Here are some of the assumptions I made when I made the answer, which are intended to give people who read the answer a better understanding of when it may not apply to their specific situation:

This applies if you just want to acknowledge that the service has conceptually started to execute Confirmation cannot provide information about any part of this execution or its initialization success, but it has started conceptually, that is, associated runnables run() will be called at some time

Of course, if you want to start and the caller returns, the importance of confirmation must be limited by the exact number of tasks waiting to be executed before returning. There will be no waiting here and the confirmation will be returned immediately, so no additional information can be given

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>