Issue #450. First commit. Queue Based Load Leveling
This commit is contained in:
parent
68ec24c62e
commit
12b70cb39c
11
pom.xml
11
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.
|
||||
-->
|
||||
<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">
|
||||
--><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>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
@ -133,7 +131,8 @@
|
||||
<module>promise</module>
|
||||
<module>page-object</module>
|
||||
<module>event-asynchronous</module>
|
||||
</modules>
|
||||
<module>queue-load-leveling</module>
|
||||
</modules>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
@ -311,7 +310,7 @@
|
||||
</goals>
|
||||
</pluginExecutionFilter>
|
||||
<action>
|
||||
<ignore></ignore>
|
||||
<ignore/>
|
||||
</action>
|
||||
</pluginExecution>
|
||||
</pluginExecutions>
|
||||
@ -465,4 +464,4 @@
|
||||
</plugins>
|
||||
</reporting>
|
||||
|
||||
</project>
|
||||
</project>
|
34
queue-load-leveling/README.md
Normal file
34
queue-load-leveling/README.md
Normal file
@ -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.
|
||||
|
||||

|
||||
|
||||
|
||||
## 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)
|
BIN
queue-load-leveling/etc/queue-load-leveling.gif
Normal file
BIN
queue-load-leveling/etc/queue-load-leveling.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
86
queue-load-leveling/etc/queue-load-leveling.ucls
Normal file
86
queue-load-leveling/etc/queue-load-leveling.ucls
Normal file
@ -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>
|
42
queue-load-leveling/pom.xml
Normal file
42
queue-load-leveling/pom.xml
Normal file
@ -0,0 +1,42 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
|
||||
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.
|
||||
|
||||
-->
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.14.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>builder</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -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.
|
||||
* <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);
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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<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.
|
||||
* @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;
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user