java ( 2907718 ) - com.iluwatar.qu ( 2908854 ) stack: com.iluwatar.queue.load.leveling.App.main(App.java:72) org.codehaus.mojo.exec.ExecJavaMojo.doMain(ExecJavaMojo.java:385) org.codehaus.mojo.exec.ExecJavaMojo.doExec(ExecJavaMojo.java:374) org.codehaus.mojo.exec.ExecJavaMojo.lambda$execute$0(ExecJavaMojo.java:296) java.base/java.lang.Thread.run(Thread.java:840)
/*
* 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.
*
* 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.
*
*
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.
*
*
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.
*
*
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());
}
}
}
No. | From | Name | Value |
---|---|---|---|
1 | class@72 | LOGGER | Logger[com.iluwatar.queue.load.leveling.App] |
2 | class@72 | SHUTDOWN_TIME | 15 |
3 | 72 | args | [Ljava.lang.String;@1430daa8 |
END | 0 | 0 | 0 |
Process Filter | Thread Filter | ||
---|---|---|---|
2907718 java | 2908994 pool-1-thread-2 | 2908854 com.iluwatar.qu | 2908990 pool-1-thread-1 |
No. | PN | PID | TID | TN | T | L | Message |
---|---|---|---|---|---|---|---|
1 | java | 2907718 | 2908854 | com.iluwatar.qu | com.iluwatar.queue.load.leveling.App | I | Submitting TaskGenerators and ServiceExecutor threads. |
2 | java | 2907718 | 2908854 | com.iluwatar.qu | com.iluwatar.queue.load.leveling.App | I | Initiating shutdown. Executor will shutdown only after all the Threads are completed. |
3 | java | 2907718 | 2908990 | pool-1-thread-1 | com.iluwatar.queue.load.leveling.TaskGenerator | I | Message-5 submitted by pool-1-thread-1 |
4 | java | 2907718 | 2908994 | pool-1-thread-2 | com.iluwatar.queue.load.leveling.TaskGenerator | I | Message-1 submitted by pool-1-thread-2 |
5 | java | 2907718 | 2908990 | pool-1-thread-1 | com.iluwatar.queue.load.leveling.TaskGenerator | I | Message-4 submitted by pool-1-thread-1 |
6 | java | 2907718 | 2908994 | pool-1-thread-2 | com.iluwatar.queue.load.leveling.TaskGenerator | I | Message-2 submitted by pool-1-thread-2 |
7 | java | 2907718 | 2908990 | pool-1-thread-1 | com.iluwatar.queue.load.leveling.TaskGenerator | I | Message-3 submitted by pool-1-thread-1 |
8 | java | 2907718 | 2908994 | pool-1-thread-2 | com.iluwatar.queue.load.leveling.TaskGenerator | I | Message-1 submitted by pool-1-thread-2 |
9 | java | 2907718 | 2908994 | pool-1-thread-2 | com.iluwatar.queue.load.leveling.ServiceExecutor | I | Message-5 submitted by pool-1-thread-1 is served. |
10 | java | 2907718 | 2908990 | pool-1-thread-1 | com.iluwatar.queue.load.leveling.TaskGenerator | I | Message-2 submitted by pool-1-thread-1 |
11 | java | 2907718 | 2908994 | pool-1-thread-2 | com.iluwatar.queue.load.leveling.ServiceExecutor | I | Message-1 submitted by pool-1-thread-2 is served. |
12 | java | 2907718 | 2908990 | pool-1-thread-1 | com.iluwatar.queue.load.leveling.TaskGenerator | I | Message-1 submitted by pool-1-thread-1 |
13 | java | 2907718 | 2908994 | pool-1-thread-2 | com.iluwatar.queue.load.leveling.ServiceExecutor | I | Message-4 submitted by pool-1-thread-1 is served. |
14 | java | 2907718 | 2908994 | pool-1-thread-2 | com.iluwatar.queue.load.leveling.ServiceExecutor | I | Message-2 submitted by pool-1-thread-2 is served. |
15 | java | 2907718 | 2908994 | pool-1-thread-2 | com.iluwatar.queue.load.leveling.ServiceExecutor | I | Message-3 submitted by pool-1-thread-1 is served. |
16 | java | 2907718 | 2908994 | pool-1-thread-2 | com.iluwatar.queue.load.leveling.ServiceExecutor | I | Message-1 submitted by pool-1-thread-2 is served. |
17 | java | 2907718 | 2908994 | pool-1-thread-2 | com.iluwatar.queue.load.leveling.ServiceExecutor | I | Message-2 submitted by pool-1-thread-1 is served. |
18 | java | 2907718 | 2908994 | pool-1-thread-2 | com.iluwatar.queue.load.leveling.ServiceExecutor | I | Message-1 submitted by pool-1-thread-1 is served. |
19 | java | 2907718 | 2908994 | pool-1-thread-2 | com.iluwatar.queue.load.leveling.ServiceExecutor | I | Service Executor: Waiting for Messages to serve .. |
20 | java | 2907718 | 2908994 | pool-1-thread-2 | com.iluwatar.queue.load.leveling.ServiceExecutor | I | Service Executor: Waiting for Messages to serve .. |
21 | java | 2907718 | 2908994 | pool-1-thread-2 | com.iluwatar.queue.load.leveling.ServiceExecutor | I | Service Executor: Waiting for Messages to serve .. |
22 | java | 2907718 | 2908994 | pool-1-thread-2 | com.iluwatar.queue.load.leveling.ServiceExecutor | I | Service Executor: Waiting for Messages to serve .. |
23 | java | 2907718 | 2908854 | com.iluwatar.qu | com.iluwatar.queue.load.leveling.App | I | Executor was shut down and Exiting. |
24 | java | 2907718 | 2908994 | pool-1-thread-2 | com.iluwatar.queue.load.leveling.ServiceExecutor | E | sleep interrupted |
END | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Functions and Shortcuts
No. | Function | Shortcuts | Description |
---|---|---|---|
1 | GB | Alt + LEFT, Alt + A | Go Backward |
2 | GF | Alt + RIGHT, Alt + D | Go Foreward |
3 | PPE | Alt + UP, Alt + W | Previous Process End |
4 | NPS | Alt + DOWN, Alt + S | Next Process Start |
5 | PB | Ctrl + LEFT, Ctrl + A | current Process Backward |
6 | PF | Ctrl + RIGHT, Ctrl + D | current Process Foreward |
7 | PPTE | Ctrl + UP, Ctrl + W | go to current Process's Previous Thread's End |
8 | PNTS | Ctrl + DOWN, Ctrl + S | go to current Process's Next Thread's Start |
9 | TB | LEFT, A | current Thread Backward |
10 | TF | RIGHT, D | current Thread Foreward |
11 | LU | UP, W | go Line Up of current code block in current thread |
12 | LD | DOWN, S | go Line Down of current code block in current thread |
13 | LP | Shift + UP, Shift + W | go to the occurrence of current line in Previous Loop |
14 | LD | Shift + DOWN, Shift + S | go to the occurrence of current line in Next Loop |
15 | BS | Home | go to code Block Start |
16 | BE | End | go to code Block End |
Project: | JavaDesignPatterns |
Update: | 20240509 |
Commit: | bf6456ba6 |
Source Code: | queue-load-leveling |
BuildTool: | Java17 |
Compiler: | Java17 |
Runtime: | Openjdk17 |
System: | MySystemD |
Kernel: | Linux5.10.211 |
Cpu: | Intel:Corei7-7700K |
Machine: | AwesomeMachine |