Package com.example.server.handler
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>
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
ConstructorsConstructorDescriptionMessageQueueExecutor
(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 TypeMethodDescriptionvoid
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()
Shutdown the ThreadPoolExecutor, preventing new tasks from being accepted.
-
Constructor Details
-
MessageQueueExecutor
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 poolmaxThreads
- the maximum number of threads to allow in the poolmessageHandler
- the MessageHandler to process incoming messages
-
-
Method Details
-
add
Add a message to the queue to be processed by the ThreadPoolExecutor.- Specified by:
add
in interfaceMessageQueue<T>
- Parameters:
message
- the message to be added to the queue
-
getNextMessage
Get the next message from the queue to be processed.- Specified by:
getNextMessage
in interfaceMessageQueue<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 interfaceMessageQueue<T>
-
getCountThreads
public int getCountThreads()Get the current number of threads in the ThreadPoolExecutor.- Specified by:
getCountThreads
in interfaceMessageQueue<T>
- Returns:
- the number of threads in the thread pool
-