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

View File

@ -16,6 +16,9 @@
*/ */
package com.iluwatar.event.asynchronous; package com.iluwatar.event.asynchronous;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Properties; import java.util.Properties;
@ -48,6 +51,8 @@ import java.util.Scanner;
*/ */
public class App { public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
public static final String PROP_FILE_NAME = "config.properties"; public static final String PROP_FILE_NAME = "config.properties";
boolean interactiveMode = false; boolean interactiveMode = false;
@ -77,7 +82,7 @@ public class App {
try { try {
prop.load(inputStream); prop.load(inputStream);
} catch (IOException e) { } 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"); String property = prop.getProperty("INTERACTIVE_MODE");
if (property.equalsIgnoreCase("YES")) { if (property.equalsIgnoreCase("YES")) {
@ -106,27 +111,27 @@ public class App {
try { try {
// Create an Asynchronous event. // Create an Asynchronous event.
int aEventId = eventManager.createAsync(60); 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); eventManager.start(aEventId);
System.out.println("Async Event [" + aEventId + "] has been started."); LOGGER.info("Async Event [{}] has been started.", aEventId);
// Create a Synchronous event. // Create a Synchronous event.
int sEventId = eventManager.create(60); 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); 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(aEventId);
eventManager.status(sEventId); eventManager.status(sEventId);
eventManager.cancel(aEventId); eventManager.cancel(aEventId);
System.out.println("Async Event [" + aEventId + "] has been stopped."); LOGGER.info("Async Event [{}] has been stopped.", aEventId);
eventManager.cancel(sEventId); eventManager.cancel(sEventId);
System.out.println("Sync Event [" + sEventId + "] has been stopped."); LOGGER.info("Sync Event [{}] has been stopped.", sEventId);
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException
| InvalidOperationException e) { | InvalidOperationException e) {
System.out.println(e.getMessage()); LOGGER.error(e.getMessage());
} }
} }
@ -139,58 +144,58 @@ public class App {
Scanner s = new Scanner(System.in); Scanner s = new Scanner(System.in);
int option = -1; int option = -1;
while (option != 4) { while (option != 4) {
System.out.println("Hello. Would you like to boil some eggs?"); LOGGER.info("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"); LOGGER.info("(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("Choose [1,2,3,4]: ");
option = s.nextInt(); option = s.nextInt();
if (option == 1) { if (option == 1) {
s.nextLine(); 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(); 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(); int eventTime = s.nextInt();
if (eventType.equalsIgnoreCase("A")) { if (eventType.equalsIgnoreCase("A")) {
try { try {
int eventId = eventManager.createAsync(eventTime); int eventId = eventManager.createAsync(eventTime);
eventManager.start(eventId); eventManager.start(eventId);
System.out.println("Egg [" + eventId + "] is being boiled."); LOGGER.info("Egg [{}] is being boiled.", eventId);
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) { } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
System.out.println(e.getMessage()); LOGGER.error(e.getMessage());
} }
} else if (eventType.equalsIgnoreCase("S")) { } else if (eventType.equalsIgnoreCase("S")) {
try { try {
int eventId = eventManager.create(eventTime); int eventId = eventManager.create(eventTime);
eventManager.start(eventId); eventManager.start(eventId);
System.out.println("Egg [" + eventId + "] is being boiled."); LOGGER.info("Egg [{}] is being boiled.", eventId);
} catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException } catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException
| EventDoesNotExistException e) { | EventDoesNotExistException e) {
System.out.println(e.getMessage()); LOGGER.error(e.getMessage());
} }
} else { } else {
System.out.println("Unknown event type."); LOGGER.info("Unknown event type.");
} }
} else if (option == 2) { } else if (option == 2) {
System.out.print("Which egg?: "); LOGGER.info("Which egg?: ");
int eventId = s.nextInt(); int eventId = s.nextInt();
try { try {
eventManager.cancel(eventId); eventManager.cancel(eventId);
System.out.println("Egg [" + eventId + "] is removed from boiler."); LOGGER.info("Egg [{}] is removed from boiler.", eventId);
} catch (EventDoesNotExistException e) { } catch (EventDoesNotExistException e) {
System.out.println(e.getMessage()); LOGGER.error(e.getMessage());
} }
} else if (option == 3) { } else if (option == 3) {
s.nextLine(); 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(); String eggChoice = s.nextLine();
if (eggChoice.equalsIgnoreCase("O")) { if (eggChoice.equalsIgnoreCase("O")) {
System.out.print("Which egg?: "); LOGGER.info("Which egg?: ");
int eventId = s.nextInt(); int eventId = s.nextInt();
try { try {
eventManager.status(eventId); eventManager.status(eventId);
} catch (EventDoesNotExistException e) { } catch (EventDoesNotExistException e) {
System.out.println(e.getMessage()); LOGGER.error(e.getMessage());
} }
} else if (eggChoice.equalsIgnoreCase("A")) { } else if (eggChoice.equalsIgnoreCase("A")) {
eventManager.statusOfAllEvents(); eventManager.statusOfAllEvents();

View File

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

View File

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