#502 Introduced logging in new example

This commit is contained in:
daniel-bryla 2016-11-04 12:19:32 +01:00
parent 0a427710bb
commit b37190a214
5 changed files with 64 additions and 50 deletions

View File

@ -149,9 +149,9 @@ public class App {
* Cache-Aside
*/
public void useCacheAsideStategy() {
System.out.println("# CachingPolicy.ASIDE");
LOGGER.info("# CachingPolicy.ASIDE");
AppManager.initCachingPolicy(CachingPolicy.ASIDE);
System.out.println(AppManager.printCacheContent());
LOGGER.info(AppManager.printCacheContent());
UserAccount userAccount3 = new UserAccount("003", "Adam", "He likes food.");
UserAccount userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
@ -160,10 +160,10 @@ public class App {
AppManager.save(userAccount4);
AppManager.save(userAccount5);
System.out.println(AppManager.printCacheContent());
LOGGER.info(AppManager.printCacheContent());
AppManager.find("003");
System.out.println(AppManager.printCacheContent());
LOGGER.info(AppManager.printCacheContent());
AppManager.find("004");
System.out.println(AppManager.printCacheContent());
LOGGER.info(AppManager.printCacheContent());
}
}

View File

@ -16,6 +16,9 @@
*/
package com.iluwatar.event.asynchronous;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
@ -48,6 +51,8 @@ import java.util.Scanner;
*/
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
public static final String PROP_FILE_NAME = "config.properties";
boolean interactiveMode = false;
@ -77,7 +82,7 @@ public class App {
try {
prop.load(inputStream);
} catch (IOException e) {
System.out.println(PROP_FILE_NAME + " was not found. Defaulting to non-interactive mode.");
LOGGER.error("{} was not found. Defaulting to non-interactive mode.", PROP_FILE_NAME, e);
}
String property = prop.getProperty("INTERACTIVE_MODE");
if (property.equalsIgnoreCase("YES")) {
@ -106,27 +111,27 @@ public class App {
try {
// Create an Asynchronous event.
int aEventId = eventManager.createAsync(60);
System.out.println("Async Event [" + aEventId + "] has been created.");
LOGGER.info("Async Event [{}] has been created.", aEventId);
eventManager.start(aEventId);
System.out.println("Async Event [" + aEventId + "] has been started.");
LOGGER.info("Async Event [{}] has been started.", aEventId);
// Create a Synchronous event.
int sEventId = eventManager.create(60);
System.out.println("Sync Event [" + sEventId + "] has been created.");
LOGGER.info("Sync Event [{}] has been created.", sEventId);
eventManager.start(sEventId);
System.out.println("Sync Event [" + sEventId + "] has been started.");
LOGGER.info("Sync Event [{}] has been started.", sEventId);
eventManager.status(aEventId);
eventManager.status(sEventId);
eventManager.cancel(aEventId);
System.out.println("Async Event [" + aEventId + "] has been stopped.");
LOGGER.info("Async Event [{}] has been stopped.", aEventId);
eventManager.cancel(sEventId);
System.out.println("Sync Event [" + sEventId + "] has been stopped.");
LOGGER.info("Sync Event [{}] has been stopped.", sEventId);
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException
| InvalidOperationException e) {
System.out.println(e.getMessage());
LOGGER.error(e.getMessage());
}
}
@ -139,58 +144,58 @@ public class App {
Scanner s = new Scanner(System.in);
int option = -1;
while (option != 4) {
System.out.println("Hello. Would you like to boil some eggs?");
System.out.println("(1) BOIL AN EGG \n(2) STOP BOILING THIS EGG \n(3) HOW ARE MY EGGS? \n(4) EXIT");
System.out.print("Choose [1,2,3,4]: ");
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]: ");
option = s.nextInt();
if (option == 1) {
s.nextLine();
System.out.print("Boil multiple eggs at once (A) or boil them one-by-one (S)?: ");
LOGGER.info("Boil multiple eggs at once (A) or boil them one-by-one (S)?: ");
String eventType = s.nextLine();
System.out.print("How long should this egg be boiled for (in seconds)?: ");
LOGGER.info("How long should this egg be boiled for (in seconds)?: ");
int eventTime = s.nextInt();
if (eventType.equalsIgnoreCase("A")) {
try {
int eventId = eventManager.createAsync(eventTime);
eventManager.start(eventId);
System.out.println("Egg [" + eventId + "] is being boiled.");
LOGGER.info("Egg [{}] is being boiled.", eventId);
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
System.out.println(e.getMessage());
LOGGER.error(e.getMessage());
}
} else if (eventType.equalsIgnoreCase("S")) {
try {
int eventId = eventManager.create(eventTime);
eventManager.start(eventId);
System.out.println("Egg [" + eventId + "] is being boiled.");
LOGGER.info("Egg [{}] is being boiled.", eventId);
} catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException
| EventDoesNotExistException e) {
System.out.println(e.getMessage());
LOGGER.error(e.getMessage());
}
} else {
System.out.println("Unknown event type.");
LOGGER.info("Unknown event type.");
}
} else if (option == 2) {
System.out.print("Which egg?: ");
LOGGER.info("Which egg?: ");
int eventId = s.nextInt();
try {
eventManager.cancel(eventId);
System.out.println("Egg [" + eventId + "] is removed from boiler.");
LOGGER.info("Egg [{}] is removed from boiler.", eventId);
} catch (EventDoesNotExistException e) {
System.out.println(e.getMessage());
LOGGER.error(e.getMessage());
}
} else if (option == 3) {
s.nextLine();
System.out.print("Just one egg (O) OR all of them (A) ?: ");
LOGGER.info("Just one egg (O) OR all of them (A) ?: ");
String eggChoice = s.nextLine();
if (eggChoice.equalsIgnoreCase("O")) {
System.out.print("Which egg?: ");
LOGGER.info("Which egg?: ");
int eventId = s.nextInt();
try {
eventManager.status(eventId);
} catch (EventDoesNotExistException e) {
System.out.println(e.getMessage());
LOGGER.error(e.getMessage());
}
} else if (eggChoice.equalsIgnoreCase("A")) {
eventManager.statusOfAllEvents();

View File

@ -16,6 +16,9 @@
*/
package com.iluwatar.event.asynchronous;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* Each Event runs as a separate/individual thread.
@ -23,6 +26,8 @@ package com.iluwatar.event.asynchronous;
*/
public class Event implements IEvent, Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(Event.class);
private int eventId;
private int eventTime;
private boolean isSynchronous;
@ -63,9 +68,9 @@ public class Event implements IEvent, Runnable {
@Override
public void status() {
if (!isComplete) {
System.out.println("[" + eventId + "] is not done.");
LOGGER.info("[{}] is not done.", eventId);
} else {
System.out.println("[" + eventId + "] is done.");
LOGGER.info("[{}] is done.", eventId);
}
}

View File

@ -16,10 +16,12 @@
*/
package com.iluwatar.event.asynchronous;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.Assert.assertTrue;
/**
*
@ -29,6 +31,8 @@ import org.junit.Test;
public class EventAsynchronousTest {
App app;
private static final Logger LOGGER = LoggerFactory.getLogger(EventAsynchronousTest.class);
@Before
public void setUp() {
app = new App();
@ -46,7 +50,7 @@ public class EventAsynchronousTest {
eventManager.cancel(aEventId);
assertTrue(eventManager.getEventPool().size() == 0);
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
System.out.println(e.getMessage());
LOGGER.error(e.getMessage());
}
}
@ -63,7 +67,7 @@ public class EventAsynchronousTest {
assertTrue(eventManager.getEventPool().size() == 0);
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException
| InvalidOperationException e) {
System.out.println(e.getMessage());
LOGGER.error(e.getMessage());
}
}
@ -76,7 +80,7 @@ public class EventAsynchronousTest {
sEventId = eventManager.create(60);
eventManager.start(sEventId);
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
System.out.println(e.getMessage());
LOGGER.error(e.getMessage());
}
}
@ -101,7 +105,7 @@ public class EventAsynchronousTest {
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException
| InvalidOperationException e) {
System.out.println(e.getMessage());
LOGGER.error(e.getMessage());
}
}
@ -129,7 +133,7 @@ public class EventAsynchronousTest {
assertTrue(eventManager.getEventPool().size() == 0);
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
System.out.println(e.getMessage());
LOGGER.error(e.getMessage());
}
}
}