Resolves checkstyle errors for naked-objects null-object object-mother object-pool observer queue-load-leveling (#1082)

* Reduces checkstyle errors in naked-objects

* Reduces checkstyle errors in null-object

* Reduces checkstyle errors in object-mother

* Reduces checkstyle errors in object-pool

* Reduces checkstyle errors in observer

* Reduces checkstyle errors in queue-load-leveling
This commit is contained in:
Anurag Agarwal
2019-11-13 00:56:15 +05:30
committed by Ilkka Seppälä
parent 1e76d91929
commit 6ef840f3cf
41 changed files with 242 additions and 288 deletions

View File

@ -26,69 +26,69 @@ package com.iluwatar.queue.load.leveling;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* 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.
*
* 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.
*/
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
//Executor shut down time limit.
private static final int SHUTDOWN_TIME = 15;
/**
* Program entry point
*
* 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.
MessageQueue msgQueue = new MessageQueue();
LOGGER.info("Submitting TaskGenerators and ServiceExecutor threads.");
// Create three TaskGenerator threads. Each of them will submit different number of jobs.
Runnable taskRunnable1 = new TaskGenerator(msgQueue, 5);
Runnable taskRunnable2 = new TaskGenerator(msgQueue, 1);
Runnable taskRunnable3 = new TaskGenerator(msgQueue, 2);
final Runnable taskRunnable1 = new TaskGenerator(msgQueue, 5);
final Runnable taskRunnable2 = new TaskGenerator(msgQueue, 1);
final Runnable taskRunnable3 = new TaskGenerator(msgQueue, 2);
// Create e service which should process the submitted jobs.
Runnable srvRunnable = new ServiceExecutor(msgQueue);
final Runnable srvRunnable = new ServiceExecutor(msgQueue);
// Create a ThreadPool of 2 threads and
// submit all Runnable task for execution to executor..
@ -96,17 +96,18 @@ public class App {
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("Intiating shutdown. Executor will shutdown only after all the Threads are completed.");
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) ) {
if (!executor.awaitTermination(SHUTDOWN_TIME, TimeUnit.SECONDS)) {
LOGGER.info("Executor was shut down and Exiting.");
executor.shutdownNow();
}

View File

@ -24,12 +24,11 @@
package com.iluwatar.queue.load.leveling;
/**
* Message class with only one parameter.
*
*/
* Message class with only one parameter.
*/
public class Message {
private final String msg;
// Parameter constructor.
public Message(String msg) {
this.msg = msg;
@ -39,7 +38,7 @@ public class Message {
public String getMsg() {
return msg;
}
@Override
public String toString() {
return msg;

View File

@ -25,30 +25,27 @@ package com.iluwatar.queue.load.leveling;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* MessageQueue class.
* In this class we will create a Blocking Queue and
* submit/retrieve all the messages from it.
* MessageQueue class. In this class we will create a Blocking Queue and submit/retrieve all the
* messages from it.
*/
public class MessageQueue {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
private final BlockingQueue<Message> blkQueue;
// Default constructor when called creates Blocking Queue object.
public MessageQueue() {
this.blkQueue = new ArrayBlockingQueue<Message>(1024);
}
/**
* All the TaskGenerator threads will call this method to insert the
* Messages in to the Blocking Queue.
* All the TaskGenerator threads will call this method to insert the Messages in to the Blocking
* Queue.
*/
public void submitMsg(Message msg) {
try {
@ -59,11 +56,10 @@ public class MessageQueue {
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.
* 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() {
Message retrievedMsg = null;
@ -72,7 +68,7 @@ public class MessageQueue {
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
return retrievedMsg;
}
}

View File

@ -27,10 +27,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* ServiceExecuotr class.
* This class will pick up Messages one by one from
* the Blocking Queue and process them.
* ServiceExecuotr class. This class will pick up Messages one by one from the Blocking Queue and
* process them.
*/
public class ServiceExecutor implements Runnable {

View File

@ -22,10 +22,10 @@
*/
package com.iluwatar.queue.load.leveling;
/**
* Task Interface.
*
*/
*/
public interface Task {
void submit(Message msg);
}

View File

@ -27,28 +27,26 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* TaskGenerator class.
* Each TaskGenerator thread will be a Worker which submit's messages to the queue.
* We need to mention the message count for each of the TaskGenerator threads.
*
*/
* TaskGenerator class. Each TaskGenerator thread will be a Worker which submit's messages to the
* queue. We need to mention the message count for each of the TaskGenerator threads.
*/
public class TaskGenerator implements Task, Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
// 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.
*/
@ -59,25 +57,25 @@ public class TaskGenerator implements Task, Runnable {
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.
* 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() {
int count = this.msgCount;
try {
while (count > 0) {
String 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);
}