Resolves checkstyle errors for event-* (#1070)

* Reduces checkstyle errors in event-aggregator

* Reduces checkstyle errors in event-asynchronous

* Reduces checkstyle errors in event-driven-architecture

* Reduces checkstyle errors in event-queue

* Reduces checkstyle errors in event-sourcing
This commit is contained in:
Anurag Agarwal
2019-11-10 23:07:10 +05:30
committed by Ilkka Seppälä
parent 7c888e8886
commit 5ae2ce6e2e
38 changed files with 208 additions and 229 deletions

View File

@ -23,38 +23,38 @@
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 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.
*
* 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.
* <p>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 {@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.
* <p>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 {
@ -67,8 +67,7 @@ public class App {
/**
* Program entry point.
*
* @param args
* command line args
* @param args command line args
*/
public static void main(String[] args) {
App app = new App();
@ -78,8 +77,9 @@ public class App {
}
/**
* App can run in interactive mode or not. Interactive mode == Allow user interaction with command line.
* Non-interactive is a quick sequential run through the available {@link EventManager} operations.
* App can run in interactive mode or not. Interactive mode == Allow user interaction with command
* line. Non-interactive is a quick sequential run through the available {@link EventManager}
* operations.
*/
public void setUp() {
Properties prop = new Properties();
@ -118,24 +118,24 @@ public class App {
try {
// Create an Asynchronous event.
int aEventId = eventManager.createAsync(60);
LOGGER.info("Async Event [{}] has been created.", aEventId);
eventManager.start(aEventId);
LOGGER.info("Async Event [{}] has been started.", aEventId);
int asyncEventId = eventManager.createAsync(60);
LOGGER.info("Async Event [{}] has been created.", asyncEventId);
eventManager.start(asyncEventId);
LOGGER.info("Async Event [{}] has been started.", asyncEventId);
// Create a Synchronous event.
int sEventId = eventManager.create(60);
LOGGER.info("Sync Event [{}] has been created.", sEventId);
eventManager.start(sEventId);
LOGGER.info("Sync Event [{}] has been started.", sEventId);
int syncEventId = eventManager.create(60);
LOGGER.info("Sync Event [{}] has been created.", syncEventId);
eventManager.start(syncEventId);
LOGGER.info("Sync Event [{}] has been started.", syncEventId);
eventManager.status(aEventId);
eventManager.status(sEventId);
eventManager.status(asyncEventId);
eventManager.status(syncEventId);
eventManager.cancel(aEventId);
LOGGER.info("Async Event [{}] has been stopped.", aEventId);
eventManager.cancel(sEventId);
LOGGER.info("Sync Event [{}] has been stopped.", sEventId);
eventManager.cancel(asyncEventId);
LOGGER.info("Async Event [{}] has been stopped.", asyncEventId);
eventManager.cancel(syncEventId);
LOGGER.info("Sync Event [{}] has been stopped.", syncEventId);
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException
| InvalidOperationException e) {
@ -211,7 +211,8 @@ public class App {
int eventId = eventManager.createAsync(eventTime);
eventManager.start(eventId);
LOGGER.info("Egg [{}] is being boiled.", eventId);
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
} catch (MaxNumOfEventsAllowedException | LongRunningEventException
| EventDoesNotExistException e) {
LOGGER.error(e.getMessage());
}
} else if (eventType.equalsIgnoreCase("S")) {
@ -219,8 +220,8 @@ public class App {
int eventId = eventManager.create(eventTime);
eventManager.start(eventId);
LOGGER.info("Egg [{}] is being boiled.", eventId);
} catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException
| EventDoesNotExistException e) {
} catch (MaxNumOfEventsAllowedException | InvalidOperationException
| LongRunningEventException | EventDoesNotExistException e) {
LOGGER.error(e.getMessage());
}
} else {

View File

@ -27,9 +27,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* Each Event runs as a separate/individual thread.
*
*/
public class Event implements IEvent, Runnable {
@ -43,9 +41,10 @@ public class Event implements IEvent, Runnable {
private ThreadCompleteListener eventListener;
/**
* Constructor.
*
* @param eventId event ID
* @param eventTime event time
* @param eventId event ID
* @param eventTime event time
* @param isSynchronous is of synchronous type
*/
public Event(final int eventId, final int eventTime, final boolean isSynchronous) {

View File

@ -24,7 +24,7 @@
package com.iluwatar.event.asynchronous;
/**
* Custom Exception Class for Non Existent Event
* Custom Exception Class for Non Existent Event.
*/
public class EventDoesNotExistException extends Exception {

View File

@ -29,29 +29,28 @@ 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.
* Once an event is complete, it then notifies EventManager through a listener. The EventManager then takes the event
* out of the pool.
*
* 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. Once an event is complete,
* it then notifies EventManager through a listener. The EventManager then takes the event out of
* the pool.
*/
public class EventManager implements ThreadCompleteListener {
public static final int MAX_RUNNING_EVENTS = 1000; // Just don't wanna have too many running events. :)
public static final int MAX_RUNNING_EVENTS = 1000;
// Just don't wanna have too many running events. :)
public static final int MIN_ID = 1;
public static final int MAX_ID = MAX_RUNNING_EVENTS;
public static final int MAX_EVENT_TIME = 1800; // in seconds / 30 minutes.
private int currentlyRunningSyncEvent = -1;
private Random rand;
private Map<Integer, Event> eventPool;
private static final String DOES_NOT_EXIST = " does not exist.";
/**
* EventManager constructor.
*
*/
public EventManager() {
rand = new Random(1);
@ -65,14 +64,15 @@ public class EventManager implements ThreadCompleteListener {
* @param eventTime Time an event should run for.
* @return eventId
* @throws MaxNumOfEventsAllowedException When too many events are running at a time.
* @throws InvalidOperationException No new synchronous events can be created when one is already running.
* @throws LongRunningEventException Long running events are not allowed in the app.
* @throws InvalidOperationException No new synchronous events can be created when one is
* already running.
* @throws LongRunningEventException Long running events are not allowed in the app.
*/
public int create(int eventTime)
throws MaxNumOfEventsAllowedException, InvalidOperationException, LongRunningEventException {
if (currentlyRunningSyncEvent != -1) {
throw new InvalidOperationException(
"Event [" + currentlyRunningSyncEvent + "] is still running. Please wait until it finishes and try again.");
throw new InvalidOperationException("Event [" + currentlyRunningSyncEvent + "] is still"
+ " running. Please wait until it finishes and try again.");
}
int eventId = createEvent(eventTime, true);
@ -87,16 +87,18 @@ public class EventManager implements ThreadCompleteListener {
* @param eventTime Time an event should run for.
* @return eventId
* @throws MaxNumOfEventsAllowedException When too many events are running at a time.
* @throws LongRunningEventException Long running events are not allowed in the app.
* @throws LongRunningEventException Long running events are not allowed in the app.
*/
public int createAsync(int eventTime) throws MaxNumOfEventsAllowedException, LongRunningEventException {
public int createAsync(int eventTime) throws MaxNumOfEventsAllowedException,
LongRunningEventException {
return createEvent(eventTime, false);
}
private int createEvent(int eventTime, boolean isSynchronous)
throws MaxNumOfEventsAllowedException, LongRunningEventException {
if (eventPool.size() == MAX_RUNNING_EVENTS) {
throw new MaxNumOfEventsAllowedException("Too many events are running at the moment. Please try again later.");
throw new MaxNumOfEventsAllowedException("Too many events are running at the moment."
+ " Please try again later.");
}
if (eventTime >= MAX_EVENT_TIME) {
@ -185,7 +187,8 @@ public class EventManager implements ThreadCompleteListener {
}
/**
* Returns a pseudo-random number between min and max, inclusive. The difference between min and max can be at most
* Returns a pseudo-random number between min and max, inclusive. The difference between min and
* max can be at most
* <code>Integer.MAX_VALUE - 1</code>.
*/
private int generateId() {

View File

@ -24,8 +24,7 @@
package com.iluwatar.event.asynchronous;
/**
* Events that fulfill the start stop and list out current status behaviour
* follow this interface
* Events that fulfill the start stop and list out current status behaviour follow this interface.
*/
public interface IEvent {

View File

@ -24,7 +24,7 @@
package com.iluwatar.event.asynchronous;
/**
* Type of Exception raised when the Operation being invoked is Invalid
* Type of Exception raised when the Operation being invoked is Invalid.
*/
public class InvalidOperationException extends Exception {

View File

@ -24,7 +24,7 @@
package com.iluwatar.event.asynchronous;
/**
* Type of Exception raised when the Operation being invoked is Long Running
* Type of Exception raised when the Operation being invoked is Long Running.
*/
public class LongRunningEventException extends Exception {

View File

@ -24,7 +24,7 @@
package com.iluwatar.event.asynchronous;
/**
* Type of Exception raised when the max number of allowed events is exceeded
* Type of Exception raised when the max number of allowed events is exceeded.
*/
public class MaxNumOfEventsAllowedException extends Exception {