2016-08-08 23:31:41 +01:00
|
|
|
/**
|
2016-11-27 14:34:20 +02:00
|
|
|
* The MIT License Copyright (c) 2014-2016 Ilkka Seppälä
|
2016-08-08 23:31:41 +01:00
|
|
|
*
|
|
|
|
* 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 com.iluwatar.event.asynchronous;
|
|
|
|
|
2016-11-04 12:19:32 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2016-08-08 23:31:41 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
|
|
/**
|
2016-11-04 12:19:32 +01:00
|
|
|
*
|
2016-08-08 23:31:41 +01:00
|
|
|
* This application demonstrates the <b>Event-based Asynchronous</b> pattern. Essentially, users (of the pattern) may
|
|
|
|
* choose to run events in an Asynchronous or Synchronous mode. There can be multiple Asynchronous events running at
|
|
|
|
* once but only one Synchronous event can run at a time. Asynchronous events are synonymous to multi-threads. The key
|
|
|
|
* point here is that the threads run in the background and the user is free to carry on with other processes. Once an
|
|
|
|
* event is complete, the appropriate listener/callback method will be called. The listener then proceeds to carry out
|
|
|
|
* further processing depending on the needs of the user.
|
2016-11-04 12:19:32 +01:00
|
|
|
*
|
2016-08-08 23:31:41 +01:00
|
|
|
* The {@link EventManager} manages the events/threads that the user creates. Currently, the supported event operations
|
|
|
|
* are: <code>start</code>, <code>stop</code>, <code>getStatus</code>. For Synchronous events, the user is unable to
|
|
|
|
* start another (Synchronous) event if one is already running at the time. The running event would have to either be
|
|
|
|
* stopped or completed before a new event can be started.
|
2016-11-04 12:19:32 +01:00
|
|
|
*
|
2016-08-08 23:31:41 +01:00
|
|
|
* The Event-based Asynchronous Pattern makes available the advantages of multithreaded applications while hiding many
|
|
|
|
* of the complex issues inherent in multithreaded design. Using a class that supports this pattern can allow you to:-
|
|
|
|
* (1) Perform time-consuming tasks, such as downloads and database operations, "in the background," without
|
|
|
|
* interrupting your application. (2) Execute multiple operations simultaneously, receiving notifications when each
|
|
|
|
* completes. (3) Wait for resources to become available without stopping ("hanging") your application. (4) Communicate
|
|
|
|
* with pending asynchronous operations using the familiar events-and-delegates model.
|
2016-11-04 12:19:32 +01:00
|
|
|
*
|
2016-08-08 23:31:41 +01:00
|
|
|
* @see EventManager
|
|
|
|
* @see Event
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class App {
|
|
|
|
|
2016-11-04 12:19:32 +01:00
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
|
|
|
|
2016-09-11 18:45:51 +01:00
|
|
|
public static final String PROP_FILE_NAME = "config.properties";
|
|
|
|
|
2016-08-08 23:31:41 +01:00
|
|
|
boolean interactiveMode = false;
|
|
|
|
|
2016-08-09 00:32:05 +01:00
|
|
|
/**
|
|
|
|
* Program entry point.
|
|
|
|
*
|
|
|
|
* @param args command line args
|
|
|
|
*/
|
2016-08-08 23:31:41 +01:00
|
|
|
public static void main(String[] args) {
|
|
|
|
App app = new App();
|
|
|
|
|
|
|
|
app.setUp();
|
|
|
|
app.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* App can run in interactive mode or not. Interactive mode == Allow user interaction with command line.
|
|
|
|
* Non-interactive is a quick sequential run through the available {@link EventManager} operations.
|
|
|
|
*/
|
|
|
|
public void setUp() {
|
|
|
|
Properties prop = new Properties();
|
|
|
|
|
2016-09-11 18:45:51 +01:00
|
|
|
InputStream inputStream = App.class.getClassLoader().getResourceAsStream(PROP_FILE_NAME);
|
2016-08-08 23:31:41 +01:00
|
|
|
|
|
|
|
if (inputStream != null) {
|
|
|
|
try {
|
|
|
|
prop.load(inputStream);
|
|
|
|
} catch (IOException e) {
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.error("{} was not found. Defaulting to non-interactive mode.", PROP_FILE_NAME, e);
|
2016-08-08 23:31:41 +01:00
|
|
|
}
|
|
|
|
String property = prop.getProperty("INTERACTIVE_MODE");
|
|
|
|
if (property.equalsIgnoreCase("YES")) {
|
|
|
|
interactiveMode = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 00:32:05 +01:00
|
|
|
/**
|
|
|
|
* Run program in either interactive mode or not.
|
|
|
|
*/
|
2016-08-08 23:31:41 +01:00
|
|
|
public void run() {
|
|
|
|
if (interactiveMode) {
|
|
|
|
runInteractiveMode();
|
|
|
|
} else {
|
|
|
|
quickRun();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 00:32:05 +01:00
|
|
|
/**
|
|
|
|
* Run program in non-interactive mode.
|
|
|
|
*/
|
2016-08-08 23:31:41 +01:00
|
|
|
public void quickRun() {
|
|
|
|
EventManager eventManager = new EventManager();
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Create an Asynchronous event.
|
2016-09-11 18:45:51 +01:00
|
|
|
int aEventId = eventManager.createAsync(60);
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.info("Async Event [{}] has been created.", aEventId);
|
2016-09-11 18:45:51 +01:00
|
|
|
eventManager.start(aEventId);
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.info("Async Event [{}] has been started.", aEventId);
|
2016-08-08 23:31:41 +01:00
|
|
|
|
|
|
|
// Create a Synchronous event.
|
2016-09-11 18:45:51 +01:00
|
|
|
int sEventId = eventManager.create(60);
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.info("Sync Event [{}] has been created.", sEventId);
|
2016-09-11 18:45:51 +01:00
|
|
|
eventManager.start(sEventId);
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.info("Sync Event [{}] has been started.", sEventId);
|
2016-08-08 23:31:41 +01:00
|
|
|
|
2016-09-11 18:45:51 +01:00
|
|
|
eventManager.status(aEventId);
|
|
|
|
eventManager.status(sEventId);
|
2016-08-08 23:31:41 +01:00
|
|
|
|
2016-09-11 18:45:51 +01:00
|
|
|
eventManager.cancel(aEventId);
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.info("Async Event [{}] has been stopped.", aEventId);
|
2016-09-11 18:45:51 +01:00
|
|
|
eventManager.cancel(sEventId);
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.info("Sync Event [{}] has been stopped.", sEventId);
|
2016-08-08 23:31:41 +01:00
|
|
|
|
|
|
|
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException
|
|
|
|
| InvalidOperationException e) {
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.error(e.getMessage());
|
2016-08-08 23:31:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 00:32:05 +01:00
|
|
|
/**
|
|
|
|
* Run program in interactive mode.
|
|
|
|
*/
|
2016-08-08 23:31:41 +01:00
|
|
|
public void runInteractiveMode() {
|
|
|
|
EventManager eventManager = new EventManager();
|
|
|
|
|
|
|
|
Scanner s = new Scanner(System.in);
|
2016-09-11 18:45:51 +01:00
|
|
|
int option = -1;
|
2016-09-19 21:50:04 +01:00
|
|
|
while (option != 4) {
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.info("Hello. Would you like to boil some eggs?");
|
|
|
|
LOGGER.info("(1) BOIL AN EGG \n(2) STOP BOILING THIS EGG \n(3) HOW ARE MY EGGS? \n(4) EXIT");
|
|
|
|
LOGGER.info("Choose [1,2,3,4]: ");
|
2016-08-08 23:31:41 +01:00
|
|
|
option = s.nextInt();
|
|
|
|
|
|
|
|
if (option == 1) {
|
|
|
|
s.nextLine();
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.info("Boil multiple eggs at once (A) or boil them one-by-one (S)?: ");
|
2016-08-08 23:31:41 +01:00
|
|
|
String eventType = s.nextLine();
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.info("How long should this egg be boiled for (in seconds)?: ");
|
2016-08-08 23:31:41 +01:00
|
|
|
int eventTime = s.nextInt();
|
|
|
|
if (eventType.equalsIgnoreCase("A")) {
|
|
|
|
try {
|
2016-09-11 18:45:51 +01:00
|
|
|
int eventId = eventManager.createAsync(eventTime);
|
|
|
|
eventManager.start(eventId);
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.info("Egg [{}] is being boiled.", eventId);
|
2016-08-08 23:31:41 +01:00
|
|
|
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.error(e.getMessage());
|
2016-08-08 23:31:41 +01:00
|
|
|
}
|
|
|
|
} else if (eventType.equalsIgnoreCase("S")) {
|
|
|
|
try {
|
2016-09-11 18:45:51 +01:00
|
|
|
int eventId = eventManager.create(eventTime);
|
|
|
|
eventManager.start(eventId);
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.info("Egg [{}] is being boiled.", eventId);
|
2016-08-08 23:31:41 +01:00
|
|
|
} catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException
|
|
|
|
| EventDoesNotExistException e) {
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.error(e.getMessage());
|
2016-08-08 23:31:41 +01:00
|
|
|
}
|
|
|
|
} else {
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.info("Unknown event type.");
|
2016-08-08 23:31:41 +01:00
|
|
|
}
|
|
|
|
} else if (option == 2) {
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.info("Which egg?: ");
|
2016-08-09 00:32:05 +01:00
|
|
|
int eventId = s.nextInt();
|
2016-08-08 23:31:41 +01:00
|
|
|
try {
|
2016-09-11 18:45:51 +01:00
|
|
|
eventManager.cancel(eventId);
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.info("Egg [{}] is removed from boiler.", eventId);
|
2016-08-08 23:31:41 +01:00
|
|
|
} catch (EventDoesNotExistException e) {
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.error(e.getMessage());
|
2016-08-08 23:31:41 +01:00
|
|
|
}
|
|
|
|
} else if (option == 3) {
|
2016-09-19 21:50:04 +01:00
|
|
|
s.nextLine();
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.info("Just one egg (O) OR all of them (A) ?: ");
|
2016-09-19 21:50:04 +01:00
|
|
|
String eggChoice = s.nextLine();
|
|
|
|
|
|
|
|
if (eggChoice.equalsIgnoreCase("O")) {
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.info("Which egg?: ");
|
2016-09-19 21:50:04 +01:00
|
|
|
int eventId = s.nextInt();
|
|
|
|
try {
|
|
|
|
eventManager.status(eventId);
|
|
|
|
} catch (EventDoesNotExistException e) {
|
2016-11-04 12:19:32 +01:00
|
|
|
LOGGER.error(e.getMessage());
|
2016-09-19 21:50:04 +01:00
|
|
|
}
|
|
|
|
} else if (eggChoice.equalsIgnoreCase("A")) {
|
|
|
|
eventManager.statusOfAllEvents();
|
2016-08-08 23:31:41 +01:00
|
|
|
}
|
|
|
|
} else if (option == 4) {
|
2016-09-11 18:45:51 +01:00
|
|
|
eventManager.shutdown();
|
2016-08-08 23:31:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|