Resolves checkstyle errors for collection-pipeline, command, commander (#1061)
* Reduces checkstyle errors in collection-pipeline * Reduces checkstyle errors in command * Reduces checkstyle errors in commander
This commit is contained in:
parent
31f27a720b
commit
2f49648047
@ -23,21 +23,18 @@
|
||||
|
||||
package com.iluwatar.collectionpipeline;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* In imperative-style programming, it is common to use for and while loops for
|
||||
* most kinds of data processing. Function composition is a simple technique
|
||||
* that lets you sequence modular functions to create more complex operations.
|
||||
* When you run data through the sequence, you have a collection pipeline.
|
||||
* Together, the Function Composition and Collection Pipeline patterns enable
|
||||
* you to create sophisticated programs where data flow from upstream to
|
||||
* downstream and is passed through a series of transformations.
|
||||
*
|
||||
* In imperative-style programming, it is common to use for and while loops for most kinds of data
|
||||
* processing. Function composition is a simple technique that lets you sequence modular functions
|
||||
* to create more complex operations. When you run data through the sequence, you have a collection
|
||||
* pipeline. Together, the Function Composition and Collection Pipeline patterns enable you to
|
||||
* create sophisticated programs where data flow from upstream to downstream and is passed through a
|
||||
* series of transformations.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
@ -46,8 +43,7 @@ public class App {
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args
|
||||
* command line args
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
@ -59,18 +55,22 @@ public class App {
|
||||
List<String> modelsFunctional = FunctionalProgramming.getModelsAfter2000(cars);
|
||||
LOGGER.info(modelsFunctional.toString());
|
||||
|
||||
Map<Category, List<Car>> groupingByCategoryImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
|
||||
Map<Category, List<Car>> groupingByCategoryImperative =
|
||||
ImperativeProgramming.getGroupingOfCarsByCategory(cars);
|
||||
LOGGER.info(groupingByCategoryImperative.toString());
|
||||
|
||||
Map<Category, List<Car>> groupingByCategoryFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
|
||||
Map<Category, List<Car>> groupingByCategoryFunctional =
|
||||
FunctionalProgramming.getGroupingOfCarsByCategory(cars);
|
||||
LOGGER.info(groupingByCategoryFunctional.toString());
|
||||
|
||||
Person john = new Person(cars);
|
||||
|
||||
List<Car> sedansOwnedImperative = ImperativeProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
|
||||
List<Car> sedansOwnedImperative =
|
||||
ImperativeProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
|
||||
LOGGER.info(sedansOwnedImperative.toString());
|
||||
|
||||
List<Car> sedansOwnedFunctional = FunctionalProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
|
||||
List<Car> sedansOwnedFunctional =
|
||||
FunctionalProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
|
||||
LOGGER.info(sedansOwnedFunctional.toString());
|
||||
}
|
||||
}
|
||||
|
@ -34,6 +34,7 @@ public class Car {
|
||||
|
||||
/**
|
||||
* Constructor to create an instance of car.
|
||||
*
|
||||
* @param make the make of the car
|
||||
* @param model the model of the car
|
||||
* @param yearOfMake the year of built of the car
|
||||
|
@ -34,6 +34,7 @@ public class CarFactory {
|
||||
|
||||
/**
|
||||
* Factory method to create a {@link List} of {@link Car} instances.
|
||||
*
|
||||
* @return {@link List} of {@link Car}
|
||||
*/
|
||||
public static List<Car> createCars() {
|
||||
|
@ -24,7 +24,7 @@
|
||||
package com.iluwatar.collectionpipeline;
|
||||
|
||||
/**
|
||||
* Enum for the category of car
|
||||
* Enum for the category of car.
|
||||
*/
|
||||
public enum Category {
|
||||
JEEP, SEDAN, CONVERTIBLE
|
||||
|
@ -32,18 +32,15 @@ import java.util.stream.Collectors;
|
||||
* Iterating and sorting with a collection pipeline
|
||||
*
|
||||
* <p>In functional programming, it's common to sequence complex operations through
|
||||
* a series of smaller modular functions or operations. The series is called a
|
||||
* composition of functions, or a function composition. When a collection of
|
||||
* data flows through a function composition, it becomes a collection pipeline.
|
||||
* Function Composition and Collection Pipeline are two design patterns
|
||||
* frequently used in functional-style programming.
|
||||
* a series of smaller modular functions or operations. The series is called a composition of
|
||||
* functions, or a function composition. When a collection of data flows through a function
|
||||
* composition, it becomes a collection pipeline. Function Composition and Collection Pipeline are
|
||||
* two design patterns frequently used in functional-style programming.
|
||||
*
|
||||
* <p>Instead of passing a lambda expression to the map method, we passed the
|
||||
* method reference Car::getModel. Likewise, instead of passing the lambda
|
||||
* expression car -> car.getYear() to the comparing method, we passed the method
|
||||
* reference Car::getYear. Method references are short, concise, and expressive.
|
||||
* It is best to use them wherever possible.
|
||||
*
|
||||
* method reference Car::getModel. Likewise, instead of passing the lambda expression car ->
|
||||
* car.getYear() to the comparing method, we passed the method reference Car::getYear. Method
|
||||
* references are short, concise, and expressive. It is best to use them wherever possible.
|
||||
*/
|
||||
public class FunctionalProgramming {
|
||||
private FunctionalProgramming() {
|
||||
@ -62,7 +59,7 @@ public class FunctionalProgramming {
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to group cars by category using groupingBy
|
||||
* Method to group cars by category using groupingBy.
|
||||
*
|
||||
* @param cars {@link List} of {@link Car} to be used for grouping
|
||||
* @return {@link Map} with category as key and cars belonging to that category as value
|
||||
@ -72,7 +69,7 @@ public class FunctionalProgramming {
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get all Sedan cars belonging to a group of persons sorted by year of manufacture
|
||||
* Method to get all Sedan cars belonging to a group of persons sorted by year of manufacture.
|
||||
*
|
||||
* @param persons {@link List} of {@link Person} to be used
|
||||
* @return {@link List} of {@link Car} to belonging to the group
|
||||
|
@ -31,23 +31,20 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Imperative-style programming to iterate over the list and get the names of
|
||||
* cars made later than the year 2000. We then sort the models in ascending
|
||||
* order by year.
|
||||
* Imperative-style programming to iterate over the list and get the names of cars made later than
|
||||
* the year 2000. We then sort the models in ascending order by year.
|
||||
*
|
||||
* <p>As you can see, there's a lot of looping in this code. First, the
|
||||
* getModelsAfter2000UsingFor method takes a list of cars as its parameter. It
|
||||
* extracts or filters out cars made after the year 2000, putting them into a
|
||||
* new list named carsSortedByYear. Next, it sorts that list in ascending order
|
||||
* by year-of-make. Finally, it loops through the list carsSortedByYear to get
|
||||
* the model names and returns them in a list.
|
||||
* getModelsAfter2000UsingFor method takes a list of cars as its parameter. It extracts or filters
|
||||
* out cars made after the year 2000, putting them into a new list named carsSortedByYear. Next, it
|
||||
* sorts that list in ascending order by year-of-make. Finally, it loops through the list
|
||||
* carsSortedByYear to get the model names and returns them in a list.
|
||||
*
|
||||
* <p>This short example demonstrates what I call the effect of statements. While
|
||||
* functions and methods in general can be used as expressions, the {@link Collections}
|
||||
* sort method doesn't return a result. Because it is used as a statement, it
|
||||
* mutates the list given as argument. Both of the for loops also mutate lists
|
||||
* as they iterate. Being statements, that's just how these elements work. As a
|
||||
* result, the code contains unnecessary garbage variables
|
||||
* functions and methods in general can be used as expressions, the {@link Collections} sort method
|
||||
* doesn't return a result. Because it is used as a statement, it mutates the list given as
|
||||
* argument. Both of the for loops also mutate lists as they iterate. Being statements, that's just
|
||||
* how these elements work. As a result, the code contains unnecessary garbage variables
|
||||
*/
|
||||
public class ImperativeProgramming {
|
||||
private ImperativeProgramming() {
|
||||
@ -55,6 +52,7 @@ public class ImperativeProgramming {
|
||||
|
||||
/**
|
||||
* Method to return the car models built after year 2000 using for loops.
|
||||
*
|
||||
* @param cars {@link List} of {@link Car} to iterate over
|
||||
* @return {@link List} of {@link String} of car models built after year 2000
|
||||
*/
|
||||
@ -82,14 +80,14 @@ public class ImperativeProgramming {
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to group cars by category using for loops
|
||||
* Method to group cars by category using for loops.
|
||||
*
|
||||
* @param cars {@link List} of {@link Car} to be used for grouping
|
||||
* @return {@link Map} with category as key and cars belonging to that category as value
|
||||
*/
|
||||
public static Map<Category, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
|
||||
Map<Category, List<Car>> groupingByCategory = new HashMap<>();
|
||||
for (Car car: cars) {
|
||||
for (Car car : cars) {
|
||||
if (groupingByCategory.containsKey(car.getCategory())) {
|
||||
groupingByCategory.get(car.getCategory()).add(car);
|
||||
} else {
|
||||
@ -102,19 +100,20 @@ public class ImperativeProgramming {
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get all Sedan cars belonging to a group of persons sorted by year of manufacture using for loops
|
||||
* Method to get all Sedan cars belonging to a group of persons sorted by year of manufacture
|
||||
* using for loops.
|
||||
*
|
||||
* @param persons {@link List} of {@link Person} to be used
|
||||
* @return {@link List} of {@link Car} to belonging to the group
|
||||
*/
|
||||
public static List<Car> getSedanCarsOwnedSortedByDate(List<Person> persons) {
|
||||
List<Car> cars = new ArrayList<>();
|
||||
for (Person person: persons) {
|
||||
for (Person person : persons) {
|
||||
cars.addAll(person.getCars());
|
||||
}
|
||||
|
||||
List<Car> sedanCars = new ArrayList<>();
|
||||
for (Car car: cars) {
|
||||
for (Car car : cars) {
|
||||
if (Category.SEDAN.equals(car.getCategory())) {
|
||||
sedanCars.add(car);
|
||||
}
|
||||
|
@ -33,6 +33,7 @@ public class Person {
|
||||
|
||||
/**
|
||||
* Constructor to create an instance of person.
|
||||
*
|
||||
* @param cars the list of cars owned
|
||||
*/
|
||||
public Person(List<Car> cars) {
|
||||
|
@ -23,14 +23,13 @@
|
||||
|
||||
package com.iluwatar.collectionpipeline;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Tests that Collection Pipeline methods work as expected.
|
||||
|
@ -24,30 +24,27 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Command pattern is a behavioral design pattern in which an object is used to encapsulate all
|
||||
* information needed to perform an action or trigger an event at a later time. This information
|
||||
* includes the method name, the object that owns the method and values for the method parameters.
|
||||
* <p>
|
||||
* Four terms always associated with the command pattern are command, receiver, invoker and client.
|
||||
* A command object (spell) knows about the receiver (target) and invokes a method of the receiver.
|
||||
* Values for parameters of the receiver method are stored in the command. The receiver then does
|
||||
* the work. An invoker object (wizard) knows how to execute a command, and optionally does
|
||||
* bookkeeping about the command execution. The invoker does not know anything about a concrete
|
||||
*
|
||||
* <p>Four terms always associated with the command pattern are command, receiver, invoker and
|
||||
* client. A command object (spell) knows about the receiver (target) and invokes a method of the
|
||||
* receiver. Values for parameters of the receiver method are stored in the command. The receiver
|
||||
* then does the work. An invoker object (wizard) knows how to execute a command, and optionally
|
||||
* does bookkeeping about the command execution. The invoker does not know anything about a concrete
|
||||
* command, it knows only about command interface. Both an invoker object and several command
|
||||
* objects are held by a client object (app). The client decides which commands to execute at which
|
||||
* points. To execute a command, it passes the command object to the invoker object.
|
||||
* <p>
|
||||
* In other words, in this example the wizard casts spells on the goblin. The wizard keeps track of
|
||||
* the previous spells cast, so it is easy to undo them. In addition, the wizard keeps track of the
|
||||
* spells undone, so they can be redone.
|
||||
*
|
||||
*
|
||||
* <p>In other words, in this example the wizard casts spells on the goblin. The wizard keeps track
|
||||
* of the previous spells cast, so it is easy to undo them. In addition, the wizard keeps track of
|
||||
* the spells undone, so they can be redone.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
/**
|
||||
*
|
||||
* Interface for Commands.
|
||||
*
|
||||
*/
|
||||
public abstract class Command {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
/**
|
||||
*
|
||||
* Goblin is the target of the spells
|
||||
*
|
||||
* Goblin is the target of the spells.
|
||||
*/
|
||||
public class Goblin extends Target {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
/**
|
||||
*
|
||||
* InvisibilitySpell is a concrete command
|
||||
*
|
||||
* InvisibilitySpell is a concrete command.
|
||||
*/
|
||||
public class InvisibilitySpell extends Command {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
/**
|
||||
*
|
||||
* ShrinkSpell is a concrete command
|
||||
*
|
||||
* ShrinkSpell is a concrete command.
|
||||
*/
|
||||
public class ShrinkSpell extends Command {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
/**
|
||||
*
|
||||
* Enumeration for target size.
|
||||
*
|
||||
*/
|
||||
public enum Size {
|
||||
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Base class for spell targets.
|
||||
*
|
||||
*/
|
||||
public abstract class Target {
|
||||
|
||||
@ -59,7 +57,7 @@ public abstract class Target {
|
||||
public abstract String toString();
|
||||
|
||||
/**
|
||||
* Print status
|
||||
* Print status.
|
||||
*/
|
||||
public void printStatus() {
|
||||
LOGGER.info("{}, [size={}] [visibility={}]", this, getSize(), getVisibility());
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
/**
|
||||
*
|
||||
* Enumeration for target visibility.
|
||||
*
|
||||
*/
|
||||
public enum Visibility {
|
||||
|
||||
|
@ -23,16 +23,13 @@
|
||||
|
||||
package com.iluwatar.command;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
*
|
||||
* Wizard is the invoker of the commands
|
||||
*
|
||||
* Wizard is the invoker of the commands.
|
||||
*/
|
||||
public class Wizard {
|
||||
|
||||
@ -46,7 +43,7 @@ public class Wizard {
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast spell
|
||||
* Cast spell.
|
||||
*/
|
||||
public void castSpell(Command command, Target target) {
|
||||
LOGGER.info("{} casts {} at {}", this, command, target);
|
||||
@ -55,7 +52,7 @@ public class Wizard {
|
||||
}
|
||||
|
||||
/**
|
||||
* Undo last spell
|
||||
* Undo last spell.
|
||||
*/
|
||||
public void undoLastSpell() {
|
||||
if (!undoStack.isEmpty()) {
|
||||
@ -67,7 +64,7 @@ public class Wizard {
|
||||
}
|
||||
|
||||
/**
|
||||
* Redo last spell
|
||||
* Redo last spell.
|
||||
*/
|
||||
public void redoLastSpell() {
|
||||
if (!redoStack.isEmpty()) {
|
||||
|
@ -31,15 +31,14 @@ import com.iluwatar.commander.messagingservice.MessagingDatabase;
|
||||
import com.iluwatar.commander.messagingservice.MessagingService;
|
||||
import com.iluwatar.commander.paymentservice.PaymentDatabase;
|
||||
import com.iluwatar.commander.paymentservice.PaymentService;
|
||||
import com.iluwatar.commander.queue.QueueDatabase;
|
||||
import com.iluwatar.commander.shippingservice.ShippingDatabase;
|
||||
import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
import com.iluwatar.commander.queue.QueueDatabase;
|
||||
|
||||
/**
|
||||
* AppEmployeeDbFailCases class looks at possible cases when Employee handle service is
|
||||
* available/unavailable.
|
||||
*/
|
||||
|
||||
public class AppEmployeeDbFailCases {
|
||||
final int numOfRetries = 3;
|
||||
final long retryDuration = 30000;
|
||||
@ -50,19 +49,24 @@ public class AppEmployeeDbFailCases {
|
||||
final long employeeTime = 240000; //4 mins
|
||||
|
||||
void employeeDatabaseUnavailableCase() throws Exception {
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
PaymentService ps =
|
||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
QueueDatabase qdb = new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
EmployeeHandle eh =
|
||||
new EmployeeHandle(new EmployeeDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,
|
||||
queueTime,queueTaskTime,paymentTime,messageTime,employeeTime);
|
||||
QueueDatabase qdb =
|
||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
@ -70,13 +74,15 @@ public class AppEmployeeDbFailCases {
|
||||
|
||||
void employeeDbSuccessCase() throws Exception {
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
||||
ShippingService ss =
|
||||
new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase(), new DatabaseUnavailableException(),
|
||||
EmployeeHandle eh =
|
||||
new EmployeeHandle(new EmployeeDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
QueueDatabase qdb = new QueueDatabase();
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,
|
||||
queueTime,queueTaskTime,paymentTime,messageTime,employeeTime);
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
|
@ -30,9 +30,9 @@ import com.iluwatar.commander.messagingservice.MessagingDatabase;
|
||||
import com.iluwatar.commander.messagingservice.MessagingService;
|
||||
import com.iluwatar.commander.paymentservice.PaymentDatabase;
|
||||
import com.iluwatar.commander.paymentservice.PaymentService;
|
||||
import com.iluwatar.commander.queue.QueueDatabase;
|
||||
import com.iluwatar.commander.shippingservice.ShippingDatabase;
|
||||
import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
import com.iluwatar.commander.queue.QueueDatabase;
|
||||
|
||||
/**
|
||||
* AppMessagingFailCases class looks at possible cases when Messaging service is
|
||||
@ -52,13 +52,15 @@ public class AppMessagingFailCases {
|
||||
//rest is successful
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
MessagingService ms =
|
||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase();
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,
|
||||
queueTime,queueTaskTime,paymentTime,messageTime,employeeTime);
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
@ -66,20 +68,26 @@ public class AppMessagingFailCases {
|
||||
|
||||
void messagingDatabaseUnavailableCasePaymentError() throws Exception {
|
||||
//rest is successful
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
PaymentService ps =
|
||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
MessagingService ms =
|
||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase();
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,
|
||||
queueTime,queueTaskTime,paymentTime,messageTime,employeeTime);
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
@ -87,19 +95,25 @@ public class AppMessagingFailCases {
|
||||
|
||||
void messagingDatabaseUnavailableCasePaymentFailure() throws Exception {
|
||||
//rest is successful
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
PaymentService ps =
|
||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,queueTime,queueTaskTime,
|
||||
paymentTime,messageTime,employeeTime);
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
||||
MessagingService ms =
|
||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb =
|
||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
Commander c =
|
||||
new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration, queueTime, queueTaskTime,
|
||||
paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
@ -107,16 +121,19 @@ public class AppMessagingFailCases {
|
||||
|
||||
void messagingSuccessCase() throws Exception {
|
||||
//done here
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
PaymentService ps =
|
||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
MessagingService ms =
|
||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase();
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,
|
||||
queueTime,queueTaskTime,paymentTime,messageTime,employeeTime);
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
|
@ -31,13 +31,12 @@ import com.iluwatar.commander.messagingservice.MessagingDatabase;
|
||||
import com.iluwatar.commander.messagingservice.MessagingService;
|
||||
import com.iluwatar.commander.paymentservice.PaymentDatabase;
|
||||
import com.iluwatar.commander.paymentservice.PaymentService;
|
||||
import com.iluwatar.commander.queue.QueueDatabase;
|
||||
import com.iluwatar.commander.shippingservice.ShippingDatabase;
|
||||
import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
import com.iluwatar.commander.queue.QueueDatabase;
|
||||
|
||||
/**
|
||||
* AppPaymentFailCases class looks at possible cases when Payment service is
|
||||
* available/unavailable.
|
||||
* AppPaymentFailCases class looks at possible cases when Payment service is available/unavailable.
|
||||
*/
|
||||
|
||||
public class AppPaymentFailCases {
|
||||
@ -50,14 +49,16 @@ public class AppPaymentFailCases {
|
||||
final long employeeTime = 240000; //4 mins
|
||||
|
||||
void paymentNotPossibleCase() throws Exception {
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
PaymentService ps =
|
||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new PaymentDetailsErrorException());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException());
|
||||
MessagingService ms =
|
||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase(new DatabaseUnavailableException());
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,
|
||||
queueTime,queueTaskTime,paymentTime,messageTime,employeeTime);
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
@ -65,15 +66,17 @@ public class AppPaymentFailCases {
|
||||
|
||||
void paymentDatabaseUnavailableCase() throws Exception {
|
||||
//rest is successful
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
PaymentService ps =
|
||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase();
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,
|
||||
queueTime,queueTaskTime,paymentTime,messageTime,employeeTime);
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
@ -81,14 +84,16 @@ public class AppPaymentFailCases {
|
||||
|
||||
void paymentSuccessCase() throws Exception {
|
||||
//goes to message after 2 retries maybe - rest is successful for now
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
PaymentService ps =
|
||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException());
|
||||
MessagingService ms =
|
||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase(new DatabaseUnavailableException());
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,
|
||||
queueTime,queueTaskTime,paymentTime,messageTime,employeeTime);
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
|
@ -31,13 +31,12 @@ import com.iluwatar.commander.messagingservice.MessagingDatabase;
|
||||
import com.iluwatar.commander.messagingservice.MessagingService;
|
||||
import com.iluwatar.commander.paymentservice.PaymentDatabase;
|
||||
import com.iluwatar.commander.paymentservice.PaymentService;
|
||||
import com.iluwatar.commander.queue.QueueDatabase;
|
||||
import com.iluwatar.commander.shippingservice.ShippingDatabase;
|
||||
import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
import com.iluwatar.commander.queue.QueueDatabase;
|
||||
|
||||
/**
|
||||
* AppQueueFailCases class looks at possible cases when Queue Database is
|
||||
* available/unavailable.
|
||||
* AppQueueFailCases class looks at possible cases when Queue Database is available/unavailable.
|
||||
*/
|
||||
|
||||
public class AppQueueFailCases {
|
||||
@ -50,17 +49,20 @@ public class AppQueueFailCases {
|
||||
final long employeeTime = 240000; //4 mins
|
||||
|
||||
void queuePaymentTaskDatabaseUnavailableCase() throws Exception {
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
PaymentService ps =
|
||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,
|
||||
queueTime,queueTaskTime,paymentTime,messageTime,employeeTime);
|
||||
QueueDatabase qdb =
|
||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
@ -69,15 +71,18 @@ public class AppQueueFailCases {
|
||||
void queueMessageTaskDatabaseUnavailableCase() throws Exception {
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
MessagingService ms =
|
||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,
|
||||
queueTime,queueTaskTime,paymentTime,messageTime,employeeTime);
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb =
|
||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
@ -85,36 +90,46 @@ public class AppQueueFailCases {
|
||||
|
||||
void queueEmployeeDbTaskDatabaseUnavailableCase() throws Exception {
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
||||
ShippingService ss =
|
||||
new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
QueueDatabase qdb = new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
EmployeeHandle eh =
|
||||
new EmployeeHandle(new EmployeeDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,
|
||||
queueTime,queueTaskTime,paymentTime,messageTime,employeeTime);
|
||||
QueueDatabase qdb =
|
||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void queueSuccessCase() throws Exception {
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
PaymentService ps =
|
||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
MessagingService ms =
|
||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,
|
||||
queueTime,queueTaskTime,paymentTime,messageTime,employeeTime);
|
||||
QueueDatabase qdb =
|
||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
|
@ -32,9 +32,9 @@ import com.iluwatar.commander.messagingservice.MessagingDatabase;
|
||||
import com.iluwatar.commander.messagingservice.MessagingService;
|
||||
import com.iluwatar.commander.paymentservice.PaymentDatabase;
|
||||
import com.iluwatar.commander.paymentservice.PaymentService;
|
||||
import com.iluwatar.commander.queue.QueueDatabase;
|
||||
import com.iluwatar.commander.shippingservice.ShippingDatabase;
|
||||
import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
import com.iluwatar.commander.queue.QueueDatabase;
|
||||
|
||||
/**
|
||||
* AppShippingFailCases class looks at possible cases when Shipping service is
|
||||
@ -52,12 +52,13 @@ public class AppShippingFailCases {
|
||||
|
||||
void itemUnavailableCase() throws Exception {
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
||||
ShippingService ss =
|
||||
new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase();
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,
|
||||
queueTime,queueTaskTime,paymentTime,messageTime,employeeTime);
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
@ -65,12 +66,13 @@ public class AppShippingFailCases {
|
||||
|
||||
void shippingNotPossibleCase() throws Exception {
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase(), new ShippingNotPossibleException());
|
||||
ShippingService ss =
|
||||
new ShippingService(new ShippingDatabase(), new ShippingNotPossibleException());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase();
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,
|
||||
queueTime,queueTaskTime,paymentTime,messageTime,employeeTime);
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
@ -79,14 +81,16 @@ public class AppShippingFailCases {
|
||||
void shippingDatabaseUnavailableCase() throws Exception {
|
||||
//rest is successful
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
ShippingService ss =
|
||||
new ShippingService(new ShippingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase();
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,
|
||||
queueTime,queueTaskTime,paymentTime,messageTime,employeeTime);
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
@ -94,14 +98,17 @@ public class AppShippingFailCases {
|
||||
|
||||
void shippingSuccessCase() throws Exception {
|
||||
//goes to payment after 2 retries maybe - rest is successful for now
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase(), new DatabaseUnavailableException(),
|
||||
PaymentService ps =
|
||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException());
|
||||
ShippingService ss =
|
||||
new ShippingService(new ShippingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException());
|
||||
MessagingService ms =
|
||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase();
|
||||
Commander c = new Commander(eh,ps,ss,ms,qdb,numOfRetries,retryDuration,
|
||||
queueTime,queueTaskTime,paymentTime,messageTime,employeeTime);
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
|
@ -23,53 +23,51 @@
|
||||
|
||||
package com.iluwatar.commander;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import com.iluwatar.commander.Order.MessageSent;
|
||||
import com.iluwatar.commander.Order.PaymentStatus;
|
||||
import com.iluwatar.commander.employeehandle.EmployeeHandle;
|
||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||
import com.iluwatar.commander.exceptions.ItemUnavailableException;
|
||||
import com.iluwatar.commander.exceptions.PaymentDetailsErrorException;
|
||||
import com.iluwatar.commander.exceptions.ShippingNotPossibleException;
|
||||
import com.iluwatar.commander.messagingservice.MessagingService;
|
||||
import com.iluwatar.commander.Order.MessageSent;
|
||||
import com.iluwatar.commander.Order.PaymentStatus;
|
||||
import com.iluwatar.commander.paymentservice.PaymentService;
|
||||
import com.iluwatar.commander.queue.QueueDatabase;
|
||||
import com.iluwatar.commander.queue.QueueTask;
|
||||
import com.iluwatar.commander.queue.QueueTask.TaskType;
|
||||
import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
import java.util.ArrayList;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*<p>Commander pattern is used to handle all issues that can come up while making a
|
||||
* distributed transaction. The idea is to have a commander, which coordinates the
|
||||
* execution of all instructions and ensures proper completion using retries and
|
||||
* taking care of idempotence. By queueing instructions while they haven't been done,
|
||||
* we can ensure a state of 'eventual consistency'.</p>
|
||||
* <p>Commander pattern is used to handle all issues that can come up while making a
|
||||
* distributed transaction. The idea is to have a commander, which coordinates the execution of all
|
||||
* instructions and ensures proper completion using retries and taking care of idempotence. By
|
||||
* queueing instructions while they haven't been done, we can ensure a state of 'eventual
|
||||
* consistency'.</p>
|
||||
* <p>In our example, we have an e-commerce application. When the user places an order,
|
||||
* the shipping service is intimated first. If the service does not respond for some
|
||||
* reason, the order is not placed. If response is received, the commander then calls
|
||||
* for the payment service to be intimated. If this fails, the shipping still takes
|
||||
* place (order converted to COD) and the item is queued. If the queue is also found
|
||||
* to be unavailable, the payment is noted to be not done and this is added to an
|
||||
* employee database. Three types of messages are sent to the user - one, if payment
|
||||
* succeeds; two, if payment fails definitively; and three, if payment fails in the
|
||||
* first attempt. If the message is not sent, this is also queued and is added to employee
|
||||
* db. We also have a time limit for each instruction to be completed, after which, the
|
||||
* instruction is not executed, thereby ensuring that resources are not held for too long.
|
||||
* In the rare occasion in which everything fails, an individual would have to step in to
|
||||
* figure out how to solve the issue.</p>
|
||||
* the shipping service is intimated first. If the service does not respond for some reason, the
|
||||
* order is not placed. If response is received, the commander then calls for the payment service to
|
||||
* be intimated. If this fails, the shipping still takes place (order converted to COD) and the item
|
||||
* is queued. If the queue is also found to be unavailable, the payment is noted to be not done and
|
||||
* this is added to an employee database. Three types of messages are sent to the user - one, if
|
||||
* payment succeeds; two, if payment fails definitively; and three, if payment fails in the first
|
||||
* attempt. If the message is not sent, this is also queued and is added to employee db. We also
|
||||
* have a time limit for each instruction to be completed, after which, the instruction is not
|
||||
* executed, thereby ensuring that resources are not held for too long. In the rare occasion in
|
||||
* which everything fails, an individual would have to step in to figure out how to solve the
|
||||
* issue.</p>
|
||||
* <p>We have abstract classes {@link Database} and {@link Service} which are extended
|
||||
* by all the databases and services. Each service has a database to be updated, and
|
||||
* receives request from an outside user (the {@link Commander} class here). There are
|
||||
* 5 microservices - {@link ShippingService}, {@link PaymentService}, {@link MessagingService},
|
||||
* {@link EmployeeHandle} and a {@link QueueDatabase}. We use retries to execute any
|
||||
* instruction using {@link Retry} class, and idempotence is ensured by going through some
|
||||
* checks before making requests to services and making change in {@link Order} class fields
|
||||
* if request succeeds or definitively fails. There are 5 classes -
|
||||
* {@link AppShippingFailCases}, {@link AppPaymentFailCases}, {@link AppMessagingFailCases},
|
||||
* {@link AppQueueFailCases} and {@link AppEmployeeDbFailCases}, which look at the different
|
||||
* scenarios that may be encountered during the placing of an order.</p>
|
||||
* by all the databases and services. Each service has a database to be updated, and receives
|
||||
* request from an outside user (the {@link Commander} class here). There are 5 microservices -
|
||||
* {@link ShippingService}, {@link PaymentService}, {@link MessagingService}, {@link EmployeeHandle}
|
||||
* and a {@link QueueDatabase}. We use retries to execute any instruction using {@link Retry} class,
|
||||
* and idempotence is ensured by going through some checks before making requests to services and
|
||||
* making change in {@link Order} class fields if request succeeds or definitively fails. There are
|
||||
* 5 classes - {@link AppShippingFailCases}, {@link AppPaymentFailCases}, {@link
|
||||
* AppMessagingFailCases}, {@link AppQueueFailCases} and {@link AppEmployeeDbFailCases}, which look
|
||||
* at the different scenarios that may be encountered during the placing of an order.</p>
|
||||
*/
|
||||
|
||||
public class Commander {
|
||||
@ -91,12 +89,13 @@ public class Commander {
|
||||
static final Logger LOG = LoggerFactory.getLogger(Commander.class);
|
||||
//we could also have another db where it stores all orders
|
||||
|
||||
Commander(EmployeeHandle empDb, PaymentService pService, ShippingService sService,
|
||||
MessagingService mService, QueueDatabase qdb, int numOfRetries, long retryDuration,
|
||||
long queueTime, long queueTaskTime, long paymentTime, long messageTime, long employeeTime) {
|
||||
this.paymentService = pService;
|
||||
this.shippingService = sService;
|
||||
this.messagingService = mService;
|
||||
Commander(EmployeeHandle empDb, PaymentService paymentService, ShippingService shippingService,
|
||||
MessagingService messagingService, QueueDatabase qdb, int numOfRetries,
|
||||
long retryDuration, long queueTime, long queueTaskTime, long paymentTime,
|
||||
long messageTime, long employeeTime) {
|
||||
this.paymentService = paymentService;
|
||||
this.shippingService = shippingService;
|
||||
this.messagingService = messagingService;
|
||||
this.employeeDb = empDb;
|
||||
this.queue = qdb;
|
||||
this.numOfRetries = numOfRetries;
|
||||
@ -118,7 +117,8 @@ public class Commander {
|
||||
Retry.Operation op = (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
LOG.debug("Order " + order.id + ": Error in connecting to shipping service, trying again..");
|
||||
LOG.debug("Order " + order.id + ": Error in connecting to shipping service, "
|
||||
+ "trying again..");
|
||||
} else {
|
||||
LOG.debug("Order " + order.id + ": Error in creating shipping request..");
|
||||
}
|
||||
@ -126,25 +126,27 @@ public class Commander {
|
||||
}
|
||||
String transactionId = shippingService.receiveRequest(order.item, order.user.address);
|
||||
//could save this transaction id in a db too
|
||||
LOG.info("Order " + order.id + ": Shipping placed successfully, transaction id: " + transactionId);
|
||||
LOG.info("Order " + order.id + ": Shipping placed successfully, transaction id: "
|
||||
+ transactionId);
|
||||
LOG.info("Order has been placed and will be shipped to you. Please wait while we make your"
|
||||
+ " payment... ");
|
||||
sendPaymentRequest(order);
|
||||
return;
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o,err) -> {
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
if (ShippingNotPossibleException.class.isAssignableFrom(err.getClass())) {
|
||||
LOG.info("Shipping is currently not possible to your address. We are working on the problem "
|
||||
+ "and will get back to you asap.");
|
||||
LOG.info("Shipping is currently not possible to your address. We are working on the problem"
|
||||
+ " and will get back to you asap.");
|
||||
finalSiteMsgShown = true;
|
||||
LOG.info("Order " + order.id + ": Shipping not possible to address, trying to add problem to employee db..");
|
||||
LOG.info("Order " + order.id + ": Shipping not possible to address, trying to add problem "
|
||||
+ "to employee db..");
|
||||
employeeHandleIssue(o);
|
||||
} else if (ItemUnavailableException.class.isAssignableFrom(err.getClass())) {
|
||||
LOG.info("This item is currently unavailable. We will inform you as soon as the item becomes "
|
||||
+ "available again.");
|
||||
LOG.info("This item is currently unavailable. We will inform you as soon as the item "
|
||||
+ "becomes available again.");
|
||||
finalSiteMsgShown = true;
|
||||
LOG.info("Order " + order.id + ": Item " + order.item + " unavailable, trying to add problem to employee "
|
||||
+ "handle..");
|
||||
LOG.info("Order " + order.id + ": Item " + order.item + " unavailable, trying to add "
|
||||
+ "problem to employee handle..");
|
||||
employeeHandleIssue(o);
|
||||
} else {
|
||||
LOG.info("Sorry, there was a problem in creating your order. Please try later.");
|
||||
@ -174,7 +176,8 @@ public class Commander {
|
||||
Retry.Operation op = (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
LOG.debug("Order " + order.id + ": Error in connecting to payment service, trying again..");
|
||||
LOG.debug("Order " + order.id + ": Error in connecting to payment service,"
|
||||
+ " trying again..");
|
||||
} else {
|
||||
LOG.debug("Order " + order.id + ": Error in creating payment request..");
|
||||
}
|
||||
@ -183,7 +186,8 @@ public class Commander {
|
||||
if (order.paid.equals(PaymentStatus.Trying)) {
|
||||
String transactionId = paymentService.receiveRequest(order.price);
|
||||
order.paid = PaymentStatus.Done;
|
||||
LOG.info("Order " + order.id + ": Payment successful, transaction Id: " + transactionId);
|
||||
LOG.info("Order " + order.id + ": Payment successful, transaction Id: "
|
||||
+ transactionId);
|
||||
if (!finalSiteMsgShown) {
|
||||
LOG.info("Payment made successfully, thank you for shopping with us!!");
|
||||
finalSiteMsgShown = true;
|
||||
@ -192,10 +196,11 @@ public class Commander {
|
||||
return;
|
||||
}
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o,err) -> {
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
if (PaymentDetailsErrorException.class.isAssignableFrom(err.getClass())) {
|
||||
if (!finalSiteMsgShown) {
|
||||
LOG.info("There was an error in payment. Your account/card details may have been incorrect. "
|
||||
LOG.info("There was an error in payment. Your account/card details "
|
||||
+ "may have been incorrect. "
|
||||
+ "Meanwhile, your order has been converted to COD and will be shipped.");
|
||||
finalSiteMsgShown = true;
|
||||
}
|
||||
@ -213,8 +218,9 @@ public class Commander {
|
||||
LOG.warn("Order " + order.id + ": Payment error, going to queue..");
|
||||
sendPaymentPossibleErrorMsg(o);
|
||||
}
|
||||
if (o.paid.equals(PaymentStatus.Trying) && System.currentTimeMillis() - o.createdTime < paymentTime) {
|
||||
QueueTask qt = new QueueTask(o, TaskType.Payment,-1);
|
||||
if (o.paid.equals(PaymentStatus.Trying) && System
|
||||
.currentTimeMillis() - o.createdTime < paymentTime) {
|
||||
QueueTask qt = new QueueTask(o, TaskType.Payment, -1);
|
||||
updateQueue(qt);
|
||||
}
|
||||
} catch (InterruptedException e1) {
|
||||
@ -237,7 +243,8 @@ public class Commander {
|
||||
|
||||
private void updateQueue(QueueTask qt) throws InterruptedException {
|
||||
if (System.currentTimeMillis() - qt.order.createdTime >= this.queueTime) {
|
||||
//since payment time is lesser than queuetime it would have already failed..additional check not needed
|
||||
// since payment time is lesser than queuetime it would have already failed..
|
||||
// additional check not needed
|
||||
LOG.trace("Order " + qt.order.id + ": Queue time for order over, failed..");
|
||||
return;
|
||||
} else if (qt.taskType.equals(TaskType.Payment) && !qt.order.paid.equals(PaymentStatus.Trying)
|
||||
@ -264,11 +271,12 @@ public class Commander {
|
||||
tryDoingTasksInQueue();
|
||||
return;
|
||||
};
|
||||
Retry.HandleErrorIssue<QueueTask> handleError = (qt,err) -> {
|
||||
Retry.HandleErrorIssue<QueueTask> handleError = (qt, err) -> {
|
||||
if (qt.taskType.equals(TaskType.Payment)) {
|
||||
qt.order.paid = PaymentStatus.NotDone;
|
||||
sendPaymentFailureMessage(qt.order);
|
||||
LOG.error("Order " + qt.order.id + ": Unable to enqueue payment task, payment failed..");
|
||||
LOG.error("Order " + qt.order.id + ": Unable to enqueue payment task,"
|
||||
+ " payment failed..");
|
||||
}
|
||||
LOG.error("Order " + qt.order.id + ": Unable to enqueue task of type " + qt.getType()
|
||||
+ ", trying to add to employee handle..");
|
||||
@ -300,7 +308,7 @@ public class Commander {
|
||||
doTasksInQueue();
|
||||
return;
|
||||
};
|
||||
Retry.HandleErrorIssue<QueueTask> handleError = (o,err) -> {
|
||||
Retry.HandleErrorIssue<QueueTask> handleError = (o, err) -> {
|
||||
return;
|
||||
};
|
||||
Retry<QueueTask> r = new Retry<QueueTask>(op, handleError, numOfRetries, retryDuration,
|
||||
@ -329,7 +337,7 @@ public class Commander {
|
||||
queueItems--;
|
||||
return;
|
||||
};
|
||||
Retry.HandleErrorIssue<QueueTask> handleError = (o,err) -> {
|
||||
Retry.HandleErrorIssue<QueueTask> handleError = (o, err) -> {
|
||||
return;
|
||||
};
|
||||
Retry<QueueTask> r = new Retry<QueueTask>(op, handleError, numOfRetries, retryDuration,
|
||||
@ -359,7 +367,8 @@ public class Commander {
|
||||
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
|
||||
+ "(Payment Success msg), trying again..");
|
||||
} else {
|
||||
LOG.debug("Order " + order.id + ": Error in creating Payment Success messaging request..");
|
||||
LOG.debug("Order " + order.id + ": Error in creating Payment Success"
|
||||
+ " messaging request..");
|
||||
}
|
||||
throw l.remove(0);
|
||||
}
|
||||
@ -367,18 +376,20 @@ public class Commander {
|
||||
&& !order.messageSent.equals(MessageSent.PaymentSuccessful)) {
|
||||
String requestId = messagingService.receiveRequest(2);
|
||||
order.messageSent = MessageSent.PaymentSuccessful;
|
||||
LOG.info("Order " + order.id + ": Payment Success message sent, request Id: " + requestId);
|
||||
LOG.info("Order " + order.id + ": Payment Success message sent,"
|
||||
+ " request Id: " + requestId);
|
||||
}
|
||||
return;
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o,err) -> {
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
try {
|
||||
if ((o.messageSent.equals(MessageSent.NoneSent) || o.messageSent.equals(MessageSent.PaymentTrying))
|
||||
if ((o.messageSent.equals(MessageSent.NoneSent) || o.messageSent
|
||||
.equals(MessageSent.PaymentTrying))
|
||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||
QueueTask qt = new QueueTask(order, TaskType.Messaging,2);
|
||||
QueueTask qt = new QueueTask(order, TaskType.Messaging, 2);
|
||||
updateQueue(qt);
|
||||
LOG.info("Order " + order.id + ": Error in sending Payment Success message, trying to "
|
||||
+ "queue task and add to employee handle..");
|
||||
LOG.info("Order " + order.id + ": Error in sending Payment Success message, trying to"
|
||||
+ " queue task and add to employee handle..");
|
||||
employeeHandleIssue(order);
|
||||
}
|
||||
} catch (InterruptedException e1) {
|
||||
@ -413,7 +424,8 @@ public class Commander {
|
||||
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
|
||||
+ "(Payment Failure msg), trying again..");
|
||||
} else {
|
||||
LOG.debug("Order " + order.id + ": Error in creating Payment Failure message request..");
|
||||
LOG.debug("Order " + order.id + ": Error in creating Payment Failure"
|
||||
+ " message request..");
|
||||
}
|
||||
throw l.remove(0);
|
||||
}
|
||||
@ -421,15 +433,17 @@ public class Commander {
|
||||
&& !order.messageSent.equals(MessageSent.PaymentSuccessful)) {
|
||||
String requestId = messagingService.receiveRequest(0);
|
||||
order.messageSent = MessageSent.PaymentFail;
|
||||
LOG.info("Order " + order.id + ": Payment Failure message sent successfully, request Id: " + requestId);
|
||||
LOG.info("Order " + order.id + ": Payment Failure message sent successfully,"
|
||||
+ " request Id: " + requestId);
|
||||
}
|
||||
return;
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o,err) -> {
|
||||
if ((o.messageSent.equals(MessageSent.NoneSent) || o.messageSent.equals(MessageSent.PaymentTrying))
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
if ((o.messageSent.equals(MessageSent.NoneSent) || o.messageSent
|
||||
.equals(MessageSent.PaymentTrying))
|
||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||
try {
|
||||
QueueTask qt = new QueueTask(order, TaskType.Messaging,0);
|
||||
QueueTask qt = new QueueTask(order, TaskType.Messaging, 0);
|
||||
updateQueue(qt);
|
||||
LOG.warn("Order " + order.id + ": Error in sending Payment Failure message, "
|
||||
+ "trying to queue task and add to employee handle..");
|
||||
@ -467,22 +481,26 @@ public class Commander {
|
||||
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
|
||||
+ "(Payment Error msg), trying again..");
|
||||
} else {
|
||||
LOG.debug("Order " + order.id + ": Error in creating Payment Error messaging request..");
|
||||
LOG.debug("Order " + order.id + ": Error in creating Payment Error"
|
||||
+ " messaging request..");
|
||||
}
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (order.paid.equals(PaymentStatus.Trying) && order.messageSent.equals(MessageSent.NoneSent)) {
|
||||
if (order.paid.equals(PaymentStatus.Trying) && order.messageSent
|
||||
.equals(MessageSent.NoneSent)) {
|
||||
String requestId = messagingService.receiveRequest(1);
|
||||
order.messageSent = MessageSent.PaymentTrying;
|
||||
LOG.info("Order " + order.id + ": Payment Error message sent successfully, request Id: " + requestId);
|
||||
LOG.info("Order " + order.id + ": Payment Error message sent successfully,"
|
||||
+ " request Id: " + requestId);
|
||||
}
|
||||
return;
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o,err) -> {
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
try {
|
||||
if (o.messageSent.equals(MessageSent.NoneSent) && order.paid.equals(PaymentStatus.Trying)
|
||||
if (o.messageSent.equals(MessageSent.NoneSent) && order.paid
|
||||
.equals(PaymentStatus.Trying)
|
||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||
QueueTask qt = new QueueTask(order,TaskType.Messaging,1);
|
||||
QueueTask qt = new QueueTask(order, TaskType.Messaging, 1);
|
||||
updateQueue(qt);
|
||||
LOG.warn("Order " + order.id + ": Error in sending Payment Error message, "
|
||||
+ "trying to queue task and add to employee handle..");
|
||||
@ -516,7 +534,8 @@ public class Commander {
|
||||
public void run() {
|
||||
Retry.Operation op = (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
LOG.warn("Order " + order.id + ": Error in connecting to employee handle, trying again..");
|
||||
LOG.warn("Order " + order.id + ": Error in connecting to employee handle,"
|
||||
+ " trying again..");
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (!order.addedToEmployeeHandle) {
|
||||
@ -526,12 +545,14 @@ public class Commander {
|
||||
}
|
||||
return;
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o,err) -> {
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
try {
|
||||
if (!o.addedToEmployeeHandle && System.currentTimeMillis() - order.createdTime < employeeTime) {
|
||||
QueueTask qt = new QueueTask(order, TaskType.EmployeeDb,-1);
|
||||
if (!o.addedToEmployeeHandle && System
|
||||
.currentTimeMillis() - order.createdTime < employeeTime) {
|
||||
QueueTask qt = new QueueTask(order, TaskType.EmployeeDb, -1);
|
||||
updateQueue(qt);
|
||||
LOG.warn("Order " + order.id + ": Error in adding to employee db, trying to queue task..");
|
||||
LOG.warn("Order " + order.id + ": Error in adding to employee db,"
|
||||
+ " trying to queue task..");
|
||||
return;
|
||||
}
|
||||
} catch (InterruptedException e1) {
|
||||
@ -580,7 +601,8 @@ public class Commander {
|
||||
} else if (qt.messageType == 1 && (!qt.order.messageSent.equals(MessageSent.NoneSent)
|
||||
|| !qt.order.paid.equals(PaymentStatus.Trying))) {
|
||||
tryDequeue();
|
||||
LOG.trace("Order " + qt.order.id + ": This messaging task does not need to be done, dequeue..");
|
||||
LOG.trace("Order " + qt.order.id + ": This messaging task does not need to be done,"
|
||||
+ " dequeue..");
|
||||
} else if (qt.messageType == 0) {
|
||||
sendPaymentFailureMessage(qt.order);
|
||||
LOG.debug("Order " + qt.order.id + ": Trying to connect to messaging service..");
|
||||
@ -594,7 +616,8 @@ public class Commander {
|
||||
} else if (qt.taskType.equals(TaskType.EmployeeDb)) {
|
||||
if (qt.order.addedToEmployeeHandle) {
|
||||
tryDequeue();
|
||||
LOG.trace("Order " + qt.order.id + ": This employee handle task already done, dequeue..");
|
||||
LOG.trace("Order " + qt.order.id + ": This employee handle task already done,"
|
||||
+ " dequeue..");
|
||||
} else {
|
||||
employeeHandleIssue(qt.order);
|
||||
LOG.debug("Order " + qt.order.id + ": Trying to connect to employee handle..");
|
||||
|
@ -26,12 +26,14 @@ package com.iluwatar.commander;
|
||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||
|
||||
/**
|
||||
* Database abstract class is extended by all databases in our example. The add and get
|
||||
* methods are used by the respective service to add to database or get from database.
|
||||
* Database abstract class is extended by all databases in our example. The add and get methods are
|
||||
* used by the respective service to add to database or get from database.
|
||||
*
|
||||
* @param <T> T is the type of object being held by database.
|
||||
*/
|
||||
|
||||
public abstract class Database<T> {
|
||||
public abstract T add(T obj) throws DatabaseUnavailableException;
|
||||
public abstract T get(String tId) throws DatabaseUnavailableException;
|
||||
|
||||
public abstract T get(String id) throws DatabaseUnavailableException;
|
||||
}
|
||||
|
@ -34,11 +34,15 @@ public class Order { //can store all transactions ids also
|
||||
|
||||
enum PaymentStatus {
|
||||
NotDone, Trying, Done
|
||||
};
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
enum MessageSent {
|
||||
NoneSent, PaymentFail, PaymentTrying, PaymentSuccessful
|
||||
};
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
final User user;
|
||||
final String item;
|
||||
|
@ -30,7 +30,8 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Retry pattern
|
||||
* Retry pattern.
|
||||
*
|
||||
* @param <T> is the type of object passed into HandleErrorIssue as a parameter.
|
||||
*/
|
||||
|
||||
@ -46,6 +47,7 @@ public class Retry<T> {
|
||||
|
||||
/**
|
||||
* HandleErrorIssue defines how to handle errors.
|
||||
*
|
||||
* @param <T> is the type of object to be passed into the method as parameter.
|
||||
*/
|
||||
|
||||
@ -76,6 +78,7 @@ public class Retry<T> {
|
||||
|
||||
/**
|
||||
* Performing the operation with retries.
|
||||
*
|
||||
* @param list is the exception list
|
||||
* @param obj is the parameter to be passed into handleIsuue method
|
||||
*/
|
||||
@ -92,15 +95,15 @@ public class Retry<T> {
|
||||
return; //return here...dont go further
|
||||
}
|
||||
try {
|
||||
long testDelay = (long) Math.pow(2, this.attempts.intValue()) * 1000 + RANDOM.nextInt(1000);
|
||||
long testDelay =
|
||||
(long) Math.pow(2, this.attempts.intValue()) * 1000 + RANDOM.nextInt(1000);
|
||||
long delay = testDelay < this.maxDelay ? testDelay : maxDelay;
|
||||
Thread.sleep(delay);
|
||||
} catch (InterruptedException f) {
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
while (true);
|
||||
} while (true);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,18 +23,19 @@
|
||||
|
||||
package com.iluwatar.commander;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Service class is an abstract class extended by all services in this example. They
|
||||
* all have a public receiveRequest method to receive requests, which could also contain
|
||||
* details of the user other than the implementation details (though we are not doing
|
||||
* that here) and updateDb method which adds to their respective databases. There is a
|
||||
* method to generate transaction/request id for the transactions/requests, which are
|
||||
* then sent back. These could be stored by the {@link Commander} class in a separate
|
||||
* database for reference (though we are not doing that here).
|
||||
* Service class is an abstract class extended by all services in this example. They all have a
|
||||
* public receiveRequest method to receive requests, which could also contain details of the user
|
||||
* other than the implementation details (though we are not doing that here) and updateDb method
|
||||
* which adds to their respective databases. There is a method to generate transaction/request id
|
||||
* for the transactions/requests, which are then sent back. These could be stored by the {@link
|
||||
* Commander} class in a separate database for reference (though we are not doing that here).
|
||||
*/
|
||||
|
||||
public abstract class Service {
|
||||
@ -45,13 +46,14 @@ public abstract class Service {
|
||||
private static final String ALL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
||||
private static final Hashtable<String, Boolean> USED_IDS = new Hashtable<>();
|
||||
|
||||
protected Service(Database db, Exception...exc) {
|
||||
protected Service(Database db, Exception... exc) {
|
||||
this.database = db;
|
||||
this.exceptionsList = new ArrayList<>(List.of(exc));
|
||||
}
|
||||
|
||||
public abstract String receiveRequest(Object...parameters) throws DatabaseUnavailableException;
|
||||
protected abstract String updateDb(Object...parameters) throws DatabaseUnavailableException;
|
||||
public abstract String receiveRequest(Object... parameters) throws DatabaseUnavailableException;
|
||||
|
||||
protected abstract String updateDb(Object... parameters) throws DatabaseUnavailableException;
|
||||
|
||||
protected String generateId() {
|
||||
StringBuilder random = new StringBuilder();
|
||||
|
@ -23,10 +23,10 @@
|
||||
|
||||
package com.iluwatar.commander.employeehandle;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import com.iluwatar.commander.Database;
|
||||
import com.iluwatar.commander.Order;
|
||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||
import java.util.Hashtable;
|
||||
|
||||
/**
|
||||
* The Employee Database is where orders which have encountered some issue(s) are added.
|
||||
@ -41,11 +41,11 @@ public class EmployeeDatabase extends Database<Order> {
|
||||
|
||||
@Override
|
||||
public Order add(Order o) throws DatabaseUnavailableException {
|
||||
return data.put(o.id,o);
|
||||
return data.put(o.id, o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Order get(String oId) throws DatabaseUnavailableException {
|
||||
return data.get(oId);
|
||||
public Order get(String orderId) throws DatabaseUnavailableException {
|
||||
return data.get(orderId);
|
||||
}
|
||||
}
|
||||
|
@ -28,21 +28,21 @@ import com.iluwatar.commander.Service;
|
||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||
|
||||
/**
|
||||
* The EmployeeHandle class is the middle-man between {@link Commander} and
|
||||
* {@link EmployeeDatabase}.
|
||||
* The EmployeeHandle class is the middle-man between {@link Commander} and {@link
|
||||
* EmployeeDatabase}.
|
||||
*/
|
||||
|
||||
public class EmployeeHandle extends Service {
|
||||
|
||||
public EmployeeHandle(EmployeeDatabase db, Exception...exc) {
|
||||
public EmployeeHandle(EmployeeDatabase db, Exception... exc) {
|
||||
super(db, exc);
|
||||
}
|
||||
|
||||
public String receiveRequest(Object...parameters) throws DatabaseUnavailableException {
|
||||
return updateDb((Order)parameters[0]);
|
||||
public String receiveRequest(Object... parameters) throws DatabaseUnavailableException {
|
||||
return updateDb((Order) parameters[0]);
|
||||
}
|
||||
|
||||
protected String updateDb(Object...parameters) throws DatabaseUnavailableException {
|
||||
protected String updateDb(Object... parameters) throws DatabaseUnavailableException {
|
||||
Order o = (Order) parameters[0];
|
||||
if (database.get(o.id) == null) {
|
||||
database.add(o);
|
||||
|
@ -24,8 +24,8 @@
|
||||
package com.iluwatar.commander.exceptions;
|
||||
|
||||
/**
|
||||
* DatabaseUnavailableException is thrown when database is unavailable and nothing
|
||||
* can be added or retrieved.
|
||||
* DatabaseUnavailableException is thrown when database is unavailable and nothing can be added or
|
||||
* retrieved.
|
||||
*/
|
||||
|
||||
public class DatabaseUnavailableException extends Exception {
|
||||
|
@ -24,8 +24,8 @@
|
||||
package com.iluwatar.commander.exceptions;
|
||||
|
||||
/**
|
||||
* PaymentDetailsErrorException is thrown when the details entered are incorrect or
|
||||
* payment cannot be made with the details given.
|
||||
* PaymentDetailsErrorException is thrown when the details entered are incorrect or payment cannot
|
||||
* be made with the details given.
|
||||
*/
|
||||
|
||||
public class PaymentDetailsErrorException extends Exception {
|
||||
|
@ -24,8 +24,8 @@
|
||||
package com.iluwatar.commander.exceptions;
|
||||
|
||||
/**
|
||||
* ShippingNotPossibleException is thrown when the address entered cannot be shipped to
|
||||
* by service currently for some reason.
|
||||
* ShippingNotPossibleException is thrown when the address entered cannot be shipped to by service
|
||||
* currently for some reason.
|
||||
*/
|
||||
|
||||
public class ShippingNotPossibleException extends Exception {
|
||||
|
@ -23,10 +23,10 @@
|
||||
|
||||
package com.iluwatar.commander.messagingservice;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import com.iluwatar.commander.Database;
|
||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||
import com.iluwatar.commander.messagingservice.MessagingService.MessageRequest;
|
||||
import java.util.Hashtable;
|
||||
|
||||
/**
|
||||
* The MessagingDatabase is where the MessageRequest is added.
|
||||
@ -45,8 +45,8 @@ public class MessagingDatabase extends Database<MessageRequest> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageRequest get(String rId) throws DatabaseUnavailableException {
|
||||
return data.get(rId);
|
||||
public MessageRequest get(String requestId) throws DatabaseUnavailableException {
|
||||
return data.get(requestId);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -29,9 +29,9 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* The MessagingService is used to send messages to user regarding their order and
|
||||
* payment status. In case an error is encountered in payment and this service is
|
||||
* found to be unavailable, the order is added to the {@link EmployeeDatabase}.
|
||||
* The MessagingService is used to send messages to user regarding their order and payment status.
|
||||
* In case an error is encountered in payment and this service is found to be unavailable, the order
|
||||
* is added to the {@link EmployeeDatabase}.
|
||||
*/
|
||||
|
||||
public class MessagingService extends Service {
|
||||
@ -39,7 +39,9 @@ public class MessagingService extends Service {
|
||||
|
||||
enum MessageToSend {
|
||||
PaymentFail, PaymentTrying, PaymentSuccessful
|
||||
};
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
class MessageRequest {
|
||||
String reqId;
|
||||
@ -51,17 +53,16 @@ public class MessagingService extends Service {
|
||||
}
|
||||
}
|
||||
|
||||
public MessagingService(MessagingDatabase db, Exception...exc) {
|
||||
public MessagingService(MessagingDatabase db, Exception... exc) {
|
||||
super(db, exc);
|
||||
}
|
||||
|
||||
/**
|
||||
* Public method which will receive request from {@link Commander}.
|
||||
*/
|
||||
|
||||
public String receiveRequest(Object...parameters) throws DatabaseUnavailableException {
|
||||
public String receiveRequest(Object... parameters) throws DatabaseUnavailableException {
|
||||
int messageToSend = (int) parameters[0];
|
||||
String rId = generateId();
|
||||
String id = generateId();
|
||||
MessageToSend msg = null;
|
||||
if (messageToSend == 0) {
|
||||
msg = MessageToSend.PaymentFail;
|
||||
@ -70,11 +71,11 @@ public class MessagingService extends Service {
|
||||
} else { //messageToSend == 2
|
||||
msg = MessageToSend.PaymentSuccessful;
|
||||
}
|
||||
MessageRequest req = new MessageRequest(rId, msg);
|
||||
MessageRequest req = new MessageRequest(id, msg);
|
||||
return updateDb(req);
|
||||
}
|
||||
|
||||
protected String updateDb(Object...parameters) throws DatabaseUnavailableException {
|
||||
protected String updateDb(Object... parameters) throws DatabaseUnavailableException {
|
||||
MessageRequest req = (MessageRequest) parameters[0];
|
||||
if (this.database.get(req.reqId) == null) { //idempotence, in case db fails here
|
||||
database.add(req); //if successful:
|
||||
@ -86,13 +87,17 @@ public class MessagingService extends Service {
|
||||
|
||||
String sendMessage(MessageToSend m) {
|
||||
if (m.equals(MessageToSend.PaymentSuccessful)) {
|
||||
return "Msg: Your order has been placed and paid for successfully! Thank you for shopping with us!";
|
||||
return "Msg: Your order has been placed and paid for successfully!"
|
||||
+ " Thank you for shopping with us!";
|
||||
} else if (m.equals(MessageToSend.PaymentTrying)) {
|
||||
return "Msg: There was an error in your payment process, we are working on it and will return back to you"
|
||||
+ " shortly. Meanwhile, your order has been placed and will be shipped.";
|
||||
return "Msg: There was an error in your payment process,"
|
||||
+ " we are working on it and will return back to you shortly."
|
||||
+ " Meanwhile, your order has been placed and will be shipped.";
|
||||
} else {
|
||||
return "Msg: There was an error in your payment process. Your order is placed and has been converted to COD."
|
||||
+ " Please reach us on CUSTOMER-CARE-NUBER in case of any queries. Thank you for shopping with us!";
|
||||
return "Msg: There was an error in your payment process."
|
||||
+ " Your order is placed and has been converted to COD."
|
||||
+ " Please reach us on CUSTOMER-CARE-NUBER in case of any queries."
|
||||
+ " Thank you for shopping with us!";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -23,11 +23,10 @@
|
||||
|
||||
package com.iluwatar.commander.paymentservice;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import com.iluwatar.commander.Database;
|
||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||
import com.iluwatar.commander.paymentservice.PaymentService.PaymentRequest;
|
||||
import java.util.Hashtable;
|
||||
|
||||
/**
|
||||
* PaymentDatabase is where the PaymentRequest is added, along with details.
|
||||
@ -48,8 +47,8 @@ public class PaymentDatabase extends Database<PaymentRequest> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PaymentRequest get(String tId) throws DatabaseUnavailableException {
|
||||
return data.get(tId);
|
||||
public PaymentRequest get(String requestId) throws DatabaseUnavailableException {
|
||||
return data.get(requestId);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,8 +27,8 @@ import com.iluwatar.commander.Service;
|
||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||
|
||||
/**
|
||||
* The PaymentService class receives request from the {@link Commander} and adds
|
||||
* to the {@link PaymentDatabase}.
|
||||
* The PaymentService class receives request from the {@link Commander} and adds to the {@link
|
||||
* PaymentDatabase}.
|
||||
*/
|
||||
|
||||
public class PaymentService extends Service {
|
||||
@ -45,7 +45,7 @@ public class PaymentService extends Service {
|
||||
}
|
||||
}
|
||||
|
||||
public PaymentService(PaymentDatabase db, Exception...exc) {
|
||||
public PaymentService(PaymentDatabase db, Exception... exc) {
|
||||
super(db, exc);
|
||||
}
|
||||
|
||||
@ -53,14 +53,14 @@ public class PaymentService extends Service {
|
||||
* Public method which will receive request from {@link Commander}.
|
||||
*/
|
||||
|
||||
public String receiveRequest(Object...parameters) throws DatabaseUnavailableException {
|
||||
public String receiveRequest(Object... parameters) throws DatabaseUnavailableException {
|
||||
//it could also be sending a userid, payment details here or something, not added here
|
||||
String tId = generateId();
|
||||
PaymentRequest req = new PaymentRequest(tId, (float)parameters[0]);
|
||||
String id = generateId();
|
||||
PaymentRequest req = new PaymentRequest(id, (float) parameters[0]);
|
||||
return updateDb(req);
|
||||
}
|
||||
|
||||
protected String updateDb(Object...parameters) throws DatabaseUnavailableException {
|
||||
protected String updateDb(Object... parameters) throws DatabaseUnavailableException {
|
||||
PaymentRequest req = (PaymentRequest) parameters[0];
|
||||
if (database.get(req.transactionId) == null || !req.paid) {
|
||||
database.add(req);
|
||||
|
@ -27,6 +27,7 @@ import com.iluwatar.commander.exceptions.IsEmptyException;
|
||||
|
||||
/**
|
||||
* Queue data structure implementation.
|
||||
*
|
||||
* @param <T> is the type of object the queue will hold.
|
||||
*/
|
||||
|
||||
@ -47,9 +48,8 @@ public class Queue<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Queue constructor
|
||||
* Queue constructor.
|
||||
*/
|
||||
|
||||
Queue() {
|
||||
front = null;
|
||||
rear = null;
|
||||
@ -91,7 +91,7 @@ public class Queue<T> {
|
||||
if (isEmpty()) {
|
||||
throw new IsEmptyException();
|
||||
} else {
|
||||
return (T)front.value;
|
||||
return (T) front.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,6 @@ package com.iluwatar.commander.queue;
|
||||
import com.iluwatar.commander.Database;
|
||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||
import com.iluwatar.commander.exceptions.IsEmptyException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -39,7 +38,7 @@ public class QueueDatabase extends Database<QueueTask> {
|
||||
private Queue<QueueTask> data;
|
||||
public ArrayList<Exception> exceptionsList;
|
||||
|
||||
public QueueDatabase(Exception...exc) {
|
||||
public QueueDatabase(Exception... exc) {
|
||||
this.data = new Queue<>();
|
||||
this.exceptionsList = new ArrayList<>(List.of(exc));
|
||||
}
|
||||
@ -52,7 +51,8 @@ public class QueueDatabase extends Database<QueueTask> {
|
||||
}
|
||||
|
||||
/**
|
||||
* peek method returns object at front without removing it from queue
|
||||
* peek method returns object at front without removing it from queue.
|
||||
*
|
||||
* @return object at front of queue
|
||||
* @throws IsEmptyException if queue is empty
|
||||
* @throws DatabaseUnavailableException if queue db is unavailable
|
||||
@ -64,7 +64,8 @@ public class QueueDatabase extends Database<QueueTask> {
|
||||
}
|
||||
|
||||
/**
|
||||
* dequeue method removes the object at front and returns it
|
||||
* dequeue method removes the object at front and returns it.
|
||||
*
|
||||
* @return object at front of queue
|
||||
* @throws IsEmptyException if queue is empty
|
||||
* @throws DatabaseUnavailableException if queue db is unavailable
|
||||
@ -76,7 +77,7 @@ public class QueueDatabase extends Database<QueueTask> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueueTask get(String tId) throws DatabaseUnavailableException {
|
||||
public QueueTask get(String taskId) throws DatabaseUnavailableException {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -31,13 +31,15 @@ import com.iluwatar.commander.Order;
|
||||
|
||||
public class QueueTask {
|
||||
|
||||
/**
|
||||
/**
|
||||
* TaskType is the type of task to be done.
|
||||
*/
|
||||
|
||||
public enum TaskType {
|
||||
Messaging, Payment, EmployeeDb
|
||||
};
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
public Order order;
|
||||
public TaskType taskType;
|
||||
@ -46,12 +48,13 @@ public class QueueTask {
|
||||
but keeping it simple here*/
|
||||
public long firstAttemptTime; //when first time attempt made to do task
|
||||
|
||||
/**
|
||||
* QueueTask constructor
|
||||
/**
|
||||
* QueueTask constructor.
|
||||
*
|
||||
* @param o is the order for which the queuetask is being created
|
||||
* @param t is the type of task to be done
|
||||
* @param messageType if it is a message, which type of message - this could have instead been object varargs,
|
||||
* and contained all additional details related to tasktype.
|
||||
* @param messageType if it is a message, which type of message - this could have instead been
|
||||
* object varargs, and contained all additional details related to tasktype.
|
||||
*/
|
||||
|
||||
public QueueTask(Order o, TaskType t, int messageType) {
|
||||
@ -62,7 +65,8 @@ public class QueueTask {
|
||||
}
|
||||
|
||||
/**
|
||||
* getType method
|
||||
* getType method.
|
||||
*
|
||||
* @return String representing type of task
|
||||
*/
|
||||
|
||||
|
@ -23,10 +23,10 @@
|
||||
|
||||
package com.iluwatar.commander.shippingservice;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import com.iluwatar.commander.Database;
|
||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||
import com.iluwatar.commander.shippingservice.ShippingService.ShippingRequest;
|
||||
import java.util.Hashtable;
|
||||
|
||||
/**
|
||||
* ShippingDatabase is where the ShippingRequest objects are added.
|
||||
@ -45,8 +45,8 @@ public class ShippingDatabase extends Database<ShippingRequest> {
|
||||
return data.put(r.transactionId, r);
|
||||
}
|
||||
|
||||
public ShippingRequest get(String transactionId) throws DatabaseUnavailableException {
|
||||
return data.get(transactionId);
|
||||
public ShippingRequest get(String trasnactionId) throws DatabaseUnavailableException {
|
||||
return data.get(trasnactionId);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,8 +27,8 @@ import com.iluwatar.commander.Service;
|
||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||
|
||||
/**
|
||||
* ShippingService class receives request from {@link Commander} class and adds it
|
||||
* to the {@link ShippingDatabase}.
|
||||
* ShippingService class receives request from {@link Commander} class and adds it to the {@link
|
||||
* ShippingDatabase}.
|
||||
*/
|
||||
|
||||
public class ShippingService extends Service {
|
||||
@ -45,7 +45,7 @@ public class ShippingService extends Service {
|
||||
}
|
||||
}
|
||||
|
||||
public ShippingService(ShippingDatabase db, Exception...exc) {
|
||||
public ShippingService(ShippingDatabase db, Exception... exc) {
|
||||
super(db, exc);
|
||||
}
|
||||
|
||||
@ -53,13 +53,14 @@ public class ShippingService extends Service {
|
||||
* Public method which will receive request from {@link Commander}.
|
||||
*/
|
||||
|
||||
public String receiveRequest(Object...parameters) throws DatabaseUnavailableException {
|
||||
String tId = generateId();
|
||||
ShippingRequest req = new ShippingRequest(tId, (String) parameters[0] /*item*/, (String) parameters[1]/*address*/);
|
||||
public String receiveRequest(Object... parameters) throws DatabaseUnavailableException {
|
||||
String id = generateId();
|
||||
ShippingRequest req =
|
||||
new ShippingRequest(id, (String) parameters[0] /*item*/, (String) parameters[1]/*address*/);
|
||||
return updateDb(req);
|
||||
}
|
||||
|
||||
protected String updateDb(Object...parameters) throws DatabaseUnavailableException {
|
||||
protected String updateDb(Object... parameters) throws DatabaseUnavailableException {
|
||||
ShippingRequest req = (ShippingRequest) parameters[0];
|
||||
if (this.database.get(req.transactionId) == null) {
|
||||
database.add(req);
|
||||
|
Loading…
x
Reference in New Issue
Block a user