From 12b70cb39c5b73fc95b7127dbfc8d9e05bd778cb Mon Sep 17 00:00:00 2001 From: Chandana Amarnath Date: Sun, 20 Nov 2016 16:25:10 +0530 Subject: [PATCH] Issue #450. First commit. Queue Based Load Leveling --- pom.xml | 11 +- queue-load-leveling/README.md | 34 +++++++ .../etc/queue-load-leveling.gif | Bin 0 -> 15062 bytes .../etc/queue-load-leveling.ucls | 86 ++++++++++++++++ queue-load-leveling/pom.xml | 42 ++++++++ .../java/org/queue/load/leveling/App.java | 96 ++++++++++++++++++ .../java/org/queue/load/leveling/Message.java | 56 ++++++++++ .../org/queue/load/leveling/MessageQueue.java | 78 ++++++++++++++ .../queue/load/leveling/ServiceExecutor.java | 67 ++++++++++++ .../java/org/queue/load/leveling/Task.java | 30 ++++++ .../queue/load/leveling/TaskGenerator.java | 87 ++++++++++++++++ .../java/org/queue/load/leveling/AppTest.java | 38 +++++++ .../queue/load/leveling/MessageQueueTest.java | 48 +++++++++ .../org/queue/load/leveling/MessageTest.java | 48 +++++++++ .../load/leveling/TaskGenSrvExeTest.java | 50 +++++++++ 15 files changed, 765 insertions(+), 6 deletions(-) create mode 100644 queue-load-leveling/README.md create mode 100644 queue-load-leveling/etc/queue-load-leveling.gif create mode 100644 queue-load-leveling/etc/queue-load-leveling.ucls create mode 100644 queue-load-leveling/pom.xml create mode 100644 queue-load-leveling/src/main/java/org/queue/load/leveling/App.java create mode 100644 queue-load-leveling/src/main/java/org/queue/load/leveling/Message.java create mode 100644 queue-load-leveling/src/main/java/org/queue/load/leveling/MessageQueue.java create mode 100644 queue-load-leveling/src/main/java/org/queue/load/leveling/ServiceExecutor.java create mode 100644 queue-load-leveling/src/main/java/org/queue/load/leveling/Task.java create mode 100644 queue-load-leveling/src/main/java/org/queue/load/leveling/TaskGenerator.java create mode 100644 queue-load-leveling/src/test/java/org/queue/load/leveling/AppTest.java create mode 100644 queue-load-leveling/src/test/java/org/queue/load/leveling/MessageQueueTest.java create mode 100644 queue-load-leveling/src/test/java/org/queue/load/leveling/MessageTest.java create mode 100644 queue-load-leveling/src/test/java/org/queue/load/leveling/TaskGenSrvExeTest.java diff --git a/pom.xml b/pom.xml index adb95bef2..511a0a465 100644 --- a/pom.xml +++ b/pom.xml @@ -17,9 +17,7 @@ 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. ---> - +--> 4.0.0 com.iluwatar java-design-patterns @@ -133,7 +131,8 @@ promise page-object event-asynchronous - + queue-load-leveling + @@ -311,7 +310,7 @@ - + @@ -465,4 +464,4 @@ - + \ No newline at end of file diff --git a/queue-load-leveling/README.md b/queue-load-leveling/README.md new file mode 100644 index 000000000..ad65dad91 --- /dev/null +++ b/queue-load-leveling/README.md @@ -0,0 +1,34 @@ +--- +layout: pattern +title: Queue based load leveling +folder: queue-load-leveling +permalink: /patterns/queue-load-leveling/ +pumlid: +categories: Other +tags: + - Java + - Difficulty-Intermediate + - Performance +--- + +## Intent +Use a queue that acts as a buffer between a task and a service that it invokes in order to smooth +intermittent heavy loads that may otherwise cause the service to fail or the task to time out. +This pattern can help to minimize the impact of peaks in demand on availability and responsiveness +for both the task and the service. + +![alt text](./etc/queue-load-leveling.gif "queue-load-leveling") + + +## Applicability + +* This pattern is ideally suited to any type of application that uses services that may be subject to overloading. +* This pattern might not be suitable if the application expects a response from the service with minimal latency. + +## Real world example + +* A Microsoft Azure web role stores data by using a separate storage service. If a large number of instances of the web role run concurrently, it is possible that the storage service could be overwhelmed and be unable to respond to requests quickly enough to prevent these requests from timing out or failing. + +## Credits + +* [Design Pattern: Queue-Based Load Leveling Pattern](https://msdn.microsoft.com/en-us/library/dn589783.aspx) \ No newline at end of file diff --git a/queue-load-leveling/etc/queue-load-leveling.gif b/queue-load-leveling/etc/queue-load-leveling.gif new file mode 100644 index 0000000000000000000000000000000000000000..4f57c3e5070179760cb647c0d5047be19a1d92f2 GIT binary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qS}Ij=y{o8zK8|!XOPBT1IOEpLDilsu+sxKnYG^1qf6{;pks^I1gQ_SM zSd{8KZQdhPw&5w0D{7HBS>jhl=AOL7AFktZH}BD|?WrH-K8)#+USW_+p;lJm9SYD}3(5KJ|*7;er3<6Iz0`O; z1(L($qe+AMe8X`{Xyg5-JYzTA%S;ku7(1(YQFF?>-RrE&969Z%KSlKBm+aM-aO&HZ zd!~vcRV3=fnrFqCcSp=eRxyU?QcKA1a~H)&miGYS65%Tx9O{@}gO9%DQf2FKSxuL{ z^Ppm@Z0oCNQC!tlQSu`m^f#yQ_fB>INWn NBP0zABshTQe*k~Fbn*ZI literal 0 HcmV?d00001 diff --git a/queue-load-leveling/etc/queue-load-leveling.ucls b/queue-load-leveling/etc/queue-load-leveling.ucls new file mode 100644 index 000000000..91440e276 --- /dev/null +++ b/queue-load-leveling/etc/queue-load-leveling.ucls @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/queue-load-leveling/pom.xml b/queue-load-leveling/pom.xml new file mode 100644 index 000000000..77041f2c0 --- /dev/null +++ b/queue-load-leveling/pom.xml @@ -0,0 +1,42 @@ + + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.14.0-SNAPSHOT + + builder + + + junit + junit + test + + + \ No newline at end of file diff --git a/queue-load-leveling/src/main/java/org/queue/load/leveling/App.java b/queue-load-leveling/src/main/java/org/queue/load/leveling/App.java new file mode 100644 index 000000000..c9c37c8b6 --- /dev/null +++ b/queue-load-leveling/src/main/java/org/queue/load/leveling/App.java @@ -0,0 +1,96 @@ +/** + * The MIT License + * Copyright (c) 2014 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 org.queue.load.leveling; + +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. + *

