Class TaskScheduler

  • All Implemented Interfaces:
    com.smartfoxserver.bitswarm.service.IService

    public class TaskScheduler
    extends java.lang.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 leverages the Java5/6 ScheduledThreadPoolExecutor in order to execute concurrent tasks using a thread pool which can be sized according to one's needs at construction times.

    In general it is recommended to use one TaskScheduler per application/Zone. You can easily maintain a Zone-based reference to the scheduler and access it not only from the Zone-level extension but also from any other Extension(s) attached to Rooms in the same Zone.

    It's also advisable to avoid spawning very large thread pools (50+ threads) unless there are specific reasons for doing so. A setting of 1-4 threads is usually sufficient to handle any number of concurrent delayed events. The only exception to this is when the tasks to be executed are long-running such as accessing a database, a remote web-service or http server etc... Finally it is possible to resize the thread pool at runtime without affecting the execution of the Tasks. ( of course with exceptions: if the number of threads is reduced drastically when running lots of concurrent tasks this could actually have an impact on the execution )

    NOTE: a runtime exception will stop the execution of the task. You should add your own try/catch block if you want to prevent this from happening, where appropriate.

    • Constructor Summary

      Constructors 
      Constructor Description
      TaskScheduler​(int threadPoolSize)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void destroy​(java.lang.Object o)
      Never accessed directly
      java.lang.String getName()
      Get the service name
      int getThreadPoolSize()
      Obtain the size of the thread pool for this Scheduler
      void handleMessage​(java.lang.Object arg0)
      Not used in this service
      void init​(java.lang.Object o)
      Never accessed directly
      void resizeThreadPool​(int threadPoolSize)
      Resize the thread pool by changing the amount of worker threads executing the tasks.
      java.util.concurrent.ScheduledFuture<?> schedule​(java.lang.Runnable task, int delay, java.util.concurrent.TimeUnit unit)
      Schedule a new task to be executed in the future, once.
      java.util.concurrent.ScheduledFuture<?> scheduleAtFixedRate​(java.lang.Runnable task, int initialDelay, int period, java.util.concurrent.TimeUnit unit)
      Schedule a new task to be executed periodically.
      void setName​(java.lang.String arg0)
      Not used in this service
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TaskScheduler

        public TaskScheduler​(int threadPoolSize)
        Parameters:
        threadPoolSize - the size of thread pool handling the Tasks
    • Method Detail

      • init

        public void init​(java.lang.Object o)
        Never accessed directly
        Specified by:
        init in interface com.smartfoxserver.bitswarm.service.IService
        Parameters:
        o - custom parameters
      • destroy

        public void destroy​(java.lang.Object o)
        Never accessed directly
        Specified by:
        destroy in interface com.smartfoxserver.bitswarm.service.IService
        Parameters:
        o - custom parameters
      • getName

        public java.lang.String getName()
        Get the service name
        Specified by:
        getName in interface com.smartfoxserver.bitswarm.service.IService
        Returns:
        the service name
      • handleMessage

        public void handleMessage​(java.lang.Object arg0)
        Not used in this service
        Specified by:
        handleMessage in interface com.smartfoxserver.bitswarm.service.IService
        Parameters:
        arg0 - the message
      • setName

        public void setName​(java.lang.String arg0)
        Not used in this service
        Specified by:
        setName in interface com.smartfoxserver.bitswarm.service.IService
        Parameters:
        arg0 - the service name
      • schedule

        public java.util.concurrent.ScheduledFuture<?> schedule​(java.lang.Runnable task,
                                                                int delay,
                                                                java.util.concurrent.TimeUnit unit)
        Schedule a new 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 unit
        Returns:
        the "future" representing the task
        See Also:
        TimeUnit, ScheduledFuture
      • scheduleAtFixedRate

        public java.util.concurrent.ScheduledFuture<?> scheduleAtFixedRate​(java.lang.Runnable task,
                                                                           int initialDelay,
                                                                           int period,
                                                                           java.util.concurrent.TimeUnit unit)
        Schedule a new task to be executed periodically. For example you might want to schedule a Task that updates all the clients with results coming from the games being played in the community. Every 20 minutes a short message would update the clients about the latest high scores and records achieved in the games.
        Parameters:
        task - the task
        initialDelay - the initial delay
        period - the interval at which the task should be executed
        unit - the time unit
        Returns:
        the "future" representing the task
        See Also:
        TimeUnit, ScheduledFuture
      • resizeThreadPool

        public void resizeThreadPool​(int threadPoolSize)
        Resize the thread pool by changing the amount of worker threads executing the tasks. Used wisely this method allows you experiment with different settings at runtime.
        Parameters:
        threadPoolSize -
      • getThreadPoolSize

        public int getThreadPoolSize()
        Obtain the size of the thread pool for this Scheduler
        Returns:
        the amount of threads in the thread pool