1616 */
1717package com .iluwatar .event .asynchronous ;
1818
19+ import org .slf4j .Logger ;
20+ import org .slf4j .LoggerFactory ;
21+
1922import java .io .IOException ;
2023import java .io .InputStream ;
2124import java .util .Properties ;
2225import java .util .Scanner ;
2326
2427/**
25- *
28+ *
2629 * This application demonstrates the <b>Event-based Asynchronous</b> pattern. Essentially, users (of the pattern) may
2730 * choose to run events in an Asynchronous or Synchronous mode. There can be multiple Asynchronous events running at
2831 * once but only one Synchronous event can run at a time. Asynchronous events are synonymous to multi-threads. The key
2932 * point here is that the threads run in the background and the user is free to carry on with other processes. Once an
3033 * event is complete, the appropriate listener/callback method will be called. The listener then proceeds to carry out
3134 * further processing depending on the needs of the user.
32- *
35+ *
3336 * The {@link EventManager} manages the events/threads that the user creates. Currently, the supported event operations
3437 * are: <code>start</code>, <code>stop</code>, <code>getStatus</code>. For Synchronous events, the user is unable to
3538 * start another (Synchronous) event if one is already running at the time. The running event would have to either be
3639 * stopped or completed before a new event can be started.
37- *
40+ *
3841 * The Event-based Asynchronous Pattern makes available the advantages of multithreaded applications while hiding many
3942 * of the complex issues inherent in multithreaded design. Using a class that supports this pattern can allow you to:-
4043 * (1) Perform time-consuming tasks, such as downloads and database operations, "in the background," without
4144 * interrupting your application. (2) Execute multiple operations simultaneously, receiving notifications when each
4245 * completes. (3) Wait for resources to become available without stopping ("hanging") your application. (4) Communicate
4346 * with pending asynchronous operations using the familiar events-and-delegates model.
44- *
47+ *
4548 * @see EventManager
4649 * @see Event
4750 *
4851 */
4952public class App {
5053
54+ private static final Logger LOGGER = LoggerFactory .getLogger (App .class );
55+
5156 public static final String PROP_FILE_NAME = "config.properties" ;
5257
5358 boolean interactiveMode = false ;
@@ -77,7 +82,7 @@ public void setUp() {
7782 try {
7883 prop .load (inputStream );
7984 } 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 );
8186 }
8287 String property = prop .getProperty ("INTERACTIVE_MODE" );
8388 if (property .equalsIgnoreCase ("YES" )) {
@@ -106,27 +111,27 @@ public void quickRun() {
106111 try {
107112 // Create an Asynchronous event.
108113 int aEventId = eventManager .createAsync (60 );
109- System . out . println ("Async Event [" + aEventId + " ] has been created." );
114+ LOGGER . info ("Async Event [{} ] has been created." , aEventId );
110115 eventManager .start (aEventId );
111- System . out . println ("Async Event [" + aEventId + " ] has been started." );
116+ LOGGER . info ("Async Event [{} ] has been started." , aEventId );
112117
113118 // Create a Synchronous event.
114119 int sEventId = eventManager .create (60 );
115- System . out . println ("Sync Event [" + sEventId + " ] has been created." );
120+ LOGGER . info ("Sync Event [{} ] has been created." , sEventId );
116121 eventManager .start (sEventId );
117- System . out . println ("Sync Event [" + sEventId + " ] has been started." );
122+ LOGGER . info ("Sync Event [{} ] has been started." , sEventId );
118123
119124 eventManager .status (aEventId );
120125 eventManager .status (sEventId );
121126
122127 eventManager .cancel (aEventId );
123- System . out . println ("Async Event [" + aEventId + " ] has been stopped." );
128+ LOGGER . info ("Async Event [{} ] has been stopped." , aEventId );
124129 eventManager .cancel (sEventId );
125- System . out . println ("Sync Event [" + sEventId + " ] has been stopped." );
130+ LOGGER . info ("Sync Event [{} ] has been stopped." , sEventId );
126131
127132 } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException
128133 | InvalidOperationException e ) {
129- System . out . println (e .getMessage ());
134+ LOGGER . error (e .getMessage ());
130135 }
131136 }
132137
@@ -139,58 +144,58 @@ public void runInteractiveMode() {
139144 Scanner s = new Scanner (System .in );
140145 int option = -1 ;
141146 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]: " );
145150 option = s .nextInt ();
146151
147152 if (option == 1 ) {
148153 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)?: " );
150155 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)?: " );
152157 int eventTime = s .nextInt ();
153158 if (eventType .equalsIgnoreCase ("A" )) {
154159 try {
155160 int eventId = eventManager .createAsync (eventTime );
156161 eventManager .start (eventId );
157- System . out . println ("Egg [" + eventId + " ] is being boiled." );
162+ LOGGER . info ("Egg [{} ] is being boiled." , eventId );
158163 } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e ) {
159- System . out . println (e .getMessage ());
164+ LOGGER . error (e .getMessage ());
160165 }
161166 } else if (eventType .equalsIgnoreCase ("S" )) {
162167 try {
163168 int eventId = eventManager .create (eventTime );
164169 eventManager .start (eventId );
165- System . out . println ("Egg [" + eventId + " ] is being boiled." );
170+ LOGGER . info ("Egg [{} ] is being boiled." , eventId );
166171 } catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException
167172 | EventDoesNotExistException e ) {
168- System . out . println (e .getMessage ());
173+ LOGGER . error (e .getMessage ());
169174 }
170175 } else {
171- System . out . println ("Unknown event type." );
176+ LOGGER . info ("Unknown event type." );
172177 }
173178 } else if (option == 2 ) {
174- System . out . print ("Which egg?: " );
179+ LOGGER . info ("Which egg?: " );
175180 int eventId = s .nextInt ();
176181 try {
177182 eventManager .cancel (eventId );
178- System . out . println ("Egg [" + eventId + " ] is removed from boiler." );
183+ LOGGER . info ("Egg [{} ] is removed from boiler." , eventId );
179184 } catch (EventDoesNotExistException e ) {
180- System . out . println (e .getMessage ());
185+ LOGGER . error (e .getMessage ());
181186 }
182187 } else if (option == 3 ) {
183188 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) ?: " );
185190 String eggChoice = s .nextLine ();
186191
187192 if (eggChoice .equalsIgnoreCase ("O" )) {
188- System . out . print ("Which egg?: " );
193+ LOGGER . info ("Which egg?: " );
189194 int eventId = s .nextInt ();
190195 try {
191196 eventManager .status (eventId );
192197 } catch (EventDoesNotExistException e ) {
193- System . out . println (e .getMessage ());
198+ LOGGER . error (e .getMessage ());
194199 }
195200 } else if (eggChoice .equalsIgnoreCase ("A" )) {
196201 eventManager .statusOfAllEvents ();
0 commit comments