Changes based on code review

This commit is contained in:
WSSIA 2016-09-11 18:45:51 +01:00
parent fce30db803
commit 9a90f2de1f
7 changed files with 125 additions and 82 deletions

View File

@ -5,6 +5,8 @@ folder: event-asynchronous
permalink: /patterns/event-asynchronous/ permalink: /patterns/event-asynchronous/
categories: Other categories: Other
tags: tags:
- difficulty-intermediate
- performance
- Java - Java
--- ---

View File

@ -65,12 +65,15 @@
</display> </display>
</class> </class>
<association id="7"> <association id="7">
<bendpoint x="433" y="475"/> <end type="SOURCE" refId="6" navigable="false">
<end type="SOURCE" refId="3" navigable="false"> <attribute id="8" name="app">
<attribute id="8" name="eventPool"/> <position height="0" width="0" x="0" y="0"/>
<multiplicity id="9" minimum="0" maximum="2147483647"/> </attribute>
<multiplicity id="9" minimum="0" maximum="1">
<position height="16" width="23" x="714" y="226"/>
</multiplicity>
</end> </end>
<end type="TARGET" refId="2" navigable="true"/> <end type="TARGET" refId="1" navigable="true"/>
<display labels="true" multiplicity="true"/> <display labels="true" multiplicity="true"/>
</association> </association>
<association id="10"> <association id="10">
@ -78,19 +81,24 @@
<attribute id="11" name="eventListener"> <attribute id="11" name="eventListener">
<position height="18" width="74" x="250" y="287"/> <position height="18" width="74" x="250" y="287"/>
</attribute> </attribute>
<multiplicity id="12" minimum="0" maximum="1"/> <multiplicity id="12" minimum="0" maximum="1">
<position height="0" width="0" x="0" y="0"/>
</multiplicity>
</end> </end>
<end type="TARGET" refId="5" navigable="true"/> <end type="TARGET" refId="5" navigable="true"/>
<display labels="true" multiplicity="true"/> <display labels="true" multiplicity="true"/>
</association> </association>
<association id="13"> <association id="13">
<end type="SOURCE" refId="6" navigable="false"> <bendpoint x="433" y="475"/>
<attribute id="14" name="app"/> <end type="SOURCE" refId="3" navigable="false">
<multiplicity id="15" minimum="0" maximum="1"> <attribute id="14" name="eventPool">
<position height="16" width="23" x="714" y="226"/> <position height="0" width="0" x="0" y="0"/>
</attribute>
<multiplicity id="15" minimum="0" maximum="2147483647">
<position height="0" width="0" x="0" y="0"/>
</multiplicity> </multiplicity>
</end> </end>
<end type="TARGET" refId="1" navigable="true"/> <end type="TARGET" refId="2" navigable="true"/>
<display labels="true" multiplicity="true"/> <display labels="true" multiplicity="true"/>
</association> </association>
<realization id="16"> <realization id="16">

View File

