|
| 1 | +/** |
| 2 | + * The MIT License Copyright (c) 2014 Ilkka Seppälä |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 5 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 6 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 7 | + * permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | + * |
| 9 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 10 | + * Software. |
| 11 | + * |
| 12 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 13 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 14 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 15 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 16 | + */ |
| 17 | +package com.iluwatar.event.asynchronous; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.util.Properties; |
| 22 | +import java.util.Scanner; |
| 23 | + |
| 24 | +/** |
| 25 | + * |
| 26 | + * This application demonstrates the <b>Event-based Asynchronous</b> pattern. Essentially, users (of the pattern) may |
| 27 | + * choose to run events in an Asynchronous or Synchronous mode. There can be multiple Asynchronous events running at |
| 28 | + * once but only one Synchronous event can run at a time. Asynchronous events are synonymous to multi-threads. The key |
| 29 | + * point here is that the threads run in the background and the user is free to carry on with other processes. Once an |
| 30 | + * event is complete, the appropriate listener/callback method will be called. The listener then proceeds to carry out |
| 31 | + * further processing depending on the needs of the user. |
| 32 | + * |
| 33 | + * The {@link EventManager} manages the events/threads that the user creates. Currently, the supported event operations |
| 34 | + * are: <code>start</code>, <code>stop</code>, <code>getStatus</code>. For Synchronous events, the user is unable to |
| 35 | + * start another (Synchronous) event if one is already running at the time. The running event would have to either be |
| 36 | + * stopped or completed before a new event can be started. |
| 37 | + * |
| 38 | + * The Event-based Asynchronous Pattern makes available the advantages of multithreaded applications while hiding many |
| 39 | + * of the complex issues inherent in multithreaded design. Using a class that supports this pattern can allow you to:- |
| 40 | + * (1) Perform time-consuming tasks, such as downloads and database operations, "in the background," without |
| 41 | + * interrupting your application. (2) Execute multiple operations simultaneously, receiving notifications when each |
| 42 | + * completes. (3) Wait for resources to become available without stopping ("hanging") your application. (4) Communicate |
| 43 | + * with pending asynchronous operations using the familiar events-and-delegates model. |
| 44 | + * |
| 45 | + * @see EventManager |
| 46 | + * @see Event |
| 47 | + * |
| 48 | + */ |
| 49 | +public class App { |
| 50 | + |
| 51 | + boolean interactiveMode = false; |
| 52 | + |
| 53 | + public static void main(String[] args) { |
| 54 | + App app = new App(); |
| 55 | + |
| 56 | + app.setUp(); |
| 57 | + app.run(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * App can run in interactive mode or not. Interactive mode == Allow user interaction with command line. |
| 62 | + * Non-interactive is a quick sequential run through the available {@link EventManager} operations. |
| 63 | + */ |
| 64 | + public void setUp() { |
| 65 | + Properties prop = new Properties(); |
| 66 | + String propFileName = "config.properties"; |
| 67 | + |
| 68 | + InputStream inputStream = App.class.getClassLoader().getResourceAsStream(propFileName); |
| 69 | + |
| 70 | + if (inputStream != null) { |
| 71 | + try { |
| 72 | + prop.load(inputStream); |
| 73 | + } catch (IOException e) { |
| 74 | + } |
| 75 | + String property = prop.getProperty("INTERACTIVE_MODE"); |
| 76 | + if (property.equalsIgnoreCase("YES")) { |
| 77 | + interactiveMode = true; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public void run() { |
| 83 | + if (interactiveMode) { |
| 84 | + runInteractiveMode(); |
| 85 | + } else { |
| 86 | + quickRun(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public void quickRun() { |
| 91 | + EventManager eventManager = new EventManager(); |
| 92 | + |
| 93 | + try { |
| 94 | + // Create an Asynchronous event. |
| 95 | + int aEventID = eventManager.createAsyncEvent(60); |
| 96 | + System.out.println("Event [" + aEventID + "] has been created."); |
| 97 | + eventManager.startEvent(aEventID); |
| 98 | + System.out.println("Event [" + aEventID + "] has been started."); |
| 99 | + |
| 100 | + // Create a Synchronous event. |
| 101 | + int sEventID = eventManager.createSyncEvent(60); |
| 102 | + System.out.println("Event [" + sEventID + "] has been created."); |
| 103 | + eventManager.startEvent(sEventID); |
| 104 | + System.out.println("Event [" + sEventID + "] has been started."); |
| 105 | + |
| 106 | + eventManager.getStatus(aEventID); |
| 107 | + eventManager.getStatus(sEventID); |
| 108 | + |
| 109 | + eventManager.stopEvent(aEventID); |
| 110 | + System.out.println("Event [" + aEventID + "] has been stopped."); |
| 111 | + eventManager.stopEvent(sEventID); |
| 112 | + System.out.println("Event [" + sEventID + "] has been stopped."); |
| 113 | + |
| 114 | + } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException |
| 115 | + | InvalidOperationException e) { |
| 116 | + System.out.println(e.getMessage()); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public void runInteractiveMode() { |
| 121 | + EventManager eventManager = new EventManager(); |
| 122 | + |
| 123 | + Scanner s = new Scanner(System.in); |
| 124 | + int option = 0; |
| 125 | + option = -1; |
| 126 | + while (option != 5) { |
| 127 | + System.out |
| 128 | + .println("(1) START_EVENT \n(2) STOP_EVENT \n(3) STATUS_OF_EVENT \n(4) STATUS_OF_ALL_EVENTS \n(5) EXIT"); |
| 129 | + System.out.print("Choose [1,2,3,4,5]: "); |
| 130 | + option = s.nextInt(); |
| 131 | + |
| 132 | + if (option == 1) { |
| 133 | + s.nextLine(); |
| 134 | + System.out.print("(A)sync or (S)ync event?: "); |
| 135 | + String eventType = s.nextLine(); |
| 136 | + System.out.print("How long should this event run for (in seconds)?: "); |
| 137 | + int eventTime = s.nextInt(); |
| 138 | + if (eventType.equalsIgnoreCase("A")) { |
| 139 | + try { |
| 140 | + int eventID = eventManager.createAsyncEvent(eventTime); |
| 141 | + System.out.println("Event [" + eventID + "] has been created."); |
| 142 | + eventManager.startEvent(eventID); |
| 143 | + System.out.println("Event [" + eventID + "] has been started."); |
| 144 | + } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) { |
| 145 | + System.out.println(e.getMessage()); |
| 146 | + } |
| 147 | + } else if (eventType.equalsIgnoreCase("S")) { |
| 148 | + try { |
| 149 | + int eventID = eventManager.createSyncEvent(eventTime); |
| 150 | + System.out.println("Event [" + eventID + "] has been created."); |
| 151 | + eventManager.startEvent(eventID); |
| 152 | + System.out.println("Event [" + eventID + "] has been started."); |
| 153 | + } catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException |
| 154 | + | EventDoesNotExistException e) { |
| 155 | + System.out.println(e.getMessage()); |
| 156 | + } |
| 157 | + } else { |
| 158 | + System.out.println("Unknown event type."); |
| 159 | + } |
| 160 | + } else if (option == 2) { |
| 161 | + System.out.print("Event ID: "); |
| 162 | + int eventID = s.nextInt(); |
| 163 | + try { |
| 164 | + eventManager.stopEvent(eventID); |
| 165 | + System.out.println("Event [" + eventID + "] has been stopped."); |
| 166 | + } catch (EventDoesNotExistException e) { |
| 167 | + System.out.println(e.getMessage()); |
| 168 | + } |
| 169 | + } else if (option == 3) { |
| 170 | + System.out.print("Event ID: "); |
| 171 | + int eventID = s.nextInt(); |
| 172 | + try { |
| 173 | + eventManager.getStatus(eventID); |
| 174 | + } catch (EventDoesNotExistException e) { |
| 175 | + System.out.println(e.getMessage()); |
| 176 | + } |
| 177 | + } else if (option == 4) { |
| 178 | + eventManager.getStatusOfAllEvents(); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + s.close(); |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments