Async Tasks
This module works only for Ontimize Boot version 3.8.0 or above. Actual release version:
Introduction
The Async Task system will allow you to run decoupled, asynchronous tasks. This module will let you run any service method in a separate, newly created thread, by simply adding an annotation to its controller method.
Previous concepts
- Task: It is the generic representation of a decoupled task. It stores information such as its UUID, its current status and the result of the execution.
- Aspect: It is a modularization of a concern that cuts across multiple classes. It allows us to intercept the execution of any given method or class and implement some alternative or extra behaviour for it.
Prerequisites
You can follow this tutorial using your own application, although for this example we will use an application created using the archetype that can be found on this page and with a REST service.
There are 2 options to follow this tutorial, clone the repository with the initial state and follow the tutorial step by step, or download the final example and see which files are new and which have been updated.
Initial project
/$ git clone https://github.com/ontimize/ontimize-examples
/ontimize-examples$ cd ontimize-examples
/ontimize-examples$ git checkout boot-async-task-initial
Final example
/$ git clone https://github.com/ontimize/ontimize-examples
/ontimize-examples$ cd ontimize-examples
/ontimize-examples$ git checkout boot-async-task
To simplify the code being written, three dots (…) may appear in some parts of the code. This indicates that there may be previous code before and after those dots.
Steps
Database
Tasks Table
With the database started, we create the new table that will store the tasks information.
Server
Add Ontimize AsyncTask dependencies
The decoupled tasks system is integrated in the Ontimize Core module, so we need to declare it as a project dependency.
boot/pom.xml
model/pom.xml
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- application.yml
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.properties
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- UserDao.java
- UserRoleDao.java
- service
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- placeholders.properties
- RoleDao.xml
- RoleServerPermissionDao.xml
- ServerPermissionDao.xml
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Modify application.yml
The application.yml file will be modified to enable the decoupled tasks module, indicate the storage engine it will use, the URL base path for the service, and its thread pool configuration. In this link you have information about the configuration of the asynchronous tasks system in the application.yml file.
The enable property must be set to true and the storage engine type must be specified in the engine property before the server is started.
The asynchronous tasks system requires the Ontimize TaskExecutor to be configured, see this link.
application.yml
For database storage
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- entities
- Candidate.java
- service
- ICandidateService.java
- IUserService.java
- entities
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- application.yml
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.properties
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- CandidateDao.java
- TaskDao.java
- UserDao.java
- UserRoleDao.java
- service
- CandidateService.java
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- CandidateDao.xml
- placeholders.properties
- RoleDao.xml
- RoleServerPermissionDao.xml
- ServerPermissionDao.xml
- TaskDao.xml
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- CandidateRestController.java
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Add Task DAO
A specific DAO will be created for the tasks table, and it will implement the DAO interface in the tasks module.
TaskDao.xml
TaskDao.java
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- application.yml
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.properties
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- TaskDao.java
- UserDao.java
- UserRoleDao.java
- service
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- placeholders.properties
- RoleDao.xml
- RoleServerPermissionDao.xml
- ServerPermissionDao.xml
- TaskDao.xml
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Annotate controller method
In order to run some service method asynchronously, we need to annotate its respective REST controller method with @OAsyncTask. This way, a new thread will be created in order to handle the method’s execution, and we will recieve an instant response with the URL where we can check the execution status and retrieve its result when it’s finished.
The service’s method MUST return a serializable object with getters and setters, as well as the controller’s method must return a ResponseEntity object. In this case, the
query()
method returns a Serializable object, the EntityResult.
We will override the query()
method of the ORestController class.
CandidateRestController.java
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- ICandidateService.java
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- application.yml
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.properties
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- CandidateDao.java
- TaskDao.java
- UserDao.java
- UserRoleDao.java
- service
- CandidateService.java
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- CandidateDao.xml
- placeholders.properties
- RoleDao.xml
- RoleServerPermissionDao.xml
- ServerPermissionDao.xml
- TaskDao.xml
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- CandidateRestController.java
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Delay service method
To know all the states through which the asynchronous request passes, we will add a delay in the candidateQuery()
method.
CandidateService.java
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- ICandidateService.java
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- application.yml
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.properties
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- CandidateDao.java
- TaskDao.java
- UserDao.java
- UserRoleDao.java
- service
- CandidateService.java
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- CandidateDao.xml
- placeholders.properties
- RoleDao.xml
- RoleServerPermissionDao.xml
- ServerPermissionDao.xml
- TaskDao.xml
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- CandidateRestController.java
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Testing
To test the asynchronous tasks we need to execute a REST request to the method that we have marked with the annotation @OAsyncTask.
In this case, the request is GET and has the following structure: http://localhost:33333/candidates/candidate?columns=ID,NAME,SURNAME
Element | Meaning |
---|---|
localhost:33333 | Indicates the host |
/candidates | Indicates the service to be queried |
/candidate | Indicates the DAO that will access that service |
?columns= | Indicates the columns to be queried |
The authorization used for this requests is authorization of the type BASIC.
The access must be done with a user and password example:
User: demo
Password: demouser
When you run the query, it should return a 202 Accepted with the following header: Location.
This header contains the relative path to the asynchronous task that you have to execute to receive the data.
Example
Location:
/tasks/f16e9af7-ec0f-444c-a173-5b0179f5d57f
To execute the query the request needs to be GET and have the following structure: http://localhost:33333/tasks/f16e9af7-ec0f-444c-a173-5b0179f5d57f
The uuid that goes after
/tasks
varies in each execution of the previous query.
The first time you run this query the status becomes Started. When the time set in the delay expires the second time you execute the query, the status becomes Completed, returns the request data and removes the task from the TASKS table.