it.gotoandplay.smartfoxserver.util.scheduling
Class Scheduler

java.lang.Object
  extended by it.gotoandplay.smartfoxserver.util.scheduling.Scheduler
All Implemented Interfaces:
it.gotoandplay.smartfoxserver.lib.IService, java.lang.Runnable

public class Scheduler
extends java.lang.Object
implements it.gotoandplay.smartfoxserver.lib.IService, java.lang.Runnable

Task Scheduler class
Handles multiple timed tasks and providing a simple call-back mechanism to notify when it's time to execute the job.
You can add any number of timed tasks to execute.
Behind the scenes the Scheduler uses 1 single thread to handle multiple timed jobs.

Example of usage:

 
package it.gotoandplay.smartfoxserver.extensions.examples;

import java.util.HashMap;
import java.util.Map;

import it.gotoandplay.smartfoxserver.data.User;
import it.gotoandplay.smartfoxserver.events.InternalEventObject;
import it.gotoandplay.smartfoxserver.extensions.AbstractExtension;
import it.gotoandplay.smartfoxserver.lib.ActionscriptObject;
import it.gotoandplay.smartfoxserver.util.scheduling.ITaskHandler;
import it.gotoandplay.smartfoxserver.util.scheduling.Scheduler;
import it.gotoandplay.smartfoxserver.util.scheduling.Task;

public class SchedulerExample extends AbstractExtension
{
         //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", "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...
        }


        public void destroy()
        {
                // Remember to stop the scheduler, in order to release its resources
                scheduler.stopService();
            trace("Scheduler Example Stops");
        }

        public void handleRequest(String cmd, String[] params, User u, int fromRoom)
        {
        }

        public void handleRequest(String cmd, ActionscriptObject ao, User u,int fromRoom)
        {
        }

        public void handleInternalEvent(InternalEventObject ieo)
        {
        }
}
 
 

Since:
1.6.0
See Also:
Task, ITaskHandler

Constructor Summary
Scheduler()
          Default constructor
Scheduler(long interval)
          Constructor with additional parameter Allows to specify the minimum time interval at which the Scheduler checks the list of jobs to execute.
 
Method Summary
 void addScheduledTask(Task task, int interval, boolean loop, ITaskHandler callback)
          Add a new scheduled task
 void destroy(java.lang.Object param)
          Destroy the Scheduler and release resources.
 void init(java.lang.Object param)
          Initializes the object.
 boolean isRunning()
           
 void removeScheduledTask(Task task)
          Deprecated.  
 void run()
           
 void startService()
          Starts the service
 void stopService()
          Stops the service.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Scheduler

public Scheduler()
Default constructor


Scheduler

public Scheduler(long interval)
Constructor with additional parameter Allows to specify the minimum time interval at which the Scheduler checks the list of jobs to execute. Example: if you plan that your faster interval will be > 30 seconds then you can set the interval to that value and avoid fast thread switching

Parameters:
interval - the minimum thread interval
Method Detail

init

public void init(java.lang.Object param)
Initializes the object. This method is already called by the constructor.

Specified by:
init in interface it.gotoandplay.smartfoxserver.lib.IService

destroy

public void destroy(java.lang.Object param)
Destroy the Scheduler and release resources.
Always use this method when the object is not needed anymore

Specified by:
destroy in interface it.gotoandplay.smartfoxserver.lib.IService

startService

public void startService()
Starts the service


stopService

public void stopService()
Stops the service.


run

public void run()
Specified by:
run in interface java.lang.Runnable

addScheduledTask

public void addScheduledTask(Task task,
                             int interval,
                             boolean loop,
                             ITaskHandler callback)
Add a new scheduled task

Parameters:
task - the task
interval - an amount of time in seconds
loop - true if the task must loop
callback - the callback handler

removeScheduledTask

public void removeScheduledTask(Task task)
Deprecated. 

Removes a scheduled task.
Instead of using this method, you can remove a task by simply setting its active flag to false.
This will mark the Task as inactive and ready for deletion inside the Scheduler process list.

Parameters:
task - the task

isRunning

public boolean isRunning()