Class MessageQueueExecutor<T>

java.lang.Object
com.example.server.handler.MessageQueueExecutor<T>
Type Parameters:
T - the type of messages to be handled
All Implemented Interfaces:
MessageQueue<T>

public class MessageQueueExecutor<T> extends Object implements MessageQueue<T>
The MessageQueueExecutor class implements a message queue using a ThreadPoolExecutor to handle incoming messages concurrently. Messages are added to the queue and processed by a MessageHandler in separate threads.
  • Constructor Summary

    Constructors
    Constructor
    Description
    MessageQueueExecutor(int minThreads, int maxThreads, MessageHandler<T> messageHandler)
    Constructor for creating a MessageQueueExecutor instance with the given minimum and maximum threads and a MessageHandler.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(T message)
    Add a message to the queue to be processed by the ThreadPoolExecutor.
    int
    Get the current number of threads in the ThreadPoolExecutor.
    Get the next message from the queue to be processed.
    void
    Shutdown the ThreadPoolExecutor, preventing new tasks from being accepted.

    Methods inherited from class java.lang.Object

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

    • MessageQueueExecutor

      public MessageQueueExecutor(int minThreads, int maxThreads, MessageHandler<T> messageHandler)
      Constructor for creating a MessageQueueExecutor instance with the given minimum and maximum threads and a MessageHandler.
      Parameters:
      minThreads - the minimum number of threads to keep in the pool
      maxThreads - the maximum number of threads to allow in the pool
      messageHandler - the MessageHandler to process incoming messages
  • Method Details

    • add

      public void add(T message)
      Add a message to the queue to be processed by the ThreadPoolExecutor.
      Specified by:
      add in interface MessageQueue<T>
      Parameters:
      message - the message to be added to the queue
    • getNextMessage

      public T getNextMessage()
      Get the next message from the queue to be processed.
      Specified by:
      getNextMessage in interface MessageQueue<T>
      Returns:
      the next message in the queue
    • shutdown

      public void shutdown()
      Shutdown the ThreadPoolExecutor, preventing new tasks from being accepted.
      Specified by:
      shutdown in interface MessageQueue<T>
    • getCountThreads

      public int getCountThreads()
      Get the current number of threads in the ThreadPoolExecutor.
      Specified by:
      getCountThreads in interface MessageQueue<T>
      Returns:
      the number of threads in the thread pool