+ * 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. + * + */ +public class App { + + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); + + /** + * Program entry point + * + * @param args command line args + */ + public static void main(String[] args) { + try { + // Create a MessageQueue object. + MessageQueue msgQueue = new MessageQueue(); + + LOGGER.info("All the TaskGenerators started."); + + // Create three TaskGenerator threads. Each of them will submit different number of jobs. + Runnable taskRunnable_1 = new TaskGenerator(msgQueue, 5); + Runnable taskRunnable_2 = new TaskGenerator(msgQueue, 1); + Runnable taskRunnable_3 = new TaskGenerator(msgQueue, 2); + + Thread taskGenerator_1 = new Thread(taskRunnable_1, "Task_Generator_1"); + Thread taskGenerator_2 = new Thread(taskRunnable_2, "Task_Generator_2"); + Thread taskGenerator_3 = new Thread(taskRunnable_3, "Task_Generator_3"); + + taskGenerator_1.start(); + taskGenerator_2.start(); + taskGenerator_3.start(); + + LOGGER.info("Service Executor started."); + + // First create e service which will process all the jobs. + Runnable srvRunnable = new ServiceExecutor(msgQueue); + Thread srvExec = new Thread(srvRunnable, "Service_Executor_Thread"); + srvExec.start(); + } catch(Exception e) { + LOGGER.error(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/queue-load-leveling/src/main/java/org/queue/load/leveling/Message.java b/queue-load-leveling/src/main/java/org/queue/load/leveling/Message.java new file mode 100644 index 000000000..cffbc7af3 --- /dev/null +++ b/queue-load-leveling/src/main/java/org/queue/load/leveling/Message.java @@ -0,0 +1,56 @@ +/** + * The MIT License + * Copyright (c) 2014 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 org.queue.load.leveling; + +/** + * Message class with only one parameter. + * +*/ +public class Message { + private String msg; + + // Empty constructor. + public Message() { + } + + // Parameter constructor. + public Message(String msg) { + super(); + this.msg = msg; + } + + // Get Method for attribute msg. + public String getMsg() { + return msg; + } + + // Set Method for attribute msg. + public void setMsg(String msg) { + this.msg = msg; + } + + @Override + public String toString() { + return msg; + } +} \ No newline at end of file diff --git a/queue-load-leveling/src/main/java/org/queue/load/leveling/MessageQueue.java b/queue-load-leveling/src/main/java/org/queue/load/leveling/MessageQueue.java new file mode 100644 index 000000000..52dcfca3f --- /dev/null +++ b/queue-load-leveling/src/main/java/org/queue/load/leveling/MessageQueue.java @@ -0,0 +1,78 @@ +/** + * The MIT License + * Copyright (c) 2014 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 org.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. + */ +public class MessageQueue { + + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); + + private BlockingQueue 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. + * @param msg + */ + 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() { + Message retrievedMsg = null; + try { + retrievedMsg = blkQueue.poll(); + } catch(Exception e) { + LOGGER.error(e.getMessage()); + } + + return retrievedMsg; + } +} \ No newline at end of file diff --git a/queue-load-leveling/src/main/java/org/queue/load/leveling/ServiceExecutor.java b/queue-load-leveling/src/main/java/org/queue/load/leveling/ServiceExecutor.java new file mode 100644 index 000000000..23fdcab8b --- /dev/null +++ b/queue-load-leveling/src/main/java/org/queue/load/leveling/ServiceExecutor.java @@ -0,0 +1,67 @@ +/** + * The MIT License + * Copyright (c) 2014 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 org.queue.load.leveling; + +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. + */ +public class ServiceExecutor implements Runnable { + + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); + + private MessageQueue msgQueue; + + public ServiceExecutor(MessageQueue msgQueue) { + this.msgQueue = msgQueue; + } + + /** + * The ServiceExecutor thread will retrieve each message and process it. + */ + public void run() { + try { + while(true) { + Message msg = msgQueue.retrieveMsg(); + + if(null != msg) { + LOGGER.info(msg.toString() + " is served."); + } else { + LOGGER.info("ServiceExecutor: All tasks are executed. Waiting."); + } + + Thread.sleep(1000); + } + } catch(InterruptedException ie) { + LOGGER.error(ie.getMessage()); + } catch(Exception e) { + LOGGER.error(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/queue-load-leveling/src/main/java/org/queue/load/leveling/Task.java b/queue-load-leveling/src/main/java/org/queue/load/leveling/Task.java new file mode 100644 index 000000000..f01a1531a --- /dev/null +++ b/queue-load-leveling/src/main/java/org/queue/load/leveling/Task.java @@ -0,0 +1,30 @@ +/** + * The MIT License + * Copyright (c) 2014 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 org.queue.load.leveling; +/** + * Task Interface. + * +*/ +public interface Task { + public void submit(Message msg); +} diff --git a/queue-load-leveling/src/main/java/org/queue/load/leveling/TaskGenerator.java b/queue-load-leveling/src/main/java/org/queue/load/leveling/TaskGenerator.java new file mode 100644 index 000000000..6b73150e1 --- /dev/null +++ b/queue-load-leveling/src/main/java/org/queue/load/leveling/TaskGenerator.java @@ -0,0 +1,87 @@ +/** + * The MIT License + * Copyright (c) 2014 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 org.queue.load.leveling; + +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. + * +*/ +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 MessageQueue msgQueue; + + // Total message count that a TaskGenerator will submit. + private 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() { + try { + while(this.msgCount > 0) { + String statusMsg = "Message-" + this.msgCount + " submitted by " + Thread.currentThread().getName(); + Message newMessage = new Message(statusMsg); + this.submit(newMessage); + + LOGGER.info(statusMsg); + + // reduce the message count. + this.msgCount--; + + // Make the current thread to sleep after every Message submission. + Thread.sleep(1000); + } + } catch(InterruptedException ie) { + LOGGER.error(ie.getMessage()); + } catch(Exception e) { + LOGGER.error(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/queue-load-leveling/src/test/java/org/queue/load/leveling/AppTest.java b/queue-load-leveling/src/test/java/org/queue/load/leveling/AppTest.java new file mode 100644 index 000000000..fc4e5dbe1 --- /dev/null +++ b/queue-load-leveling/src/test/java/org/queue/load/leveling/AppTest.java @@ -0,0 +1,38 @@ +/** + * The MIT License + * Copyright (c) 2014 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 org.queue.load.leveling; + +import java.io.IOException; + +import org.junit.Test; + +/** + * Tests that Caching example runs without errors. + */ +public class AppTest { + @Test + public void test() throws IOException { + String[] args = {}; + App.main(args); + } +} \ No newline at end of file diff --git a/queue-load-leveling/src/test/java/org/queue/load/leveling/MessageQueueTest.java b/queue-load-leveling/src/test/java/org/queue/load/leveling/MessageQueueTest.java new file mode 100644 index 000000000..b82d4db2e --- /dev/null +++ b/queue-load-leveling/src/test/java/org/queue/load/leveling/MessageQueueTest.java @@ -0,0 +1,48 @@ +/** + * The MIT License + * Copyright (c) 2014 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 org.queue.load.leveling; + +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * Test case for submitting and retrieving messages from Blocking Queue. + * + */ +public class MessageQueueTest { + + @Test + public void messageQueueTest() { + + MessageQueue msgQueue = new MessageQueue(); + Message msg = new Message("MessageQueue Test"); + + // submit message + msgQueue.submitMsg(msg); + + // retrieve message + assertEquals(msg.getMsg(), msgQueue.retrieveMsg().getMsg()); + } + +} diff --git a/queue-load-leveling/src/test/java/org/queue/load/leveling/MessageTest.java b/queue-load-leveling/src/test/java/org/queue/load/leveling/MessageTest.java new file mode 100644 index 000000000..bbd8bb8f6 --- /dev/null +++ b/queue-load-leveling/src/test/java/org/queue/load/leveling/MessageTest.java @@ -0,0 +1,48 @@ +/** + * The MIT License + * Copyright (c) 2014 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 org.queue.load.leveling; + +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * Test case for creating and checking the Message. + * + */ +public class MessageTest { + + @Test + public void messageTest() { + + // Parameterized constructor test. + String testMsg = "Message Test"; + Message msg = new Message(testMsg); + assertEquals(msg.getMsg(), testMsg); + + // Default constructor and setter method test. + Message simpleMsg = new Message(); + simpleMsg.setMsg(testMsg); + assertEquals(simpleMsg.getMsg(), testMsg); + } +} diff --git a/queue-load-leveling/src/test/java/org/queue/load/leveling/TaskGenSrvExeTest.java b/queue-load-leveling/src/test/java/org/queue/load/leveling/TaskGenSrvExeTest.java new file mode 100644 index 000000000..d98fee30a --- /dev/null +++ b/queue-load-leveling/src/test/java/org/queue/load/leveling/TaskGenSrvExeTest.java @@ -0,0 +1,50 @@ +/** + * The MIT License + * Copyright (c) 2014 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 org.queue.load.leveling; + +import org.junit.Test; + +/** + * + * Test case for submitting Message to Blocking Queue by TaskGenerator + * and retrieve the message by ServiceExecutor. + * + */ +public class TaskGenSrvExeTest { + + @Test + public void taskGeneratorTest() { + MessageQueue msgQueue = new MessageQueue(); + + // Create a task generator thread with 1 job to submit. + Runnable taskRunnable = new TaskGenerator(msgQueue, 1); + Thread taskGenThr = new Thread(taskRunnable); + taskGenThr.start(); + + // Create a service executor thread. + Runnable srvRunnable = new ServiceExecutor(msgQueue); + Thread srvExeThr = new Thread(srvRunnable); + srvExeThr.start(); + } + +}