6.x Java Extensions CookBook

This document provides quick snippets of code organized by theme that will get you started with most of the server side coding tasks. Feel free to suggest more "recipes" by sending us an email or posting in our support forums

» Scheduler Recipes

The Scheduler object can handle multiple timed tasks providing a simple callback mechanism that notifies when it's time to execute the job. You can add any number of timed tasks to execute. Behind the scenes the Scheduler uses a single thread to handle multiple timed jobs.

NOTE: The Scheduler class is designed to handle multiple task with a minimum resolution of around 800-1000 milliseconds (~ 1 sec) or higher. Typical use cases can be any timed tasks, game timers, count downs etc... If you need to run very fast jobs with intervals of a few milliseconds then it is more appropriate to use a dedicate thread, a thread pool or the Executor classes from Java 1.5 and higher.

Recipe #1:

This is an example of various timers running concurrently within the same Scheduler object.

//This is the class that will handle the callbacks from the Scheduler
class MyTaskHandler implements ITaskHandler
{
        int loopCount = 0;

        public void doTask(Task task) throws Exception
        {
                String id = (String) task.id;
                String message = "Handling Task { " + id + " }";

                if (id.equals("looping"))
                {
                        loopCount++;
                        message += ", Loop count: " + loopCount;

                        if (loopCount == 5)
                        {
                                message += ", Stopping Loop!";

                                // This will stop the execution of the looping task
                                task.active = false;
                        }
                }

                else if (id.equals("param"))
                {
                        message += ", Passed params: " + task.parameters;
                }

                System.out.println(message);

        }
}

Scheduler scheduler;
ITaskHandler handler;

public void init()
{
        trace("Scheduler Example Starts");

        // Create an instance of the scheduler
        scheduler = new Scheduler();

        // Create and instance of the callback handler
        handler = new MyTaskHandler();

        // Create a new task
        Task loopingTaks = new Task("looping");

        // Create a new task
        Task normalTask = new Task("normal");

        // Create a map of parameters that we'll add to the 3rd task
        Map params = new HashMap();
        params.put("Italy", "Europe");
        params.put("Canada", "North America");
        params.put("China", "Asia");

        // create a new task with the additional parameters
        Task paramTask = new Task("param", params);

        // Add tasks to the scheduler
        scheduler.addScheduledTask(loopingTaks, 3, true, handler);
        scheduler.addScheduledTask(normalTask, 5, false, handler);
        scheduler.addScheduledTask(paramTask, 2, false, handler);

        // Start the scheduler
        scheduler.startService();

        //... you can keep adding more tasks at runtime...
}

 


doc index