6

I have a method and it calls another method, for which the result is not important. To be exact, it is a POST request to a web service and the result is processed in that method and not in the calling method. Now I want the main method to return before that task finishes.

In essence, I need some sort of asynchronousity. What tools can I use in Java? Here are the steps again:

  1. Method A calls Method B
  2. Method B starts executing (we are not interested in the results of method b: it makes a call to a web service and processes the results itself)
  3. Method A returns before Method B finishes
Stephane Landelle
  • 6,990
  • 2
  • 23
  • 29
Kevin Wu
  • 1,357
  • 1
  • 15
  • 34

4 Answers4

2

You can use CompletableFuture.runAsync() to call method B async.

You can add a callback by calling .thenRun() on returned future if you want to do something when method B exited

Prim
  • 2,880
  • 2
  • 15
  • 29
1

If you are using Spring then its very easy and without any boilerplate code. Have a look at @Async support in Spring. Here is a very simple example to get you quickly started.

@Service
public class YourClass {
    public void methodA() {
        methodB();
    }

    @Async
    public void methodB() {
        // Your POST logic
    }
}

To enable async support in your xml configuration, you need to have something like below

<task:annotation-driven />
<task:executor id="defaultAsyncTaskExecutor" pool-size="5-15" queue-capacity="100" />
Jay
  • 1,539
  • 1
  • 16
  • 27
  • Hi Jay, I am indeed using Spring and this is the solution I am currently leaning towards. Just looking at how to set up my xml config for this solution. Do you have any advice on that? – Kevin Wu Dec 24 '15 at 09:01
  • Updated the answer to have xml configuration – Jay Dec 24 '15 at 09:05
  • What if methodA() has a return value such as String. Will this still work the same? I am working through a similar issue and I haven't been able to get method A to return before method B completes. – Jim Stevens Jul 12 '20 at 16:56
  • @JimStevens yes it will just work the same as void method in above example – Jay Jul 13 '20 at 20:11
  • btw the [answer from Prim](https://stackoverflow.com/a/34449649/471467) is also a nice neat way to do if you are using Java 8 or later. No dependency on framework supporting async mechanism. – Jay Jul 13 '20 at 20:13
0
new Thread(new Runnable() {
    public void run() {
        //Do whatever
    }
}).start();
David Petran
  • 3
  • 1
  • 4
0

Try to use AsyncHttpClient ,example

`AsyncHttpClient asyncHttpClient = new AsyncHttpClient(
                                builder.setFollowRedirect(true).
                                setWebSocketTimeout(120000).
                                setAcceptAnyCertificate(true).
                                setMaxConnections(100).
                                setMaxRequestRetry(3).
                                setReadTimeout(120000).
                                setConnectTimeout(120000).
                                setRequestTimeout(120000).
                                build()); 
asyncHttpClient.prepareGet(url).execute(new AsyncCompletionHandler<Response>(){
                                @Override
                                public Response onCompleted(Response response) throws Exception{
                                      //your logic here
                                    }

                                @Override
                                public void onThrowable(Throwable t){
                                    //your logic here
                                }
                        }); 

`

Azat Nugusbayev
  • 1,391
  • 11
  • 19