|
16 | 16 | */ |
17 | 17 | package com.iluwatar.event.asynchronous; |
18 | 18 |
|
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | + |
19 | 22 | import java.io.IOException; |
20 | 23 | import java.io.InputStream; |
21 | 24 | import java.util.Properties; |
22 | 25 | import java.util.Scanner; |
23 | 26 |
|
24 | 27 | /** |
25 | | - * |
| 28 | + * |
26 | 29 | * This application demonstrates the <b>Event-based Asynchronous</b> pattern. Essentially, users (of the pattern) may |
27 | 30 | * choose to run events in an Asynchronous or Synchronous mode. There can be multiple Asynchronous events running at |
28 | 31 | * once but only one Synchronous event can run at a time. Asynchronous events are synonymous to multi-threads. The key |
29 | 32 | * point here is that the threads run in the background and the user is free to carry on with other processes. Once an |
30 | 33 | * event is complete, the appropriate listener/callback method will be called. The listener then proceeds to carry out |
31 | 34 | * further processing depending on the needs of the user. |
32 | | - * |
| 35 | + * |
33 | 36 | * The {@link EventManager} manages the events/threads that the user creates. Currently, the supported event operations |
34 | 37 | * are: <code>start</code>, <code>stop</code>, <code>getStatus</code>. For Synchronous events, the user is unable to |
35 | 38 | * start another (Synchronous) event if one is already running at the time. The running event would have to either be |
36 | 39 | * stopped or completed before a new event can be started. |
37 | | - * |
| 40 | + * |
38 | 41 | * The Event-based Asynchronous Pattern makes available the advantages of multithreaded applications while hiding many |
39 | 42 | * of the complex issues inherent in multithreaded design. Using a class that supports this pattern can allow you to:- |
40 | 43 | * (1) Perform time-consuming tasks, such as downloads and database operations, "in the background," without |
41 | 44 | * interrupting your application. (2) Execute multiple operations simultaneously, receiving notifications when each |
42 | 45 | * completes. (3) Wait for resources to become available without stopping ("hanging") your application. (4) Communicate |
43 | 46 | * with pending asynchronous operations using the familiar events-and-delegates model. |
44 | | - * |
| 47 | + * |
45 | 48 | * @see EventManager |
46 | 49 | * @see Event |
47 | 50 | * |
48 | 51 | */ |
49 | 52 | public class App { |
50 | 53 |
|
| 54 | + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); |
| 55 | + |
51 | 56 | public static final String PROP_FILE_NAME = "config.properties"; |
52 | 57 |
|
53 | 58 | boolean interactiveMode = false; |
@@ -77,7 +82,7 @@ public void setUp() { |
77 | 82 | try { |
78 | 83 | prop.load(inputStream); |
79 | 84 | } catch (IOException e) { |
80 | | - System.out.println(PROP_FILE_NAME + " was not found. Defaulting to non-interactive mode."); |
| 85 | + LOGGER.error("{} was not found. Defaulting to non-interactive mode.", PROP_FILE_NAME, e); |
81 | 86 | } |
82 | 87 | String property = prop.getProperty("INTERACTIVE_MODE"); |
83 | 88 | if (property.equalsIgnoreCase("YES")) { |
@@ -106,27 +111,27 @@ public void quickRun() { |
106 | 111 | try { |
107 | 112 | // Create an Asynchronous event. |
108 | 113 | int aEventId = eventManager.createAsync(60); |
109 | | - System.out.println("Async Event [" + aEventId + "] has been created."); |
| 114 | + LOGGER.info("Async Event [{}] has been created.", aEventId); |
110 | 115 | eventManager.start(aEventId); |
111 | | - System.out.println("Async Event [" + aEventId + "] has been started."); |
| 116 | + LOGGER.info("Async Event [{}] has been started.", aEventId); |
112 | 117 |
|
113 | 118 | // Create a Synchronous event. |
114 | 119 | int sEventId = eventManager.create(60); |
115 | | - System.out.println("Sync Event [" + sEventId + "] has been created."); |
| 120 | + LOGGER.info("Sync Event [{}] has been created.", sEventId); |
116 | 121 | eventManager.start(sEventId); |
117 | | - System.out.println("Sync Event [" + sEventId + "] has been started."); |
| 122 | + LOGGER.info("Sync Event [{}] has been started.", sEventId); |
118 | 123 |
|
119 | 124 | eventManager.status(aEventId); |
120 | 125 | eventManager.status(sEventId); |
121 | 126 |
|
122 | 127 | eventManager.cancel(aEventId); |
123 | | - System.out.println("Async Event [" + aEventId + "] has been stopped."); |
| 128 | + LOGGER.info("Async Event [{}] has been stopped.", aEventId); |
124 | 129 | eventManager.cancel(sEventId); |
125 | | - System.out.println("Sync Event [" + sEventId + "] has been stopped."); |
| 130 | + LOGGER.info("Sync Event [{}] has been stopped.", sEventId); |
126 | 131 |
|
127 | 132 | } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException |
128 | 133 | | InvalidOperationException e) { |
129 | | - System.out.println(e.getMessage()); |
| 134 | + LOGGER.error(e.getMessage()); |
130 | 135 | } |
131 | 136 | } |
132 | 137 |
|
@@ -139,58 +144,58 @@ public void runInteractiveMode() { |
139 | 144 | Scanner s = new Scanner(System.in); |
140 | 145 | int option = -1; |
141 | 146 | while (option != 4) { |
142 | | - System.out.println("Hello. Would you like to boil some eggs?"); |
143 | | - System.out.println("(1) BOIL AN EGG \n(2) STOP BOILING THIS EGG \n(3) HOW ARE MY EGGS? \n(4) EXIT"); |
144 | | - System.out.print("Choose [1,2,3,4]: "); |
| 147 | + LOGGER.info("Hello. Would you like to boil some eggs?"); |
| 148 | + LOGGER.info("(1) BOIL AN EGG \n(2) STOP BOILING THIS EGG \n(3) HOW ARE MY EGGS? \n(4) EXIT"); |
| 149 | + LOGGER.info("Choose [1,2,3,4]: "); |
145 | 150 | option = s.nextInt(); |
146 | 151 |
|
147 | 152 | if (option == 1) { |
148 | 153 | s.nextLine(); |
149 | | - System.out.print("Boil multiple eggs at once (A) or boil them one-by-one (S)?: "); |
| 154 | + LOGGER.info("Boil multiple eggs at once (A) or boil them one-by-one (S)?: "); |
150 | 155 | String eventType = s.nextLine(); |
151 | | - System.out.print("How long should this egg be boiled for (in seconds)?: "); |
| 156 | + LOGGER.info("How long should this egg be boiled for (in seconds)?: "); |
152 | 157 | int eventTime = s.nextInt(); |
153 | 158 | if (eventType.equalsIgnoreCase("A")) { |
154 | 159 | try { |
155 | 160 | int eventId = eventManager.createAsync(eventTime); |
156 | 161 | eventManager.start(eventId); |
157 | | - System.out.println("Egg [" + eventId + "] is being boiled."); |
| 162 | + LOGGER.info("Egg [{}] is being boiled.", eventId); |
158 | 163 | } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) { |
159 | | - System.out.println(e.getMessage()); |
| 164 | + LOGGER.error(e.getMessage()); |
160 | 165 | } |
161 | 166 | } else if (eventType.equalsIgnoreCase("S")) { |
162 | 167 | try { |
163 | 168 | int eventId = eventManager.create(eventTime); |
164 | 169 | eventManager.start(eventId); |
165 | | - System.out.println("Egg [" + eventId + "] is being boiled."); |
| 170 | + LOGGER.info("Egg [{}] is being boiled.", eventId); |
166 | 171 | } catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException |
167 | 172 | | EventDoesNotExistException e) { |
168 | | - System.out.println(e.getMessage()); |
| 173 | + LOGGER.error(e.getMessage()); |
169 | 174 | } |
170 | 175 | } else { |
171 | | - System.out.println("Unknown event type."); |
| 176 | + LOGGER.info("Unknown event type."); |
172 | 177 | } |
173 | 178 | } else if (option == 2) { |
174 | | - System.out.print("Which egg?: "); |
| 179 | + LOGGER.info("Which egg?: "); |
175 | 180 | int eventId = s.nextInt(); |
176 | 181 | try { |
177 | 182 | eventManager.cancel(eventId); |
178 | | - System.out.println("Egg [" + eventId + "] is removed from boiler."); |
| 183 | + LOGGER.info("Egg [{}] is removed from boiler.", eventId); |
179 | 184 | } catch (EventDoesNotExistException e) { |
180 | | - System.out.println(e.getMessage()); |
| 185 | + LOGGER.error(e.getMessage()); |
181 | 186 | } |
182 | 187 | } else if (option == 3) { |
183 | 188 | s.nextLine(); |
184 | | - System.out.print("Just one egg (O) OR all of them (A) ?: "); |
| 189 | + LOGGER.info("Just one egg (O) OR all of them (A) ?: "); |
185 | 190 | String eggChoice = s.nextLine(); |
186 | 191 |
|
187 | 192 | if (eggChoice.equalsIgnoreCase("O")) { |
188 | | - System.out.print("Which egg?: "); |
| 193 | + LOGGER.info("Which egg?: "); |
189 | 194 | int eventId = s.nextInt(); |
190 | 195 | try { |
191 | 196 | eventManager.status(eventId); |
192 | 197 | } catch (EventDoesNotExistException e) { |
193 | | - System.out.println(e.getMessage()); |
| 198 | + LOGGER.error(e.getMessage()); |
194 | 199 | } |
195 | 200 | } else if (eggChoice.equalsIgnoreCase("A")) { |
196 | 201 | eventManager.statusOfAllEvents(); |
|