Skip to content

Commit b37190a

Browse files
author
daniel-bryla
committed
iluwatar#502 Introduced logging in new example
1 parent 0a42771 commit b37190a

File tree

5 files changed

+64
-50
lines changed

5 files changed

+64
-50
lines changed

caching/src/main/java/com/iluwatar/caching/App.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ public void useReadThroughAndWriteBehindStrategy() {
149149
* Cache-Aside
150150
*/
151151
public void useCacheAsideStategy() {
152-
System.out.println("# CachingPolicy.ASIDE");
152+
LOGGER.info("# CachingPolicy.ASIDE");
153153
AppManager.initCachingPolicy(CachingPolicy.ASIDE);
154-
System.out.println(AppManager.printCacheContent());
154+
LOGGER.info(AppManager.printCacheContent());
155155

156156
UserAccount userAccount3 = new UserAccount("003", "Adam", "He likes food.");
157157
UserAccount userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
@@ -160,10 +160,10 @@ public void useCacheAsideStategy() {
160160
AppManager.save(userAccount4);
161161
AppManager.save(userAccount5);
162162

163-
System.out.println(AppManager.printCacheContent());
163+
LOGGER.info(AppManager.printCacheContent());
164164
AppManager.find("003");
165-
System.out.println(AppManager.printCacheContent());
165+
LOGGER.info(AppManager.printCacheContent());
166166
AppManager.find("004");
167-
System.out.println(AppManager.printCacheContent());
167+
LOGGER.info(AppManager.printCacheContent());
168168
}
169169
}

event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/App.java

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,43 @@
1616
*/
1717
package com.iluwatar.event.asynchronous;
1818

19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
1922
import java.io.IOException;
2023
import java.io.InputStream;
2124
import java.util.Properties;
2225
import 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
*/
4952
public 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();

event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/Event.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616
*/
1717
package com.iluwatar.event.asynchronous;
1818

19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
1922
/**
20-
*
23+
*
2124
* Each Event runs as a separate/individual thread.
2225
*
2326
*/
2427
public class Event implements IEvent, Runnable {
2528

29+
private static final Logger LOGGER = LoggerFactory.getLogger(Event.class);
30+
2631
private int eventId;
2732
private int eventTime;
2833
private boolean isSynchronous;
@@ -31,7 +36,7 @@ public class Event implements IEvent, Runnable {
3136
private ThreadCompleteListener eventListener;
3237

3338
/**
34-
*
39+
*
3540
* @param eventId event ID
3641
* @param eventTime event time
3742
* @param isSynchronous is of synchronous type
@@ -63,9 +68,9 @@ public void stop() {
6368
@Override
6469
public void status() {
6570
if (!isComplete) {
66-
System.out.println("[" + eventId + "] is not done.");
71+
LOGGER.info("[{}] is not done.", eventId);
6772
} else {
68-
System.out.println("[" + eventId + "] is done.");
73+
LOGGER.info("[{}] is done.", eventId);
6974
}
7075
}
7176

event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/EventManager.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.concurrent.ConcurrentHashMap;
2323

2424
/**
25-
*
25+
*
2626
* EventManager handles and maintains a pool of event threads. {@link Event} threads are created upon user request. Thre
2727
* are two types of events; Asynchronous and Synchronous. There can be multiple Asynchronous events running at once but
2828
* only one Synchronous event running at a time. Currently supported event operations are: start, stop, and getStatus.
@@ -52,7 +52,7 @@ public EventManager() {
5252

5353
/**
5454
* Create a Synchronous event.
55-
*
55+
*
5656
* @param eventTime Time an event should run for.
5757
* @return eventId
5858
* @throws MaxNumOfEventsAllowedException When too many events are running at a time.
@@ -74,7 +74,7 @@ public int create(int eventTime)
7474

7575
/**
7676
* Create an Asynchronous event.
77-
*
77+
*
7878
* @param eventTime Time an event should run for.
7979
* @return eventId
8080
* @throws MaxNumOfEventsAllowedException When too many events are running at a time.
@@ -106,7 +106,7 @@ private int createEvent(int eventTime, boolean isSynchronous)
106106

107107
/**
108108
* Starts event.
109-
*
109+
*
110110
* @param eventId The event that needs to be started.
111111
* @throws EventDoesNotExistException If event does not exist in our eventPool.
112112
*/
@@ -120,7 +120,7 @@ public void start(int eventId) throws EventDoesNotExistException {
120120

121121
/**
122122
* Stops event.
123-
*
123+
*
124124
* @param eventId The event that needs to be stopped.
125125
* @throws EventDoesNotExistException If event does not exist in our eventPool.
126126
*/
@@ -139,7 +139,7 @@ public void cancel(int eventId) throws EventDoesNotExistException {
139139

140140
/**
141141
* Get status of a running event.
142-
*
142+
*
143143
* @param eventId The event to inquire status of.
144144
* @throws EventDoesNotExistException If event does not exist in our eventPool.
145145
*/

event-asynchronous/src/test/java/com/iluwatar/event/asynchronous/EventAsynchronousTest.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
*/
1717
package com.iluwatar.event.asynchronous;
1818

19-
import static org.junit.Assert.assertTrue;
20-
2119
import org.junit.Before;
2220
import org.junit.Test;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
24+
import static org.junit.Assert.assertTrue;
2325

2426
/**
2527
*
@@ -29,6 +31,8 @@
2931
public class EventAsynchronousTest {
3032
App app;
3133

34+
private static final Logger LOGGER = LoggerFactory.getLogger(EventAsynchronousTest.class);
35+
3236
@Before
3337
public void setUp() {
3438
app = new App();
@@ -46,7 +50,7 @@ public void testAsynchronousEvent() {
4650
eventManager.cancel(aEventId);
4751
assertTrue(eventManager.getEventPool().size() == 0);
4852
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
49-
System.out.println(e.getMessage());
53+
LOGGER.error(e.getMessage());
5054
}
5155
}
5256

@@ -63,7 +67,7 @@ public void testSynchronousEvent() {
6367
assertTrue(eventManager.getEventPool().size() == 0);
6468
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException
6569
| InvalidOperationException e) {
66-
System.out.println(e.getMessage());
70+
LOGGER.error(e.getMessage());
6771
}
6872
}
6973

@@ -76,7 +80,7 @@ public void testUnsuccessfulSynchronousEvent() throws InvalidOperationException
7680
sEventId = eventManager.create(60);
7781
eventManager.start(sEventId);
7882
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
79-
System.out.println(e.getMessage());
83+
LOGGER.error(e.getMessage());
8084
}
8185
}
8286

@@ -101,7 +105,7 @@ public void testFullSynchronousEvent() {
101105

102106
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException
103107
| InvalidOperationException e) {
104-
System.out.println(e.getMessage());
108+
LOGGER.error(e.getMessage());
105109
}
106110
}
107111

@@ -129,7 +133,7 @@ public void testFullAsynchronousEvent() {
129133
assertTrue(eventManager.getEventPool().size() == 0);
130134

131135
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
132-
System.out.println(e.getMessage());
136+
LOGGER.error(e.getMessage());
133137
}
134138
}
135139
}

0 commit comments

Comments
 (0)