Skip to content

Commit

Permalink
Changes based on code review
Browse files Browse the repository at this point in the history
  • Loading branch information
WSSIA committed Sep 11, 2016
1 parent fce30db commit 9a90f2d
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 82 deletions.
2 changes: 2 additions & 0 deletions event-asynchronous/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ folder: event-asynchronous
permalink: /patterns/event-asynchronous/
categories: Other
tags:
- difficulty-intermediate
- performance
- Java
---

Expand Down
30 changes: 19 additions & 11 deletions event-asynchronous/etc/event-asynchronous.ucls
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,40 @@
</display>
</class>
<association id="7">
<bendpoint x="433" y="475"/>
<end type="SOURCE" refId="3" navigable="false">
<attribute id="8" name="eventPool"/>
<multiplicity id="9" minimum="0" maximum="2147483647"/>
<end type="SOURCE" refId="6" navigable="false">
<attribute id="8" name="app">
<position height="0" width="0" x="0" y="0"/>
</attribute>
<multiplicity id="9" minimum="0" maximum="1">
<position height="16" width="23" x="714" y="226"/>
</multiplicity>
</end>
<end type="TARGET" refId="2" navigable="true"/>
<end type="TARGET" refId="1" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<association id="10">
<end type="SOURCE" refId="2" navigable="false">
<attribute id="11" name="eventListener">
<position height="18" width="74" x="250" y="287"/>
</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 type="TARGET" refId="5" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<association id="13">
<end type="SOURCE" refId="6" navigable="false">
<attribute id="14" name="app"/>
<multiplicity id="15" minimum="0" maximum="1">
<position height="16" width="23" x="714" y="226"/>
<bendpoint x="433" y="475"/>
<end type="SOURCE" refId="3" navigable="false">
<attribute id="14" name="eventPool">
<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>
</end>
<end type="TARGET" refId="1" navigable="true"/>
<end type="TARGET" refId="2" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<realization id="16">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
*/
public class App {

public static final String PROP_FILE_NAME = "config.properties";

boolean interactiveMode = false;

/**
Expand All @@ -68,15 +70,14 @@ public static void main(String[] args) {
*/
public void setUp() {
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) {
try {
prop.load(inputStream);
} 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");
if (property.equalsIgnoreCase("YES")) {
Expand Down Expand Up @@ -104,23 +105,23 @@ public void quickRun() {

try {
// Create an Asynchronous event.
int aEventId = eventManager.createAsyncEvent(60);
int aEventId = eventManager.createAsync(60);
System.out.println("Event [" + aEventId + "] has been created.");
eventManager.startEvent(aEventId);
eventManager.start(aEventId);
System.out.println("Event [" + aEventId + "] has been started.");

// Create a Synchronous event.
int sEventId = eventManager.createSyncEvent(60);
int sEventId = eventManager.create(60);
System.out.println("Event [" + sEventId + "] has been created.");
eventManager.startEvent(sEventId);
eventManager.start(sEventId);
System.out.println("Event [" + sEventId + "] has been started.");

eventManager.getStatus(aEventId);
eventManager.getStatus(sEventId);
eventManager.status(aEventId);
eventManager.status(sEventId);

eventManager.stopEvent(aEventId);
eventManager.cancel(aEventId);
System.out.println("Event [" + aEventId + "] has been stopped.");
eventManager.stopEvent(sEventId);
eventManager.cancel(sEventId);
System.out.println("Event [" + sEventId + "] has been stopped.");

} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException
Expand All @@ -136,35 +137,33 @@ public void runInteractiveMode() {
EventManager eventManager = new EventManager();

Scanner s = new Scanner(System.in);
int option = 0;
option = -1;
int option = -1;
while (option != 5) {
System.out
.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("Hello. Would you like to boil some eggs?");
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]: ");
option = s.nextInt();

if (option == 1) {
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();
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();
if (eventType.equalsIgnoreCase("A")) {
try {
int eventId = eventManager.createAsyncEvent(eventTime);
System.out.println("Event [" + eventId + "] has been created.");
eventManager.startEvent(eventId);
System.out.println("Event [" + eventId + "] has been started.");
int eventId = eventManager.createAsync(eventTime);
eventManager.start(eventId);
System.out.println("Egg [" + eventId + "] is being boiled.");
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
System.out.println(e.getMessage());
}
} else if (eventType.equalsIgnoreCase("S")) {
try {
int eventId = eventManager.createSyncEvent(eventTime);
System.out.println("Event [" + eventId + "] has been created.");
eventManager.startEvent(eventId);
System.out.println("Event [" + eventId + "] has been started.");
int eventId = eventManager.create(eventTime);
eventManager.start(eventId);
System.out.println("Egg [" + eventId + "] is being boiled.");
} catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException
| EventDoesNotExistException e) {
System.out.println(e.getMessage());
Expand All @@ -173,24 +172,26 @@ public void runInteractiveMode() {
System.out.println("Unknown event type.");
}
} else if (option == 2) {
System.out.print("Event ID: ");
System.out.print("Which egg?: ");
int eventId = s.nextInt();
try {
eventManager.stopEvent(eventId);
System.out.println("Event [" + eventId + "] has been stopped.");
eventManager.cancel(eventId);
System.out.println("Egg [" + eventId + "] is removed from boiler.");
} catch (EventDoesNotExistException e) {
System.out.println(e.getMessage());
}
} else if (option == 3) {
System.out.print("Event ID: ");
System.out.print("Which egg?: ");
int eventId = s.nextInt();
try {
eventManager.getStatus(eventId);
eventManager.status(eventId);
} catch (EventDoesNotExistException e) {
System.out.println(e.getMessage());
}
} else if (option == 4) {
eventManager.getStatusOfAllEvents();
eventManager.statusOfAllEvents();
} else if (option == 5) {
eventManager.shutdown();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ public class Event implements IEvent, Runnable {
private int eventId;
private int eventTime;
private Thread thread;
private long counter = 0;
private boolean isComplete = false;
private ThreadCompleteListener eventListener;

public Event(int eventId, int eventTime) {
public Event(final int eventId, final int eventTime) {
this.eventId = eventId;
this.eventTime = eventTime;
}
Expand All @@ -49,9 +48,9 @@ public void stop() {
@Override
public void status() {
if (!isComplete) {
System.out.println("[" + eventId + "] I am at not done. [" + counter + "%]");
System.out.println("[" + eventId + "] is not done.");
} else {
System.out.println("[" + eventId + "] I am done.");
System.out.println("[" + eventId + "] is done.");
}
}

Expand All @@ -61,14 +60,13 @@ public void run() {
long endTime = currentTime + (eventTime * 1000);
while (System.currentTimeMillis() < endTime) {
try {
counter += 1;
Thread.sleep(5000); // Sleep for 5 seconds.
} catch (InterruptedException e) {
return;
}
}
isComplete = true;
notifyListener();
completed();
}

public final void addListener(final ThreadCompleteListener listener) {
Expand All @@ -79,9 +77,9 @@ public final void removeListener(final ThreadCompleteListener listener) {
this.eventListener = null;
}

private final void notifyListener() {
private final void completed() {
if (eventListener != null) {
eventListener.notifyOfThreadComplete(eventId);
eventListener.completedEventHandler(eventId);
}
}

Expand Down

0 comments on commit 9a90f2d

Please sign in to comment.