Class TaskScheduler

java.lang.Object
com.smartfoxserver.util.TaskScheduler
All Implemented Interfaces:
com.smartfoxserver.bitswarm.service.IService

public class TaskScheduler extends Object implements com.smartfoxserver.bitswarm.service.IService

Overview

The TaskScheduler class allows to handle multiple interval-based tasks or delayed tasks in an efficient and convenient way. The class uses the Java's ScheduledThreadPoolExecutor backed by virtual threads to manage concurrent tasks.

In SmartFoxServer 3 all Extension's task should be submitted to the main TaskScheduler, there's no need to create new ones.

NOTE: Exceptions can stop the execution of the task silently. Always wrap your Task code inside a try/catch block. Example:


 getApi().getScheduler().schedule(() -> {
 
 	try
 	{
 		// Task logic here...
 	}
 	catch(Exception ex)
 	{
 		trace("An error occurred: ", ex.toString());
 	}
 	
 }, 2, TimeUnit.SECONDS); 
 
  • Constructor Details

    • TaskScheduler

      public TaskScheduler(int threadPoolSize)
      Instead of creating a new instance use the API's scheduler
      getApi().getScheduler(); 
      Internal
  • Method Details

    • init

      public void init(Object o)
      Specified by:
      init in interface com.smartfoxserver.bitswarm.service.IService
      Internal
    • destroy

      public void destroy(Object o)
      Specified by:
      destroy in interface com.smartfoxserver.bitswarm.service.IService
      Internal
    • getName

      public String getName()
      Specified by:
      getName in interface com.smartfoxserver.bitswarm.service.IService
      Internal
    • handleMessage

      public void handleMessage(Object params)
      Specified by:
      handleMessage in interface com.smartfoxserver.bitswarm.service.IService
      Internal
    • setName

      public void setName(String name)
      Specified by:
      setName in interface com.smartfoxserver.bitswarm.service.IService
      Internal
    • schedule

      public ScheduledFuture<?> schedule(Runnable task, int delay, TimeUnit unit)
      Schedule a Task to be executed in the future, once.
      Parameters:
      task - the task to be executed
      delay - the amount of delay before the execution
      unit - the time scale
      Returns:
      the ScheduledFuture representing the task
      See Also:
    • scheduleAtFixedRate

      public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, int initialDelay, int period, TimeUnit unit)
      Schedule Task to be executed periodically.
      Parameters:
      task - the task
      initialDelay - the initial delay
      period - the interval at which the task should be executed
      unit - the time unit
      Returns:
      the ScheduledFuture representing the task
      See Also: