Spring REST with Async Methods
May 10, 2016This post was inspired by Creating Asynchronous Methods published at spring.io.
When dealing with RESTful services in Spring, they are relatively easy to setup. That is if you use Spring Boot. However, how do you deal with asynchronous requests. In short, how do you create a non-blocking RESTful API quickly? The easiest way is to add @Async to your methods so they are invoked asynchronously by Spring. All of this can be achieved without resorting to any methods from import java.util.concurrent.*
Setting up:
- Create a simple Spring Boot project using this tutorial Building a RESTful Web Service
- Add @EnableAsync to your Spring Boot Application (in the case of our example, this is Application.java)
- Create a Service and annotate the method you wish invoke asynchronously with @Async.
- Create the REST Controller class
- Add the dependency to the newly created service using @Autowired
- Create the request method (in our case this is a simple GET)
- Call the async method
- Run your project a SpringBoot App (Running a Spring Boot app in STS)
Add @EnableAsync to Application.java
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Create the Service with the @Async method
package hello;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class SampleAsyncService {
@Async
public void sampleAsyncMethod() {
long time = System.currentTimeMillis();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// We've been interrupted
System.out.println(String.format("Task interrupted after %d milliseconds", System.currentTimeMillis() - time));
return;
}
System.out.println(String.format("Task completed after %d milliseconds", System.currentTimeMillis() - time));
}
}
Create a Rest Controller and call the Async service
package hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SampleAsyncController {
@Autowired
public SampleAsyncService sampleAsyncService;
@RequestMapping(value = "/controller", method = RequestMethod.GET)
@ResponseBody
public String foo() {
long time = System.currentTimeMillis();
// Call Async service
sampleAsyncService.sampleAsyncMethod();
String response = String.format("Response completed in %d milliseconds.", (System.currentTimeMillis() - time));
// you should see this message before the message in the async method
System.out.println(response);
return response;
}
}
That's it.
/.Happy Coding
