Repository
https://github.com/adsau59/supbot
What Will I Learn?
- What is Scheduler used for in Supbot API?
- How Scheduler works in Supbot API?
- How to use Scheduler in Supbot API?
Requirements
- Java Development Kit, IDEA intellij
- Google Chrome, Selenium Chrome Webdrivers
- Supbot API
- Natty, natural language date parser
Required Knowledge
Resources for this Tutorial:
Difficulty
Basic
✔Intermediate
Advanced
Description
Outline and Overview
In this Supbot tutorial we will take a look on Scheduler.
What is Scheduler used for?
It is used in two ways,
- Periodic Task,
Tasks which are performed on an interval, like daily, weekly, yearly tasks - Timed Task,
Tasks which are scheduled for a specific time, like reminders
How does Scheduler works?
Scheduler in Supbot API works using 3 componnets,
- SchdeuleTask
- Schedule
- ScheduleManager
ScheduleTask
ScheduleTask is an interface with 3 methods,
task()
: This method is used is executed when the schedule is arrived.taskMissed()
: This method is used is executed when the schedule is missed.getNextSchedule()
: This method is used to reschedule the date for the next schedule, can return null if the schedule is to be destroyed after running.- ScheduleTask implements
Serializable
.
Schedule
- Schedule class encapsulates ScheduleTask, the date for schedule and the timer object.
- Schedule also calculates the delay for the Schedule when the bot is launched.
ScheduleManager
ScheduleManager has two jobs,
- First is to add the schedule to the database when it is added,
- and Second is to reload the schedule back when the bot is started.
How to use Scheduler?
There are two methods to use Scheduler,
- Static Method
- Dynamic Method
Static Method
Static Method of creating Scheduler is used when you want to create the schedule yourself and has to be initialized just once.
In order to create Scheduler using static method you have to,
- Create a class which implements ScheduleTask.
- Add the class object of the class you made into ScheduleTaskInitializer using
Bot.getScheduleTaskInitializer.add()
method in theaddThingsInBot
method in the Main class. - then use
schedule create
console command to initialize the Schedule.
Dynamic Method
Dynamic Method of creating Scheduler is used when you want the clients to create the schedule or it has to be initialized multiple times.
In order to create Scheduler using dynamic method you have to,
- Create a class which implements ScheduleTask.
- Create a Schedule Object using it.
- Add it into ScheduleManager using
Bot.getScheduleManager.add()
method.
After we learn about Scheduler, we will look both of these methods in the video tutorial by creating,
- GoodMorning Scheduler, which sends good morning messege every morning, and
- RemindMe Command, which creates a reminder for the time the client specifies using
natty
API
Creating the GoodMorning Scheduler you will learn how to create Schedule using Static method and creating RemindMe Command you will learn how to create Schedule using Dynamic method.
This will create an understanding for you by which you can use both of these methods when it is required when you create your own features.
Video Tutorial
Curriculum
Contact
If you have any problems or you want to contact me on DefineX Community discord server