#502 Introduced logging in new example
This commit is contained in:
parent
0a427710bb
commit
b37190a214
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -16,38 +16,43 @@
|
||||
*/
|
||||
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;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* This application demonstrates the <b>Event-based Asynchronous</b> pattern. Essentially, users (of the pattern) may
|
||||
* choose to run events in an Asynchronous or Synchronous mode. There can be multiple Asynchronous events running at
|
||||
* once but only one Synchronous event can run at a time. Asynchronous events are synonymous to multi-threads. The key
|
||||
* point here is that the threads run in the background and the user is free to carry on with other processes. Once an
|
||||
* event is complete, the appropriate listener/callback method will be called. The listener then proceeds to carry out
|
||||
* further processing depending on the needs of the user.
|
||||
*
|
||||
*
|
||||
* The {@link EventManager} manages the events/threads that the user creates. Currently, the supported event operations
|
||||
* are: <code>start</code>, <code>stop</code>, <code>getStatus</code>. For Synchronous events, the user is unable to
|
||||
* start another (Synchronous) event if one is already running at the time. The running event would have to either be
|
||||
* stopped or completed before a new event can be started.
|
||||
*
|
||||
*
|
||||
* The Event-based Asynchronous Pattern makes available the advantages of multithreaded applications while hiding many
|
||||
* of the complex issues inherent in multithreaded design. Using a class that supports this pattern can allow you to:-
|
||||
* (1) Perform time-consuming tasks, such as downloads and database operations, "in the background," without
|
||||
* interrupting your application. (2) Execute multiple operations simultaneously, receiving notifications when each
|
||||
* completes. (3) Wait for resources to become available without stopping ("hanging") your application. (4) Communicate
|
||||
* with pending asynchronous operations using the familiar events-and-delegates model.
|
||||
*
|
||||
*
|
||||
* @see EventManager
|
||||
* @see Event
|
||||
*
|
||||
*/
|
||||
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();
|
||||
|
@ -16,13 +16,18 @@
|
||||
*/
|
||||
package com.iluwatar.event.asynchronous;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* Each Event runs as a separate/individual thread.
|
||||
*
|
||||
*/
|
||||
public class Event implements IEvent, Runnable {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Event.class);
|
||||
|
||||
private int eventId;
|
||||
private int eventTime;
|
||||
private boolean isSynchronous;
|
||||
@ -31,7 +36,7 @@ public class Event implements IEvent, Runnable {
|
||||
private ThreadCompleteListener eventListener;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param eventId event ID
|
||||
* @param eventTime event time
|
||||
* @param isSynchronous is of synchronous type
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,7 @@ import java.util.Random;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* EventManager handles and maintains a pool of event threads. {@link Event} threads are created upon user request. Thre
|
||||
* are two types of events; Asynchronous and Synchronous. There can be multiple Asynchronous events running at once but
|
||||
* only one Synchronous event running at a time. Currently supported event operations are: start, stop, and getStatus.
|
||||
@ -52,7 +52,7 @@ public class EventManager implements ThreadCompleteListener {
|
||||
|
||||
/**
|
||||
* Create a Synchronous event.
|
||||
*
|
||||
*
|
||||
* @param eventTime Time an event should run for.
|
||||
* @return eventId
|
||||
* @throws MaxNumOfEventsAllowedException When too many events are running at a time.
|
||||
@ -74,7 +74,7 @@ public class EventManager implements ThreadCompleteListener {
|
||||
|
||||
/**
|
||||
* Create an Asynchronous event.
|
||||
*
|
||||
*
|
||||
* @param eventTime Time an event should run for.
|
||||
* @return eventId
|
||||
* @throws MaxNumOfEventsAllowedException When too many events are running at a time.
|
||||
@ -106,7 +106,7 @@ public class EventManager implements ThreadCompleteListener {
|
||||
|
||||
/**
|
||||
* Starts event.
|
||||
*
|
||||
*
|
||||
* @param eventId The event that needs to be started.
|
||||
* @throws EventDoesNotExistException If event does not exist in our eventPool.
|
||||
*/
|
||||
@ -120,7 +120,7 @@ public class EventManager implements ThreadCompleteListener {
|
||||
|
||||
/**
|
||||
* Stops event.
|
||||
*
|
||||
*
|
||||
* @param eventId The event that needs to be stopped.
|
||||
* @throws EventDoesNotExistException If event does not exist in our eventPool.
|
||||
*/
|
||||
@ -139,7 +139,7 @@ public class EventManager implements ThreadCompleteListener {
|
||||
|
||||
/**
|
||||
* Get status of a running event.
|
||||
*
|
||||
*
|
||||
* @param eventId The event to inquire status of.
|
||||
* @throws EventDoesNotExistException If event does not exist in our eventPool.
|
||||
*/
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user