|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectit.gotoandplay.smartfoxserver.util.scheduling.Scheduler
public class Scheduler
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)
{
}
}
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 |
|---|
public Scheduler()
public Scheduler(long interval)
interval - the minimum thread interval| Method Detail |
|---|
public void init(java.lang.Object param)
init in interface it.gotoandplay.smartfoxserver.lib.IServicepublic void destroy(java.lang.Object param)
destroy in interface it.gotoandplay.smartfoxserver.lib.IServicepublic void startService()
public void stopService()
public void run()
run in interface java.lang.Runnable
public void addScheduledTask(Task task,
int interval,
boolean loop,
ITaskHandler callback)
task - the taskinterval - an amount of time in secondsloop - true if the task must loopcallback - the callback handlerpublic void removeScheduledTask(Task task)
task - the taskpublic boolean isRunning()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||