How to work with Spring Callable Controller

Spring Callable Controller is an asynchronized approach of Spring Requests with Servlet 3, it will help to scale a heavy system by releasing container threads while processing expensive tasks. In the How to work with Spring Callable Controller tutorial, JavaSampleApproach will guide you how to work with Spring Callable Controller.

Related Posts:
1. Spring Framework 4.3 New Feature RequestMapping: @GetMapping, @PostMapping, @PutMapping, @DeleteMapping
2. Spring MVC – @RequestMapping with Methods, Headers, Params, @PathVariable and @RequestParam


I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot

II. Overview
1. Project Structure

spring callable controller project structure

2. Step to do

– Create Spring Boot project
– Create a Service
– Create Spring Callable Controller
– Run & Enjoys Result

III. Practices
1. Create Spring Boot project

– Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, add project info. Press Next, then Finish, a Spring Boot project will be created successfully.

Open pom.xml file, add Web dependency:

2. Create a Service

Create a simple service:

3. Create Spring Callable Controller

Create CallableController with 2 RequestMappings:
– 1 normal request
– 1 callable request: use interface Callable for return results

4. Run & Enjoys Result

– Run the project with Spring Boot mode.
– Make a normal-request: localhost:8080/normalrequest, Logs:

– Make a callable-request: localhost:8080/callablerequest, Logs:

Explain results:
When make a callable request, the container thread for handling the request is returned immediately, while a heavy job (with Callable thread) is managed by another Excuting Manager of Spring for long time proccessing. And the connection between Client and Server are keep for communication util the job is Done and return result to client.

IV. Sourcecode

SpringMVCCallableController


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*