mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 08:58:26 +00:00
refactor: rename queue based load leveling
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
---
|
||||
title: Queue-Based Load Leveling
|
||||
category: Resilience
|
||||
language: en
|
||||
tag:
|
||||
- Asynchronous
|
||||
- Buffering
|
||||
- Decoupling
|
||||
- Fault tolerance
|
||||
- Messaging
|
||||
- Scalability
|
||||
- Synchronization
|
||||
- Thread management
|
||||
---
|
||||
|
||||
## Also known as
|
||||
|
||||
* Load Leveling
|
||||
* Message Queuing
|
||||
|
||||
## Intent
|
||||
|
||||
Queue-Based Load Leveling aims to manage the load in a system by using a queue to level the workload between producers and consumers, ensuring that heavy loads are handled smoothly without overwhelming the system.
|
||||
|
||||
## Explanation
|
||||
|
||||
Real-world example
|
||||
|
||||
> Imagine a popular restaurant with a limited number of kitchen staff (consumers) and a large number of customers placing orders (producers). During peak hours, if all customers were served immediately, the kitchen would be overwhelmed, leading to long wait times and potential mistakes in orders. To manage this, the restaurant implements a queue-based load leveling system using a ticketing machine.
|
||||
>
|
||||
> When customers place orders, they receive a ticket number and their order is placed in a queue. The kitchen staff then processes orders one at a time in the order they were received. This ensures that the kitchen can handle the workload at a manageable pace, preventing overload and maintaining service quality. Customers wait comfortably knowing their order is in line and will be handled efficiently, even during the busiest times.
|
||||
|
||||
In plain words
|
||||
|
||||
> Queue-Based Load Leveling is a design pattern that uses a queue to manage and balance the workload between producers and consumers, preventing system overload and ensuring smooth processing.
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> Message Queues are essential components for inter-process communication (IPC) and inter-thread communication, using queues to manage the passing of messages. They help in decoupling producers and consumers, allowing asynchronous processing, which is a key aspect of the Queue-Based Load Leveling pattern.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
The Queue-Based Load Leveling pattern helps to manage high-volume, sporadic bursts of tasks that can overwhelm a system. It uses a queue as a buffer to hold tasks, decoupling the task generation from task processing. The tasks are then processed at a manageable rate.
|
||||
|
||||
First, let's look at the `MessageQueue` and `Message` classes. The `MessageQueue` acts as a buffer, storing messages until they are retrieved by the `ServiceExecutor`. The `Message` represents the tasks to be processed.
|
||||
|
||||
```java
|
||||
public class Message {
|
||||
// Message details
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class MessageQueue {
|
||||
private Queue<Message> queue;
|
||||
|
||||
public MessageQueue() {
|
||||
queue = new LinkedList<>();
|
||||
}
|
||||
|
||||
// Method to add a message to the queue
|
||||
public void addMessage(Message message) {
|
||||
queue.add(message);
|
||||
}
|
||||
|
||||
// Method to retrieve a message from the queue
|
||||
public Message getMessage() {
|
||||
return queue.poll();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Next, we have the `TaskGenerator` class. This class represents the task producers. It generates tasks and submits them to the `MessageQueue`.
|
||||
|
||||
```java
|
||||
public class TaskGenerator implements Runnable {
|
||||
|
||||
private MessageQueue msgQueue;
|
||||
private int taskCount;
|
||||
|
||||
public TaskGenerator(MessageQueue msgQueue, int taskCount) {
|
||||
this.msgQueue = msgQueue;
|
||||
this.taskCount = taskCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (int i = 0; i < taskCount; i++) {
|
||||
Message message = new Message(); // Create a new message
|
||||
msgQueue.addMessage(message); // Add the message to the queue
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The `ServiceExecutor` class represents the task consumer. It retrieves tasks from the `MessageQueue` and processes them.
|
||||
|
||||
```java
|
||||
public class ServiceExecutor implements Runnable {
|
||||
|
||||
private MessageQueue msgQueue;
|
||||
|
||||
public ServiceExecutor(MessageQueue msgQueue) {
|
||||
this.msgQueue = msgQueue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
Message message = msgQueue.getMessage(); // Retrieve a message from the queue
|
||||
if (message != null) {
|
||||
// Process the message
|
||||
} else {
|
||||
// No more messages to process
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Finally, we have the `App` class which sets up the `TaskGenerator` and `ServiceExecutor` threads and submits them to an `ExecutorService`.
|
||||
|
||||
```java
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
var msgQueue = new MessageQueue();
|
||||
|
||||
final var taskRunnable1 = new TaskGenerator(msgQueue, 5);
|
||||
final var taskRunnable2 = new TaskGenerator(msgQueue, 1);
|
||||
final var taskRunnable3 = new TaskGenerator(msgQueue, 2);
|
||||
|
||||
final var srvRunnable = new ServiceExecutor(msgQueue);
|
||||
|
||||
ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||
executor.submit(taskRunnable1);
|
||||
executor.submit(taskRunnable2);
|
||||
executor.submit(taskRunnable3);
|
||||
executor.submit(srvRunnable);
|
||||
|
||||
executor.shutdown();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In this example, the `TaskGenerator` threads generate tasks at a variable rate and submit them to the `MessageQueue`. The `ServiceExecutor` retrieves the tasks from the queue and processes them at its own pace, preventing the system from being overwhelmed by peak loads.
|
||||
|
||||
Running the application produces the following console output:
|
||||
|
||||
```
|
||||
[main] INFO App - Submitting TaskGenerators and ServiceExecutor threads.
|
||||
[main] INFO App - Initiating shutdown. Executor will shutdown only after all the Threads are completed.
|
||||
[pool-1-thread-2] INFO TaskGenerator - Message-1 submitted by pool-1-thread-2
|
||||
[pool-1-thread-1] INFO TaskGenerator - Message-5 submitted by pool-1-thread-1
|
||||
[pool-1-thread-1] INFO TaskGenerator - Message-4 submitted by pool-1-thread-1
|
||||
[pool-1-thread-2] INFO TaskGenerator - Message-2 submitted by pool-1-thread-2
|
||||
[pool-1-thread-1] INFO TaskGenerator - Message-3 submitted by pool-1-thread-1
|
||||
[pool-1-thread-2] INFO TaskGenerator - Message-1 submitted by pool-1-thread-2
|
||||
[pool-1-thread-1] INFO TaskGenerator - Message-2 submitted by pool-1-thread-1
|
||||
[pool-1-thread-2] INFO ServiceExecutor - Message-1 submitted by pool-1-thread-2 is served.
|
||||
[pool-1-thread-1] INFO TaskGenerator - Message-1 submitted by pool-1-thread-1
|
||||
[pool-1-thread-2] INFO ServiceExecutor - Message-5 submitted by pool-1-thread-1 is served.
|
||||
[pool-1-thread-2] INFO ServiceExecutor - Message-4 submitted by pool-1-thread-1 is served.
|
||||
[pool-1-thread-2] INFO ServiceExecutor - Message-2 submitted by pool-1-thread-2 is served.
|
||||
[pool-1-thread-2] INFO ServiceExecutor - Message-3 submitted by pool-1-thread-1 is served.
|
||||
[pool-1-thread-2] INFO ServiceExecutor - Message-1 submitted by pool-1-thread-2 is served.
|
||||
[pool-1-thread-2] INFO ServiceExecutor - Message-2 submitted by pool-1-thread-1 is served.
|
||||
[pool-1-thread-2] INFO ServiceExecutor - Message-1 submitted by pool-1-thread-1 is served.
|
||||
[pool-1-thread-2] INFO ServiceExecutor - Service Executor: Waiting for Messages to serve ..
|
||||
[pool-1-thread-2] INFO ServiceExecutor - Service Executor: Waiting for Messages to serve ..
|
||||
[pool-1-thread-2] INFO ServiceExecutor - Service Executor: Waiting for Messages to serve ..
|
||||
[pool-1-thread-2] INFO ServiceExecutor - Service Executor: Waiting for Messages to serve ..
|
||||
[main] INFO App - Executor was shut down and Exiting.
|
||||
[pool-1-thread-2] ERROR ServiceExecutor - sleep interrupted
|
||||
```
|
||||
|
||||
## Class diagram
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
|
||||
* When there are variable workloads, and you need to ensure that peak loads do not overwhelm the system
|
||||
* In distributed systems where tasks are produced at a different rate than they are consumed
|
||||
* For decoupling producers and consumers in an asynchronous messaging system
|
||||
|
||||
## Known Uses
|
||||
|
||||
* Amazon Web Services (AWS) Simple Queue Service (SQS)
|
||||
* RabbitMQ
|
||||
* Java Message Service (JMS) in enterprise Java applications
|
||||
|
||||
## Consequences
|
||||
|
||||
Benefits:
|
||||
|
||||
* Decouples the producers and consumers, allowing each to operate at its own pace
|
||||
* Increases system resilience and fault tolerance by preventing overload conditions
|
||||
* Enhances scalability by allowing more consumers to be added to handle increased load
|
||||
|
||||
Trade-offs:
|
||||
|
||||
* Adds complexity to the system architecture
|
||||
* May introduce latency as messages need to be queued and dequeued
|
||||
* Requires additional components (queues) to be managed and monitored
|
||||
|
||||
## Related Patterns
|
||||
|
||||
* Asynchronous Messaging: Queue-Based Load Leveling uses asynchronous messaging to decouple producers and consumers
|
||||
* [Circuit Breaker](https://java-design-patterns.com/patterns/circuit-breaker/): Often used in conjunction with Queue-Based Load Leveling to prevent system overloads by temporarily halting message processing
|
||||
* [Producer-Consumer](https://java-design-patterns.com/patterns/producer-consumer/): Queue-Based Load Leveling is a specific application of the Producer-Consumer pattern where the queue serves as the intermediary
|
||||
* [Retry](https://java-design-patterns.com/patterns/retry/): Works with Queue-Based Load Leveling to handle transient failures by retrying failed operations
|
||||
|
||||
## Credits
|
||||
|
||||
* [Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems](https://amzn.to/3y6yv1z)
|
||||
* [Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions](https://amzn.to/3WcFVui)
|
||||
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
|
||||
* [Queue-Based Load Leveling - Microsoft](https://docs.microsoft.com/en-us/azure/architecture/patterns/queue-based-load-leveling)
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
@@ -0,0 +1,86 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.11" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
|
||||
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
|
||||
<class id="1" language="java" name="org.queue.load.leveling.TaskGenerator" project="queue-load-leveling"
|
||||
file="/queue-load-leveling/src/main/java/org/queue/load/leveling/TaskGenerator.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="470" y="213"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<interface id="2" language="java" name="org.queue.load.leveling.Task" project="queue-load-leveling"
|
||||
file="/queue-load-leveling/src/main/java/org/queue/load/leveling/Task.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="426" y="389"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<class id="3" language="java" name="org.queue.load.leveling.MessageQueue" project="queue-load-leveling"
|
||||
file="/queue-load-leveling/src/main/java/org/queue/load/leveling/MessageQueue.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="661" y="419"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="4" language="java" name="org.queue.load.leveling.Message" project="queue-load-leveling"
|
||||
file="/queue-load-leveling/src/main/java/org/queue/load/leveling/Message.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="594" y="657"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="5" language="java" name="org.queue.load.leveling.ServiceExecutor" project="queue-load-leveling"
|
||||
file="/queue-load-leveling/src/main/java/org/queue/load/leveling/ServiceExecutor.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="775" y="193"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<realization id="6">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="2"/>
|
||||
</realization>
|
||||
<association id="7">
|
||||
<end type="SOURCE" refId="1" navigable="false">
|
||||
<attribute id="8" name="msgQueue"/>
|
||||
<multiplicity id="9" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="3" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="10">
|
||||
<end type="SOURCE" refId="3" navigable="false">
|
||||
<attribute id="11" name="blkQueue"/>
|
||||
<multiplicity id="12" minimum="0" maximum="2147483647"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="4" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="13">
|
||||
<end type="SOURCE" refId="5" navigable="false">
|
||||
<attribute id="14" name="msgQueue"/>
|
||||
<multiplicity id="15" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="3" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
||||
@@ -0,0 +1,44 @@
|
||||
@startuml
|
||||
package com.iluwatar.queue.load.leveling {
|
||||
class App {
|
||||
- LOGGER : Logger {static}
|
||||
- SHUTDOWN_TIME : int {static}
|
||||
+ App()
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
class Message {
|
||||
- msg : String
|
||||
+ Message(msg : String)
|
||||
+ getMsg() : String
|
||||
+ toString() : String
|
||||
}
|
||||
class MessageQueue {
|
||||
- LOGGER : Logger {static}
|
||||
- blkQueue : BlockingQueue<Message>
|
||||
+ MessageQueue()
|
||||
+ retrieveMsg() : Message
|
||||
+ submitMsg(msg : Message)
|
||||
}
|
||||
class ServiceExecutor {
|
||||
- LOGGER : Logger {static}
|
||||
- msgQueue : MessageQueue
|
||||
+ ServiceExecutor(msgQueue : MessageQueue)
|
||||
+ run()
|
||||
}
|
||||
interface Task {
|
||||
+ submit(Message) {abstract}
|
||||
}
|
||||
class TaskGenerator {
|
||||
- LOGGER : Logger {static}
|
||||
- msgCount : int
|
||||
- msgQueue : MessageQueue
|
||||
+ TaskGenerator(msgQueue : MessageQueue, msgCount : int)
|
||||
+ run()
|
||||
+ submit(msg : Message)
|
||||
}
|
||||
}
|
||||
MessageQueue --> "-blkQueue" Message
|
||||
ServiceExecutor --> "-msgQueue" MessageQueue
|
||||
TaskGenerator --> "-msgQueue" MessageQueue
|
||||
TaskGenerator ..|> Task
|
||||
@enduml
|
||||
@@ -0,0 +1,62 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
|
||||
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
|
||||
The MIT License
|
||||
Copyright © 2014-2022 Ilkka Seppälä
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.26.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>queue-based-load-leveling</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.iluwatar.queue.load.leveling.App</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.queue.load.leveling;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Many solutions in the cloud involve running tasks that invoke services. In this environment, if a
|
||||
* service is subjected to intermittent heavy loads, it can cause performance or reliability
|
||||
* issues.
|
||||
*
|
||||
* <p>A service could be a component that is part of the same solution as the tasks that utilize
|
||||
* it, or it could be a third-party service providing access to frequently used resources such as a
|
||||
* cache or a storage service. If the same service is utilized by a number of tasks running
|
||||
* concurrently, it can be difficult to predict the volume of requests to which the service might be
|
||||
* subjected at any given point in time.
|
||||
*
|
||||
* <p>We will build a queue-based-load-leveling to solve above problem. Refactor the solution and
|
||||
* introduce a queue between the task and the service. The task and the service run asynchronously.
|
||||
* The task posts a message containing the data required by the service to a queue. The queue acts
|
||||
* as a buffer, storing the message until it is retrieved by the service. The service retrieves the
|
||||
* messages from the queue and processes them. Requests from a number of tasks, which can be
|
||||
* generated at a highly variable rate, can be passed to the service through the same message
|
||||
* queue.
|
||||
*
|
||||
* <p>The queue effectively decouples the tasks from the service, and the service can handle the
|
||||
* messages at its own pace irrespective of the volume of requests from concurrent tasks.
|
||||
* Additionally, there is no delay to a task if the service is not available at the time it posts a
|
||||
* message to the queue.
|
||||
*
|
||||
* <p>In this example we have a class {@link MessageQueue} to hold the message {@link Message}
|
||||
* objects. All the worker threads {@link TaskGenerator} will submit the messages to the
|
||||
* MessageQueue. The service executor class {@link ServiceExecutor} will pick up one task at a time
|
||||
* from the Queue and execute them.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
//Executor shut down time limit.
|
||||
private static final int SHUTDOWN_TIME = 15;
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
// An Executor that provides methods to manage termination and methods that can
|
||||
// produce a Future for tracking progress of one or more asynchronous tasks.
|
||||
ExecutorService executor = null;
|
||||
|
||||
try {
|
||||
// Create a MessageQueue object.
|
||||
var msgQueue = new MessageQueue();
|
||||
|
||||
LOGGER.info("Submitting TaskGenerators and ServiceExecutor threads.");
|
||||
|
||||
// Create three TaskGenerator threads. Each of them will submit different number of jobs.
|
||||
final var taskRunnable1 = new TaskGenerator(msgQueue, 5);
|
||||
final var taskRunnable2 = new TaskGenerator(msgQueue, 1);
|
||||
final var taskRunnable3 = new TaskGenerator(msgQueue, 2);
|
||||
|
||||
// Create e service which should process the submitted jobs.
|
||||
final var srvRunnable = new ServiceExecutor(msgQueue);
|
||||
|
||||
// Create a ThreadPool of 2 threads and
|
||||
// submit all Runnable task for execution to executor
|
||||
executor = Executors.newFixedThreadPool(2);
|
||||
executor.submit(taskRunnable1);
|
||||
executor.submit(taskRunnable2);
|
||||
executor.submit(taskRunnable3);
|
||||
|
||||
// submitting serviceExecutor thread to the Executor service.
|
||||
executor.submit(srvRunnable);
|
||||
|
||||
// Initiates an orderly shutdown.
|
||||
LOGGER.info("Initiating shutdown."
|
||||
+ " Executor will shutdown only after all the Threads are completed.");
|
||||
executor.shutdown();
|
||||
|
||||
// Wait for SHUTDOWN_TIME seconds for all the threads to complete
|
||||
// their tasks and then shut down the executor and then exit.
|
||||
if (!executor.awaitTermination(SHUTDOWN_TIME, TimeUnit.SECONDS)) {
|
||||
LOGGER.info("Executor was shut down and Exiting.");
|
||||
executor.shutdownNow();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.queue.load.leveling;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* Message class with only one parameter.
|
||||
*/
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class Message {
|
||||
private final String msg;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.queue.load.leveling;
|
||||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* MessageQueue class. In this class we will create a Blocking Queue and submit/retrieve all the
|
||||
* messages from it.
|
||||
*/
|
||||
@Slf4j
|
||||
public class MessageQueue {
|
||||
|
||||
private final BlockingQueue<Message> blkQueue;
|
||||
|
||||
// Default constructor when called creates Blocking Queue object.
|
||||
public MessageQueue() {
|
||||
this.blkQueue = new ArrayBlockingQueue<>(1024);
|
||||
}
|
||||
|
||||
/**
|
||||
* All the TaskGenerator threads will call this method to insert the Messages in to the Blocking
|
||||
* Queue.
|
||||
*/
|
||||
public void submitMsg(Message msg) {
|
||||
try {
|
||||
if (null != msg) {
|
||||
blkQueue.add(msg);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* All the messages will be retrieved by the ServiceExecutor by calling this method and process
|
||||
* them. Retrieves and removes the head of this queue, or returns null if this queue is empty.
|
||||
*/
|
||||
public Message retrieveMsg() {
|
||||
try {
|
||||
return blkQueue.poll();
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.queue.load.leveling;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* ServiceExecuotr class. This class will pick up Messages one by one from the Blocking Queue and
|
||||
* process them.
|
||||
*/
|
||||
@Slf4j
|
||||
public class ServiceExecutor implements Runnable {
|
||||
|
||||
private final MessageQueue msgQueue;
|
||||
|
||||
public ServiceExecutor(MessageQueue msgQueue) {
|
||||
this.msgQueue = msgQueue;
|
||||
}
|
||||
|
||||
/**
|
||||
* The ServiceExecutor thread will retrieve each message and process it.
|
||||
*/
|
||||
public void run() {
|
||||
try {
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
var msg = msgQueue.retrieveMsg();
|
||||
|
||||
if (null != msg) {
|
||||
LOGGER.info(msg + " is served.");
|
||||
} else {
|
||||
LOGGER.info("Service Executor: Waiting for Messages to serve .. ");
|
||||
}
|
||||
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.queue.load.leveling;
|
||||
|
||||
/**
|
||||
* Task Interface.
|
||||
*/
|
||||
public interface Task {
|
||||
void submit(Message msg);
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.queue.load.leveling;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* TaskGenerator class. Each TaskGenerator thread will be a Worker which submits messages to the
|
||||
* queue. We need to mention the message count for each of the TaskGenerator threads.
|
||||
*/
|
||||
@Slf4j
|
||||
public class TaskGenerator implements Task, Runnable {
|
||||
|
||||
// MessageQueue reference using which we will submit our messages.
|
||||
private final MessageQueue msgQueue;
|
||||
|
||||
// Total message count that a TaskGenerator will submit.
|
||||
private final int msgCount;
|
||||
|
||||
// Parameterized constructor.
|
||||
public TaskGenerator(MessageQueue msgQueue, int msgCount) {
|
||||
this.msgQueue = msgQueue;
|
||||
this.msgCount = msgCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit messages to the Blocking Queue.
|
||||
*/
|
||||
public void submit(Message msg) {
|
||||
try {
|
||||
this.msgQueue.submitMsg(msg);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Each TaskGenerator thread will submit all the messages to the Queue. After every message
|
||||
* submission TaskGenerator thread will sleep for 1 second.
|
||||
*/
|
||||
public void run() {
|
||||
var count = this.msgCount;
|
||||
|
||||
try {
|
||||
while (count > 0) {
|
||||
var statusMsg = "Message-" + count + " submitted by " + Thread.currentThread().getName();
|
||||
this.submit(new Message(statusMsg));
|
||||
|
||||
LOGGER.info(statusMsg);
|
||||
|
||||
// reduce the message count.
|
||||
count--;
|
||||
|
||||
// Make the current thread to sleep after every Message submission.
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.queue.load.leveling;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application Test
|
||||
*/
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.queue.load.leveling;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test case for submitting and retrieving messages from Blocking Queue.
|
||||
*/
|
||||
class MessageQueueTest {
|
||||
|
||||
@Test
|
||||
void messageQueueTest() {
|
||||
|
||||
var msgQueue = new MessageQueue();
|
||||
|
||||
// submit message
|
||||
msgQueue.submitMsg(new Message("MessageQueue Test"));
|
||||
|
||||
// retrieve message
|
||||
assertEquals("MessageQueue Test", msgQueue.retrieveMsg().getMsg());
|
||||
}
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.queue.load.leveling;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test case for creating and checking the Message.
|
||||
*/
|
||||
class MessageTest {
|
||||
|
||||
@Test
|
||||
void messageTest() {
|
||||
|
||||
// Parameterized constructor test.
|
||||
var testMsg = "Message Test";
|
||||
var msg = new Message(testMsg);
|
||||
assertEquals(testMsg, msg.getMsg());
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.queue.load.leveling;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test case for submitting Message to Blocking Queue by TaskGenerator and retrieve the message by
|
||||
* ServiceExecutor.
|
||||
*/
|
||||
class TaskGenSrvExeTest {
|
||||
|
||||
@Test
|
||||
void taskGeneratorTest() {
|
||||
var msgQueue = new MessageQueue();
|
||||
|
||||
// Create a task generator thread with 1 job to submit.
|
||||
var taskRunnable = new TaskGenerator(msgQueue, 1);
|
||||
var taskGenThr = new Thread(taskRunnable);
|
||||
taskGenThr.start();
|
||||
|
||||
assertNotNull(taskGenThr);
|
||||
|
||||
// Create a service executor thread.
|
||||
var srvRunnable = new ServiceExecutor(msgQueue);
|
||||
var srvExeThr = new Thread(srvRunnable);
|
||||
srvExeThr.start();
|
||||
|
||||
assertNotNull(srvExeThr);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user