Fixed all the code changes after review

This commit is contained in:
Chandana Amarnath 2016-11-28 20:13:18 +05:30
parent 1a75ab8df1
commit 1387e2bc96
9 changed files with 58 additions and 47 deletions

View File

@ -31,4 +31,4 @@ for both the task and the service.
## Credits ## Credits
* [Design Pattern: Queue-Based Load Leveling Pattern](https://msdn.microsoft.com/en-us/library/dn589783.aspx) * [Microsoft Cloud Design Patterns: Queue-Based Load Leveling Pattern](https://msdn.microsoft.com/en-us/library/dn589783.aspx)

View File

@ -23,6 +23,10 @@
package org.queue.load.leveling; package org.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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -58,37 +62,56 @@ public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class); 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 * @param args command line args
*/ */
public static void main(String[] 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 { try {
// Create a MessageQueue object. // Create a MessageQueue object.
MessageQueue msgQueue = new MessageQueue(); MessageQueue msgQueue = new MessageQueue();
LOGGER.info("All the TaskGenerators started."); LOGGER.info("Submitting TaskGenerators and ServiceExecutor threads.");
// Create three TaskGenerator threads. Each of them will submit different number of jobs. // Create three TaskGenerator threads. Each of them will submit different number of jobs.
Runnable taskRunnable1 = new TaskGenerator(msgQueue, 5); Runnable taskRunnable1 = new TaskGenerator(msgQueue, 5);
Runnable taskRunnable2 = new TaskGenerator(msgQueue, 1); Runnable taskRunnable2 = new TaskGenerator(msgQueue, 1);
Runnable taskRunnable3 = new TaskGenerator(msgQueue, 2); Runnable taskRunnable3 = new TaskGenerator(msgQueue, 2);
Thread taskGenerator1 = new Thread(taskRunnable1, "Task_Generator_1"); // Create e service which should process the submitted jobs.
Thread taskGenerator2 = new Thread(taskRunnable2, "Task_Generator_2");
Thread taskGenerator3 = new Thread(taskRunnable3, "Task_Generator_3");
taskGenerator1.start();
taskGenerator2.start();
taskGenerator3.start();
LOGGER.info("Service Executor started.");
// First create e service which will process all the jobs.
Runnable srvRunnable = new ServiceExecutor(msgQueue); Runnable srvRunnable = new ServiceExecutor(msgQueue);
Thread srvExec = new Thread(srvRunnable, "Service_Executor_Thread");
srvExec.start(); // 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("Intiating 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 (InterruptedException ie) {
LOGGER.error(ie.getMessage());
} catch (Exception e) { } catch (Exception e) {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
} }

View File

@ -27,12 +27,8 @@ package org.queue.load.leveling;
* *
*/ */
public class Message { public class Message {
private String msg; private final String msg;
// Empty constructor.
public Message() {
}
// Parameter constructor. // Parameter constructor.
public Message(String msg) { public Message(String msg) {
super(); super();
@ -44,11 +40,6 @@ public class Message {
return msg; return msg;
} }
// Set Method for attribute msg.
public void setMsg(String msg) {
this.msg = msg;
}
@Override @Override
public String toString() { public String toString() {
return msg; return msg;

View File

@ -38,7 +38,7 @@ public class MessageQueue {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class); private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
private BlockingQueue<Message> blkQueue; private final BlockingQueue<Message> blkQueue;
// Default constructor when called creates Blocking Queue object. // Default constructor when called creates Blocking Queue object.
public MessageQueue() { public MessageQueue() {

View File

@ -36,7 +36,7 @@ public class ServiceExecutor implements Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class); private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
private MessageQueue msgQueue; private final MessageQueue msgQueue;
public ServiceExecutor(MessageQueue msgQueue) { public ServiceExecutor(MessageQueue msgQueue) {
this.msgQueue = msgQueue; this.msgQueue = msgQueue;
@ -53,12 +53,12 @@ public class ServiceExecutor implements Runnable {
if (null != msg) { if (null != msg) {
LOGGER.info(msg.toString() + " is served."); LOGGER.info(msg.toString() + " is served.");
} else { } else {
LOGGER.info("ServiceExecutor: All tasks are executed. Waiting."); LOGGER.info("Service Executor: Waiting for Messages to serve .. ");
} }
Thread.sleep(1000); Thread.sleep(1000);
} }
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
LOGGER.error(ie.getMessage()); LOGGER.error(ie.getMessage());
} catch (Exception e) { } catch (Exception e) {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());

View File

@ -37,10 +37,10 @@ public class TaskGenerator implements Task, Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class); private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
// MessageQueue reference using which we will submit our messages. // MessageQueue reference using which we will submit our messages.
private MessageQueue msgQueue; private final MessageQueue msgQueue;
// Total message count that a TaskGenerator will submit. // Total message count that a TaskGenerator will submit.
private int msgCount; private final int msgCount;
// Parameterized constructor. // Parameterized constructor.
public TaskGenerator(MessageQueue msgQueue, int msgCount) { public TaskGenerator(MessageQueue msgQueue, int msgCount) {
@ -64,16 +64,18 @@ public class TaskGenerator implements Task, Runnable {
* After every message submission TaskGenerator thread will sleep for 1 second. * After every message submission TaskGenerator thread will sleep for 1 second.
*/ */
public void run() { public void run() {
int count = this.msgCount;
try { try {
while (this.msgCount > 0) { while (count > 0) {
String statusMsg = "Message-" + this.msgCount + " submitted by " + Thread.currentThread().getName(); String statusMsg = "Message-" + count + " submitted by " + Thread.currentThread().getName();
Message newMessage = new Message(statusMsg); this.submit(new Message(statusMsg));
this.submit(newMessage);
LOGGER.info(statusMsg); LOGGER.info(statusMsg);
// reduce the message count. // reduce the message count.
this.msgCount--; count--;
// Make the current thread to sleep after every Message submission. // Make the current thread to sleep after every Message submission.
Thread.sleep(1000); Thread.sleep(1000);

View File

@ -27,7 +27,7 @@ import java.io.IOException;
import org.junit.Test; import org.junit.Test;
/** /**
* Tests that Caching example runs without errors. * Application Test
*/ */
public class AppTest { public class AppTest {
@Test @Test

View File

@ -22,9 +22,10 @@
*/ */
package org.queue.load.leveling; package org.queue.load.leveling;
import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import org.junit.Test;
/** /**
* *
* Test case for submitting and retrieving messages from Blocking Queue. * Test case for submitting and retrieving messages from Blocking Queue.
@ -36,13 +37,12 @@ public class MessageQueueTest {
public void messageQueueTest() { public void messageQueueTest() {
MessageQueue msgQueue = new MessageQueue(); MessageQueue msgQueue = new MessageQueue();
Message msg = new Message("MessageQueue Test");
// submit message // submit message
msgQueue.submitMsg(msg); msgQueue.submitMsg(new Message("MessageQueue Test"));
// retrieve message // retrieve message
assertEquals(msg.getMsg(), msgQueue.retrieveMsg().getMsg()); assertEquals(msgQueue.retrieveMsg().getMsg(), "MessageQueue Test");
} }
} }

View File

@ -39,10 +39,5 @@ public class MessageTest {
String testMsg = "Message Test"; String testMsg = "Message Test";
Message msg = new Message(testMsg); Message msg = new Message(testMsg);
assertEquals(msg.getMsg(), testMsg); assertEquals(msg.getMsg(), testMsg);
// Default constructor and setter method test.
Message simpleMsg = new Message();
simpleMsg.setMsg(testMsg);
assertEquals(simpleMsg.getMsg(), testMsg);
} }
} }