Hello, welcome back to my blog about automated trading. Last week I
have summarized the interfaces with third parties. This week I will cover the task scheduling.
For the latest source code, please download repository and update to branch 8-scheduling, changeset dc70abec76f0.
For the latest source code, please download repository and update to branch 8-scheduling, changeset dc70abec76f0.
Background
Task scheduling is very important thing. During the day there are 3 possible types of tasks. The first type is invoked at the specified time (e.g. after market closes). The second type is invoked when some event happens (e.g. new message is delivered during trading session). And third type is invoked per user request.
I put the same core requirement for all of them. If there is an error, I would like to know about it.
In addition by the experience I discovered that it is useful to have the following:
- Stop on the first error when chain of tasks is invoked
- Log some of the tasks when they finish successfully (not all of them)
- Run tasks in synchronous and asynchronous mode
- Discover current status of any task
- Run scheduled taks with respect to the different time zone
Coding
The first interface to look at is Engine. This is just a container which allows to invoke tasks synchronously and asynchronously and allows to query the status of each of them. Then there is an implementation called SectionEngine. And this implementation contains property listener which allows to attach the different listeners. Listener implementations are ListTaskRunListener, ExceptionTaskRunListener, LogTaskRunListener and MessageExceptionTaskRunListener. Combination of these allows to satisfy the behavior (and actually gives me flexibility to reuse same code for other projects (; ).
Now I have put one class called TaskScheduler. This is implementation which automatically invokes the tasks. Please note that there are annotations there. These will be useful once the whole code runs within a container. TaskScheduler uses CronSequenceGenerator from spring to manage time zones. This works pretty well, see the accompanying test case.
Last there is an enumeration called TradingCloudTask. This simply enumerates the tasks I have in my cloud. In the real implementation I create instance of engine which runs TradingCloudTask. So have fun with it.
That's it for today. Next week I make an article about containers which allows to deploy the whole solution to the cloud. Thank you for reading!
No comments:
Post a Comment