Class QueueService


  • @ThreadSafe
    public class QueueService
    extends java.lang.Object
    A service for managing start, pause and shutdown of task processors.
    Since:
    14.07.2017
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.List<QueueId> awaitTermination​(java.time.Duration timeout)
      Wait for tasks (and threads) termination in all queues within given timeout.
      java.util.List<QueueShardId> awaitTermination​(QueueId queueId, java.time.Duration timeout)
      Wait for tasks (and threads) termination in specified queue within given timeout.
      boolean isPaused()
      Get attribute that all queues were paused with pause() method.
      boolean isPaused​(QueueId queueId)
      Get attribute that the specified queue were paused with pause(QueueId) method.
      boolean isShutdown()
      Get attribute that the tasks processing was stopped in all registered queues with shutdown().
      boolean isShutdown​(QueueId queueId)
      Get attribute that the tasks processing was stopped in one specific queue with shutdown() method.
      boolean isTerminated()
      Get attribute that all queues finished their execution and all task threads were terminated.
      boolean isTerminated​(QueueId queueId)
      Get attribute that all the processing task threads were successfully terminated in the specified queue.
      void pause()
      Pause task processing in all queues.
      void pause​(QueueId queueId)
      Pause task processing in specified queue.
      <PayloadT> boolean registerQueue​(QueueConsumer<PayloadT> consumer)
      Register new task processor of given payload type.
      void shutdown()
      Stop tasks processing in all queues registered in the service, semantic is the same as for ExecutorService.shutdownNow().
      void shutdown​(QueueId queueId)
      Stop tasks processing in one given queue, semantic is the same as for ExecutorService.shutdownNow().
      void start()
      Start tasks processing in all queues registered in the service.
      void start​(QueueId queueId)
      Start tasks processing in one given queue.
      void unpause()
      Continue task processing in all queues.
      void unpause​(QueueId queueId)
      Continue task processing in specified queue.
      java.util.Map<QueueId,​java.lang.String> updateQueueConfigs​(java.util.Collection<QueueConfig> configs)
      Update queue configurations.
      void wakeup​(QueueId queueId, QueueShardId queueShardId)
      Force continue task processing in specified queue by given shard identifier.
      • Methods inherited from class java.lang.Object

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

      • registerQueue

        public <PayloadT> boolean registerQueue​(@Nonnull
                                                QueueConsumer<PayloadT> consumer)
        Register new task processor of given payload type.
        Type Parameters:
        PayloadT - Type of the processor (type of the payload in the task).
        Parameters:
        consumer - Task processor.
        Returns:
        Attribute of successful task processor registration.
      • start

        public void start()
        Start tasks processing in all queues registered in the service.
      • start

        public void start​(@Nonnull
                          QueueId queueId)
        Start tasks processing in one given queue.
        Parameters:
        queueId - Queue identifier.
      • shutdown

        public void shutdown()
        Stop tasks processing in all queues registered in the service, semantic is the same as for ExecutorService.shutdownNow().
      • shutdown

        public void shutdown​(@Nonnull
                             QueueId queueId)
        Stop tasks processing in one given queue, semantic is the same as for ExecutorService.shutdownNow().
        Parameters:
        queueId - Queue identifier.
      • isShutdown

        public boolean isShutdown​(@Nonnull
                                  QueueId queueId)
        Get attribute that the tasks processing was stopped in one specific queue with shutdown() method. Semantic is the same as for ExecutorService.isShutdown().
        Parameters:
        queueId - Queue identifier.
        Returns:
        true if the tasks processing was stopped.
      • isShutdown

        public boolean isShutdown()
        Get attribute that the tasks processing was stopped in all registered queues with shutdown(). Semantic is the same as for ExecutorService.isShutdown().
        Returns:
        true if the tasks processing was stopped.
      • isTerminated

        public boolean isTerminated​(@Nonnull
                                    QueueId queueId)
        Get attribute that all the processing task threads were successfully terminated in the specified queue. Semantic is the same as for ExecutorService.isTerminated().
        Parameters:
        queueId - Queue identifier.
        Returns:
        true if all the task threads were terminated in specified queue.
      • isTerminated

        public boolean isTerminated()
        Get attribute that all queues finished their execution and all task threads were terminated. Semantic is the same as for ExecutorService.isTerminated().
        Returns:
        true if all task threads in all queues were terminated.
      • pause

        public void pause​(@Nonnull
                          QueueId queueId)
        Pause task processing in specified queue. To start the processing again, use {unpause(QueueId) method.
        Parameters:
        queueId - Queue identifier.
      • pause

        public void pause()
        Pause task processing in all queues. To start processing, use unpause() method.
      • unpause

        public void unpause​(@Nonnull
                            QueueId queueId)
        Continue task processing in specified queue. To pause processing, use {pause(QueueId) method.
        Parameters:
        queueId - Queue identifier.
      • unpause

        public void unpause()
        Continue task processing in all queues. To pause processing, use pause() method.
      • isPaused

        public boolean isPaused()
        Get attribute that all queues were paused with pause() method.
        Returns:
        true if queues were paused.
      • isPaused

        public boolean isPaused​(@Nonnull
                                QueueId queueId)
        Get attribute that the specified queue were paused with pause(QueueId) method.
        Parameters:
        queueId - Queue identifier.
        Returns:
        true if specified queue were paused.
      • awaitTermination

        public java.util.List<QueueId> awaitTermination​(@Nonnull
                                                        java.time.Duration timeout)
        Wait for tasks (and threads) termination in all queues within given timeout. Semantic is the same as for ExecutorService.awaitTermination(long, TimeUnit).
        Parameters:
        timeout - Wait timeout.
        Returns:
        List of queues, which didn't stop their work (didn't terminate).
      • awaitTermination

        public java.util.List<QueueShardId> awaitTermination​(@Nonnull
                                                             QueueId queueId,
                                                             @Nonnull
                                                             java.time.Duration timeout)
        Wait for tasks (and threads) termination in specified queue within given timeout. Semantic is the same as for ExecutorService.awaitTermination(long, TimeUnit).
        Parameters:
        queueId - Queue identifier.
        timeout - Wait timeout.
        Returns:
        List of shards, where the work didn't stop (working threads on which were not terminated).
      • wakeup

        public void wakeup​(@Nonnull
                           QueueId queueId,
                           @Nonnull
                           QueueShardId queueShardId)
        Force continue task processing in specified queue by given shard identifier.

        Processing continues only if the queue were paused with QueueConfigsReader.SETTING_NO_TASK_TIMEOUT event.

        It might be useful for queues which interact with the end user, whereas the end users might often expect possibly the quickest response on their actions. Applies right after a task enqueue, therefore should be called only after successful task insertion transaction. Applies also to tests to improve the speed of test execution.

        Parameters:
        queueId - Queue identifier.
        queueShardId - Shard identifier.