@ -48,6 +48,8 @@ import java.util.Scanner;
*/ */
public class App { public class App {
public static final String PROP_FILE_NAME = "config.properties";
boolean interactiveMode = false; boolean interactiveMode = false;
/** /**
@ -68,15 +70,14 @@ public class App {
*/ */
public void setUp() { public void setUp() {
Properties prop = new Properties(); Properties prop = new Properties();
String propFileName = "config.properties";
InputStream inputStream = App.class.getClassLoader().getResourceAsStream(propFileName); InputStream inputStream = App.class.getClassLoader().getResourceAsStream(PROP_FILE_NAME);
if (inputStream != null) { if (inputStream != null) {
try { try {
prop.load(inputStream); prop.load(inputStream);
} catch (IOException e) { } catch (IOException e) {
System.out.println(propFileName + " was not found. Defaulting to non-interactive mode."); System.out.println(PROP_FILE_NAME + " was not found. Defaulting to non-interactive mode.");
} }
String property = prop.getProperty("INTERACTIVE_MODE"); String property = prop.getProperty("INTERACTIVE_MODE");
if (property.equalsIgnoreCase("YES")) { if (property.equalsIgnoreCase("YES")) {
@ -104,23 +105,23 @@ public class App {
try { try {
// Create an Asynchronous event. // Create an Asynchronous event.
int aEventId = eventManager.createAsyncEvent(60); int aEventId = eventManager.createAsync(60);
System.out.println("Event [" + aEventId + "] has been created."); System.out.println("Event [" + aEventId + "] has been created.");
eventManager.startEvent(aEventId); eventManager.start(aEventId);
System.out.println("Event [" + aEventId + "] has been started."); System.out.println("Event [" + aEventId + "] has been started.");
// Create a Synchronous event. // Create a Synchronous event.
int sEventId = eventManager.createSyncEvent(60); int sEventId = eventManager.create(60);
System.out.println("Event [" + sEventId + "] has been created."); System.out.println("Event [" + sEventId + "] has been created.");
eventManager.startEvent(sEventId); eventManager.start(sEventId);
System.out.println("Event [" + sEventId + "] has been started."); System.out.println("Event [" + sEventId + "] has been started.");
eventManager.getStatus(aEventId); eventManager.status(aEventId);
eventManager.getStatus(sEventId); eventManager.status(sEventId);
eventManager.stopEvent(aEventId); eventManager.cancel(aEventId);
System.out.println("Event [" + aEventId + "] has been stopped."); System.out.println("Event [" + aEventId + "] has been stopped.");
eventManager.stopEvent(sEventId); eventManager.cancel(sEventId);
System.out.println("Event [" + sEventId + "] has been stopped."); System.out.println("Event [" + sEventId + "] has been stopped.");
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException
@ -136,35 +137,33 @@ public class App {
EventManager eventManager = new EventManager(); EventManager eventManager = new EventManager();
Scanner s = new Scanner(System.in); Scanner s = new Scanner(System.in);
int option = 0; int option = -1;
option = -1;
while (option != 5) { while (option != 5) {
System.out System.out.println("Hello. Would you like to boil some eggs?");
.println("(1) START_EVENT \n(2) STOP_EVENT \n(3) STATUS_OF_EVENT \n(4) STATUS_OF_ALL_EVENTS \n(5) EXIT"); System.out.println(
"(1) BOIL AN EGG \n(2) STOP BOILING THIS EGG \n(3) HOW IS MY EGG? \n(4) HOW ARE MY EGGS? \n(5) EXIT");
System.out.print("Choose [1,2,3,4,5]: "); System.out.print("Choose [1,2,3,4,5]: ");
option = s.nextInt(); option = s.nextInt();
if (option == 1) { if (option == 1) {
s.nextLine(); s.nextLine();
System.out.print("(A)sync or (S)ync event?: "); System.out.print("Boil multiple eggs at once (A) or boil them one-by-one (S)?: ");
String eventType = s.nextLine(); String eventType = s.nextLine();
System.out.print("How long should this event run for (in seconds)?: "); System.out.print("How long should this egg be boiled for (in seconds)?: ");
int eventTime = s.nextInt(); int eventTime = s.nextInt();
if (eventType.equalsIgnoreCase("A")) { if (eventType.equalsIgnoreCase("A")) {
try { try {
int eventId = eventManager.createAsyncEvent(eventTime); int eventId = eventManager.createAsync(eventTime);
System.out.println("Event [" + eventId + "] has been created."); eventManager.start(eventId);
eventManager.startEvent(eventId); System.out.println("Egg [" + eventId + "] is being boiled.");
System.out.println("Event [" + eventId + "] has been started.");
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) { } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
} }
} else if (eventType.equalsIgnoreCase("S")) { } else if (eventType.equalsIgnoreCase("S")) {
try { try {
int eventId = eventManager.createSyncEvent(eventTime); int eventId = eventManager.create(eventTime);
System.out.println("Event [" + eventId + "] has been created."); eventManager.start(eventId);
eventManager.startEvent(eventId); System.out.println("Egg [" + eventId + "] is being boiled.");
System.out.println("Event [" + eventId + "] has been started.");
} catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException } catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException
| EventDoesNotExistException e) { | EventDoesNotExistException e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
@ -173,24 +172,26 @@ public class App {
System.out.println("Unknown event type."); System.out.println("Unknown event type.");
} }
} else if (option == 2) { } else if (option == 2) {
System.out.print("Event ID: "); System.out.print("Which egg?: ");
int eventId = s.nextInt(); int eventId = s.nextInt();
try { try {
eventManager.stopEvent(eventId); eventManager.cancel(eventId);
System.out.println("Event [" + eventId + "] has been stopped."); System.out.println("Egg [" + eventId + "] is removed from boiler.");
} catch (EventDoesNotExistException e) { } catch (EventDoesNotExistException e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
} }
} else if (option == 3) { } else if (option == 3) {
System.out.print("Event ID: "); System.out.print("Which egg?: ");
int eventId = s.nextInt(); int eventId = s.nextInt();
try { try {
eventManager.getStatus(eventId); eventManager.status(eventId);
} catch (EventDoesNotExistException e) { } catch (EventDoesNotExistException e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
} }
} else if (option == 4) { } else if (option == 4) {
eventManager.getStatusOfAllEvents(); eventManager.statusOfAllEvents();
} else if (option == 5) {
eventManager.shutdown();
} }
} }

View File

@ -26,11 +26,10 @@ public class Event implements IEvent, Runnable {
private int eventId; private int eventId;
private int eventTime; private int eventTime;
private Thread thread; private Thread thread;
private long counter = 0;
private boolean isComplete = false; private boolean isComplete = false;
private ThreadCompleteListener eventListener; private ThreadCompleteListener eventListener;
public Event(int eventId, int eventTime) { public Event(final int eventId, final int eventTime) {
this.eventId = eventId; this.eventId = eventId;
this.eventTime = eventTime; this.eventTime = eventTime;
} }
@ -49,9 +48,9 @@ public class Event implements IEvent, Runnable {
@Override @Override
public void status() { public void status() {
if (!isComplete) { if (!isComplete) {
System.out.println("[" + eventId + "] I am at not done. [" + counter + "%]"); System.out.println("[" + eventId + "] is not done.");
} else { } else {
System.out.println("[" + eventId + "] I am done."); System.out.println("[" + eventId + "] is done.");
} }
} }
@ -61,14 +60,13 @@ public class Event implements IEvent, Runnable {
long endTime = currentTime + (eventTime * 1000); long endTime = currentTime + (eventTime * 1000);
while (System.currentTimeMillis() < endTime) { while (System.currentTimeMillis() < endTime) {
try { try {
counter += 1;
Thread.sleep(5000); // Sleep for 5 seconds. Thread.sleep(5000); // Sleep for 5 seconds.
} catch (InterruptedException e) { } catch (InterruptedException e) {
return; return;
} }
} }
isComplete = true; isComplete = true;
notifyListener(); completed();
} }
public final void addListener(final ThreadCompleteListener listener) { public final void addListener(final ThreadCompleteListener listener) {
@ -79,9 +77,9 @@ public class Event implements IEvent, Runnable {
this.eventListener = null; this.eventListener = null;
} }
private final void notifyListener() { private final void completed() {
if (eventListener != null) { if (eventListener != null) {
eventListener.notifyOfThreadComplete(eventId); eventListener.completedEventHandler(eventId);
} }
} }

View File

@ -32,10 +32,10 @@ import java.util.concurrent.ConcurrentHashMap;
*/ */
public class EventManager implements ThreadCompleteListener { public class EventManager implements ThreadCompleteListener {
private int minId = 1; public static final int MAX_RUNNING_EVENTS = 1000; // Just don't wanna have too many running events. :)
private int maxId = Integer.MAX_VALUE - 1; // Be cautious of overflows. public static final int MIN_ID = 1;
private int maxRunningEvents = 1000; // no particular reason. Just don't wanna have too many running events. :) public static final int MAX_ID = MAX_RUNNING_EVENTS;
private int maxEventTime = 1800; // in seconds / 30 minutes. public static final int MAX_EVENT_TIME = 1800; // in seconds / 30 minutes.
private int currentlyRunningSyncEvent = -1; private int currentlyRunningSyncEvent = -1;
private Random rand; private Random rand;
private Map<Integer, Event> eventPool; private Map<Integer, Event> eventPool;
@ -46,7 +46,7 @@ public class EventManager implements ThreadCompleteListener {
*/ */
public EventManager() { public EventManager() {
rand = new Random(1); rand = new Random(1);
eventPool = new ConcurrentHashMap<Integer, Event>(maxRunningEvents); eventPool = new ConcurrentHashMap<Integer, Event>(MAX_RUNNING_EVENTS);
} }
@ -59,7 +59,7 @@ public class EventManager implements ThreadCompleteListener {
* @throws InvalidOperationException No new synchronous events can be created when one is already running. * @throws InvalidOperationException No new synchronous events can be created when one is already running.
* @throws LongRunningEventException Long running events are not allowed in the app. * @throws LongRunningEventException Long running events are not allowed in the app.
*/ */
public int createSyncEvent(int eventTime) public int create(int eventTime)
throws MaxNumOfEventsAllowedException, InvalidOperationException, LongRunningEventException { throws MaxNumOfEventsAllowedException, InvalidOperationException, LongRunningEventException {
int eventId = createEvent(eventTime); int eventId = createEvent(eventTime);
if (currentlyRunningSyncEvent != -1) { if (currentlyRunningSyncEvent != -1) {
@ -79,18 +79,18 @@ public class EventManager implements ThreadCompleteListener {
* @throws MaxNumOfEventsAllowedException When too many events are running at a time. * @throws MaxNumOfEventsAllowedException When too many events are running at a time.
* @throws LongRunningEventException Long running events are not allowed in the app. * @throws LongRunningEventException Long running events are not allowed in the app.
*/ */
public int createAsyncEvent(int eventTime) throws MaxNumOfEventsAllowedException, LongRunningEventException { public int createAsync(int eventTime) throws MaxNumOfEventsAllowedException, LongRunningEventException {
return createEvent(eventTime); return createEvent(eventTime);
} }
private int createEvent(int eventTime) throws MaxNumOfEventsAllowedException, LongRunningEventException { private int createEvent(int eventTime) throws MaxNumOfEventsAllowedException, LongRunningEventException {
if (eventPool.size() == maxRunningEvents) { if (eventPool.size() == MAX_RUNNING_EVENTS) {
throw new MaxNumOfEventsAllowedException("Too many events are running at the moment. Please try again later."); throw new MaxNumOfEventsAllowedException("Too many events are running at the moment. Please try again later.");
} }
if (eventTime >= maxEventTime) { if (eventTime >= MAX_EVENT_TIME) {
throw new LongRunningEventException( throw new LongRunningEventException(
"Maximum event time allowed is " + maxEventTime + " seconds. Please try again."); "Maximum event time allowed is " + MAX_EVENT_TIME + " seconds. Please try again.");
} }
int newEventId = generateId(); int newEventId = generateId();
@ -108,7 +108,7 @@ public class EventManager implements ThreadCompleteListener {
* @param eventId The event that needs to be started. * @param eventId The event that needs to be started.
* @throws EventDoesNotExistException If event does not exist in our eventPool. * @throws EventDoesNotExistException If event does not exist in our eventPool.
*/ */
public void startEvent(int eventId) throws EventDoesNotExistException { public void start(int eventId) throws EventDoesNotExistException {
if (!eventPool.containsKey(eventId)) { if (!eventPool.containsKey(eventId)) {
throw new EventDoesNotExistException(eventId + " does not exist."); throw new EventDoesNotExistException(eventId + " does not exist.");
} }
@ -122,7 +122,7 @@ public class EventManager implements ThreadCompleteListener {
* @param eventId The event that needs to be stopped. * @param eventId The event that needs to be stopped.
* @throws EventDoesNotExistException If event does not exist in our eventPool. * @throws EventDoesNotExistException If event does not exist in our eventPool.
*/ */
public void stopEvent(int eventId) throws EventDoesNotExistException { public void cancel(int eventId) throws EventDoesNotExistException {
if (!eventPool.containsKey(eventId)) { if (!eventPool.containsKey(eventId)) {
throw new EventDoesNotExistException(eventId + " does not exist."); throw new EventDoesNotExistException(eventId + " does not exist.");
} }
@ -141,7 +141,7 @@ public class EventManager implements ThreadCompleteListener {
* @param eventId The event to inquire status of. * @param eventId The event to inquire status of.
* @throws EventDoesNotExistException If event does not exist in our eventPool. * @throws EventDoesNotExistException If event does not exist in our eventPool.
*/ */
public void getStatus(int eventId) throws EventDoesNotExistException { public void status(int eventId) throws EventDoesNotExistException {
if (!eventPool.containsKey(eventId)) { if (!eventPool.containsKey(eventId)) {
throw new EventDoesNotExistException(eventId + " does not exist."); throw new EventDoesNotExistException(eventId + " does not exist.");
} }
@ -153,7 +153,7 @@ public class EventManager implements ThreadCompleteListener {
* Gets status of all running events. * Gets status of all running events.
*/ */
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public void getStatusOfAllEvents() { public void statusOfAllEvents() {
Iterator it = eventPool.entrySet().iterator(); Iterator it = eventPool.entrySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next(); Map.Entry pair = (Map.Entry) it.next();
@ -161,6 +161,18 @@ public class EventManager implements ThreadCompleteListener {
} }
} }
/**
* Stop all running events.
*/
@SuppressWarnings("rawtypes")
public void shutdown() {
Iterator it = eventPool.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
((Event) pair.getValue()).stop();
}
}
/** /**
* Returns a pseudo-random number between min and max, inclusive. The difference between min and max can be at most * Returns a pseudo-random number between min and max, inclusive. The difference between min and max can be at most
* <code>Integer.MAX_VALUE - 1</code>. * <code>Integer.MAX_VALUE - 1</code>.
@ -168,9 +180,9 @@ public class EventManager implements ThreadCompleteListener {
private int generateId() { private int generateId() {
// nextInt is normally exclusive of the top value, // nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive // so add 1 to make it inclusive
int randomNum = rand.nextInt((maxId - minId) + 1) + minId; int randomNum = rand.nextInt((MAX_ID - MIN_ID) + 1) + MIN_ID;
while (eventPool.containsKey(randomNum)) { while (eventPool.containsKey(randomNum)) {
randomNum = rand.nextInt((maxId - minId) + 1) + minId; randomNum = rand.nextInt((MAX_ID - MIN_ID) + 1) + MIN_ID;
} }
return randomNum; return randomNum;
@ -180,9 +192,22 @@ public class EventManager implements ThreadCompleteListener {
* Callback from an {@link Event} (once it is complete). The Event is then removed from the pool. * Callback from an {@link Event} (once it is complete). The Event is then removed from the pool.
*/ */
@Override @Override
public void notifyOfThreadComplete(int eventId) { public void completedEventHandler(int eventId) {
eventPool.get(eventId).status(); eventPool.get(eventId).status();
eventPool.remove(eventId); eventPool.remove(eventId);
} }
/**
* Getter method for event pool.
*/
public Map<Integer, Event> getEventPool() {
return eventPool;
}
/**
* Get number of currently running Synchronous events.
*/
public int numOfCurrentlyRunningSyncEvent() {
return currentlyRunningSyncEvent;
}
} }

View File

@ -17,5 +17,5 @@
package com.iluwatar.event.asynchronous; package com.iluwatar.event.asynchronous;
public interface ThreadCompleteListener { public interface ThreadCompleteListener {
void notifyOfThreadComplete(final int eventId); void completedEventHandler(final int eventId);
} }

View File

@ -16,6 +16,8 @@
*/ */
package com.iluwatar.event.asynchronous; package com.iluwatar.event.asynchronous;
import static org.junit.Assert.assertTrue;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -36,9 +38,13 @@ public class EventAsynchronousTest {
public void testAsynchronousEvent() { public void testAsynchronousEvent() {
EventManager eventManager = new EventManager(); EventManager eventManager = new EventManager();
try { try {
int aEventId = eventManager.createAsyncEvent(60); int aEventId = eventManager.createAsync(60);
eventManager.startEvent(aEventId); eventManager.start(aEventId);
eventManager.stopEvent(aEventId); assertTrue(eventManager.getEventPool().size() == 1);
assertTrue(eventManager.getEventPool().size() < EventManager.MAX_RUNNING_EVENTS);
assertTrue(eventManager.numOfCurrentlyRunningSyncEvent() == -1);
eventManager.cancel(aEventId);
assertTrue(eventManager.getEventPool().size() == 0);
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) { } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
} }
@ -48,25 +54,28 @@ public class EventAsynchronousTest {
public void testSynchronousEvent() { public void testSynchronousEvent() {
EventManager eventManager = new EventManager(); EventManager eventManager = new EventManager();
try { try {
int sEventId = eventManager.createSyncEvent(60); int sEventId = eventManager.create(60);
eventManager.startEvent(sEventId); eventManager.start(sEventId);
eventManager.stopEvent(sEventId); assertTrue(eventManager.getEventPool().size() == 1);
assertTrue(eventManager.getEventPool().size() < EventManager.MAX_RUNNING_EVENTS);
assertTrue(eventManager.numOfCurrentlyRunningSyncEvent() != -1);
eventManager.cancel(sEventId);
assertTrue(eventManager.getEventPool().size() == 0);
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException
| InvalidOperationException e) { | InvalidOperationException e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
} }
} }
@Test @Test(expected = InvalidOperationException.class)
public void testUnsuccessfulSynchronousEvent() { public void testUnsuccessfulSynchronousEvent() throws InvalidOperationException {
EventManager eventManager = new EventManager(); EventManager eventManager = new EventManager();
try { try {
int sEventId = eventManager.createSyncEvent(60); int sEventId = eventManager.create(60);
eventManager.startEvent(sEventId); eventManager.start(sEventId);
sEventId = eventManager.createSyncEvent(60); sEventId = eventManager.create(60);
eventManager.startEvent(sEventId); eventManager.start(sEventId);
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
| InvalidOperationException e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
} }
} }