Harnessing the power of Quartz Scheduler in ant media server

"Only a life lived for others is a life worthwhile." – Albert Einstein

Harnessing the power of Quartz Scheduler in ant media server

Quartz scheduler is an enterprise-grade popular open-source job scheduling library for Java applications. The ant media server extends from an open-source media server framework and therefore it brings along all the goodies of the same framework with it. One such extremely useful feature is the quartz scheduler.

On its own, the quartz scheduler library can be quite overwhelming for developers who are new to it. But luckily the API is encapsulated and simplified in the context of the media server and we do not need to know every complex aspect of the quartz scheduler library in order to use it.

Here we will take a look into four encapsulated methods of the media server that help us schedule jobs/tasks using the Quartz scheduler behind the scenes. These methods are available through the main application class – AntMediaApplicationAdapter. Therefore you will always need to first access the application adapter and then access the scheduler methods through it. If you are in a plugin you will need to request the application adapter reference through the plugin instance and then you can refer to the scheduler methods.

Scheduling tasks that repeat at regular intervals


Sometimes you have a code you want to repeat every N seconds/minutes/hours. In that case, you use the addScheduledJob method. This method signature needs two parameters.

Interval - Time interval to run the scheduled job (in milliseconds)

IScheduledJob - An implementation of IScheduledJob. (A ScheduledJob object)

Example

AntMediaApplicationAdapter app = getApplication();		

app.addScheduledJob(5000, new IScheduledJob() {

	@Override
	public void execute(ISchedulingService service) throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		// executes every 5000 ms
	}
	
});

The above example will execute logic placed inside the execute method every 5000 milliseconds.

Scheduling tasks that start with an initial delay & repeat at regular intervals


Whenever you have a logic that you want to start with a delay and then repeat every N seconds/minutes/hours, you will want to use the addScheduledJobAfterDelay method. This method’s signature needs three parameters.

Interval - Time interval between consequent executions scheduled job (in milliseconds)

IScheduledJob - An implementation of IScheduledJob. (A ScheduledJob object)

Delay - The delay time in milliseconds to pass before the first execution.

Example

AntMediaApplicationAdapter app = getApplication();
app.addScheduledJobAfterDelay(5000, new IScheduledJob() {

	@Override
	public void execute(ISchedulingService service) throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		// executes every 5000 ms with a initial delay of 5000 ms for the first execution
	}
	
}, 5000);

The above example will execute logic placed inside the execute method after 5000 milliseconds initially and will repeat execution every 5000 milliseconds thereof.

Scheduling tasks that will run just once at a fixed date-time


What if you want to execute a code just once on a certain date or time? Something like this can be perfect for scheduling playlists or even restreaming IP cameras at a given date/time. For this, we have the addScheduledOnceJob method. This method asks for two parameters :

DateTime - A java DateTime object

IScheduledJob - An implementation of IScheduledJob. (A ScheduledJob object)

Example:

AntMediaApplicationAdapter app = getApplication();

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 7);

app.addScheduledOnceJob(cal.getTime(), new IScheduledJob() {

	@Override
	public void execute(ISchedulingService service) throws CloneNotSupportedException {
		// TODO Auto-generated method stub
                // executes after 7 days from today		
	}
	
});

The above example will execute logic placed inside the execute method 7 calendar days from today.

Scheduling tasks that will run just once at an offset millisecond from the current date


Finally, we come to the 4th method, which is a variation of the previous method – addScheduledOnceJob. The main difference is that instead of the first parameter being a DateTime object it is the offset in milliseconds from the current date-time to the scheduled date-time. So basically you specify how many milliseconds later in the future you want the job to be triggered

TimeOffset - Time offset in milliseconds from the current date to when given job should be run

IScheduledJob - An implementation of IScheduledJob. (A ScheduledJob object)

Example:

AntMediaApplicationAdapter app = getApplication();

Calendar cal = Calendar.getInstance();
long current_millis = cal.getTimeInMillis();

cal.add(Calendar.DATE, 7);
long future_millis = cal.getTimeInMillis();		

long diff_millis = future_millis - current_millis;

app.addScheduledOnceJob(diff_millis, new IScheduledJob() {

	@Override
	public void execute(ISchedulingService service) throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		// executes just once - after time offset in milliseconds have passed
	}
	
});

Important Note!!


If the server is restarted all the scheduled jobs are lost as they are primarily held in the memory. To workaround this you should first represent your schedules in terms of a persistent file format and store it somewhere on the server. then you should feed the scheduler using the file data.