Java 11 migration: patterns (remaining b-c) (#1081)
* Moves business-delegate pattern to java 11 * Moves bytecode pattern to java 11 * Moves caching pattern to java 11 * Moves callback pattern to java 11 * Moves chain pattern to java 11 * Moves circuit-breaker pattern to java 11 * Moves collection-pipeline pattern to java 11 * Moves command pattern to java 11 * Moves commander pattern to java 11 * Moves composite pattern to java 11 * Corrects test cases
This commit is contained in:
committed by
Ilkka Seppälä
parent
6ef840f3cf
commit
33ea7335b1
@ -40,51 +40,47 @@ import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
* available/unavailable.
|
||||
*/
|
||||
public class AppEmployeeDbFailCases {
|
||||
final int numOfRetries = 3;
|
||||
final long retryDuration = 30000;
|
||||
final long queueTime = 240000; //4 mins
|
||||
final long queueTaskTime = 60000; //1 min
|
||||
final long paymentTime = 120000; //2 mins
|
||||
final long messageTime = 150000; //2.5 mins
|
||||
final long employeeTime = 240000; //4 mins
|
||||
private final int numOfRetries = 3;
|
||||
private final long retryDuration = 30000;
|
||||
private final long queueTime = 240000; //4 mins
|
||||
private final long queueTaskTime = 60000; //1 min
|
||||
private final long paymentTime = 120000; //2 mins
|
||||
private final long messageTime = 150000; //2.5 mins
|
||||
private 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());
|
||||
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 =
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
var ss = new ShippingService(new ShippingDatabase());
|
||||
var ms = new MessagingService(new MessagingDatabase());
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
var 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,
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void employeeDbSuccessCase() throws Exception {
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
||||
ShippingService ss =
|
||||
new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
||||
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,
|
||||
var ps = new PaymentService(new PaymentDatabase());
|
||||
var ss = new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
||||
var ms = new MessagingService(new MessagingDatabase());
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
var qdb = new QueueDatabase();
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
@ -95,7 +91,7 @@ public class AppEmployeeDbFailCases {
|
||||
*/
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
AppEmployeeDbFailCases aefc = new AppEmployeeDbFailCases();
|
||||
var aefc = new AppEmployeeDbFailCases();
|
||||
//aefc.employeeDatabaseUnavailableCase();
|
||||
aefc.employeeDbSuccessCase();
|
||||
}
|
||||
|
@ -40,102 +40,95 @@ import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
*/
|
||||
|
||||
public class AppMessagingFailCases {
|
||||
final int numOfRetries = 3;
|
||||
final long retryDuration = 30000;
|
||||
final long queueTime = 240000; //4 mins
|
||||
final long queueTaskTime = 60000; //1 min
|
||||
final long paymentTime = 120000; //2 mins
|
||||
final long messageTime = 150000; //2.5 mins
|
||||
final long employeeTime = 240000; //4 mins
|
||||
private final int numOfRetries = 3;
|
||||
private final long retryDuration = 30000;
|
||||
private final long queueTime = 240000; //4 mins
|
||||
private final long queueTaskTime = 60000; //1 min
|
||||
private final long paymentTime = 120000; //2 mins
|
||||
private final long messageTime = 150000; //2.5 mins
|
||||
private final long employeeTime = 240000; //4 mins
|
||||
|
||||
void messagingDatabaseUnavailableCasePaymentSuccess() throws Exception {
|
||||
//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());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase();
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
var ps = new PaymentService(new PaymentDatabase());
|
||||
var ss = new ShippingService(new ShippingDatabase());
|
||||
var ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
var qdb = new QueueDatabase();
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
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());
|
||||
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());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase();
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
var ss = new ShippingService(new ShippingDatabase());
|
||||
var 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());
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
var qdb = new QueueDatabase();
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
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 =
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
var ss = new ShippingService(new ShippingDatabase());
|
||||
var ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
var qdb =
|
||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
Commander c =
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void messagingSuccessCase() throws Exception {
|
||||
//done here
|
||||
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());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb = new QueueDatabase();
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
var ss = new ShippingService(new ShippingDatabase());
|
||||
var ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
var qdb = new QueueDatabase();
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
@ -146,7 +139,7 @@ public class AppMessagingFailCases {
|
||||
*/
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
AppMessagingFailCases amfc = new AppMessagingFailCases();
|
||||
var amfc = new AppMessagingFailCases();
|
||||
//amfc.messagingDatabaseUnavailableCasePaymentSuccess();
|
||||
//amfc.messagingDatabaseUnavailableCasePaymentError();
|
||||
//amfc.messagingDatabaseUnavailableCasePaymentFailure();
|
||||
|
@ -40,62 +40,58 @@ import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
*/
|
||||
|
||||
public class AppPaymentFailCases {
|
||||
final int numOfRetries = 3;
|
||||
final long retryDuration = 30000;
|
||||
final long queueTime = 240000; //4 mins
|
||||
final long queueTaskTime = 60000; //1 min
|
||||
final long paymentTime = 120000; //2 mins
|
||||
final long messageTime = 150000; //2.5 mins
|
||||
final long employeeTime = 240000; //4 mins
|
||||
private final int numOfRetries = 3;
|
||||
private final long retryDuration = 30000;
|
||||
private final long queueTime = 240000; //4 mins
|
||||
private final long queueTaskTime = 60000; //1 min
|
||||
private final long paymentTime = 120000; //2 mins
|
||||
private final long messageTime = 150000; //2.5 mins
|
||||
private final long employeeTime = 240000; //4 mins
|
||||
|
||||
void paymentNotPossibleCase() throws Exception {
|
||||
PaymentService ps =
|
||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new PaymentDetailsErrorException());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
||||
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,
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new PaymentDetailsErrorException());
|
||||
var ss = new ShippingService(new ShippingDatabase());
|
||||
var ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException());
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
var qdb = new QueueDatabase(new DatabaseUnavailableException());
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
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());
|
||||
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,
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
var ss = new ShippingService(new ShippingDatabase());
|
||||
var ms = new MessagingService(new MessagingDatabase());
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
var qdb = new QueueDatabase();
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void paymentSuccessCase() throws Exception {
|
||||
//goes to message after 2 retries maybe - rest is successful for now
|
||||
PaymentService ps =
|
||||
new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
||||
MessagingService ms =
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
var ss = new ShippingService(new ShippingDatabase());
|
||||
var 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,
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
var qdb = new QueueDatabase(new DatabaseUnavailableException());
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
@ -106,7 +102,7 @@ public class AppPaymentFailCases {
|
||||
*/
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
AppPaymentFailCases apfc = new AppPaymentFailCases();
|
||||
var apfc = new AppPaymentFailCases();
|
||||
//apfc.paymentNotPossibleCase();
|
||||
//apfc.paymentDatabaseUnavailableCase();
|
||||
apfc.paymentSuccessCase();
|
||||
|
@ -40,98 +40,93 @@ import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
*/
|
||||
|
||||
public class AppQueueFailCases {
|
||||
final int numOfRetries = 3;
|
||||
final long retryDuration = 30000;
|
||||
final long queueTime = 240000; //4 mins
|
||||
final long queueTaskTime = 60000; //1 min
|
||||
final long paymentTime = 120000; //2 mins
|
||||
final long messageTime = 150000; //2.5 mins
|
||||
final long employeeTime = 240000; //4 mins
|
||||
private final int numOfRetries = 3;
|
||||
private final long retryDuration = 30000;
|
||||
private final long queueTime = 240000; //4 mins
|
||||
private final long queueTaskTime = 60000; //1 min
|
||||
private final long paymentTime = 120000; //2 mins
|
||||
private final long messageTime = 150000; //2.5 mins
|
||||
private 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());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
||||
MessagingService ms = new MessagingService(new MessagingDatabase());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb =
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
var ss = new ShippingService(new ShippingDatabase());
|
||||
var ms = new MessagingService(new MessagingDatabase());
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
var 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,
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
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 =
|
||||
var ps = new PaymentService(new PaymentDatabase());
|
||||
var ss = new ShippingService(new ShippingDatabase());
|
||||
var ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
var 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,
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void queueEmployeeDbTaskDatabaseUnavailableCase() throws Exception {
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
||||
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 =
|
||||
var ps = new PaymentService(new PaymentDatabase());
|
||||
var ss = new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
||||
var ms = new MessagingService(new MessagingDatabase());
|
||||
var 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());
|
||||
var 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,
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var 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());
|
||||
ShippingService ss = new ShippingService(new ShippingDatabase());
|
||||
MessagingService ms =
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
var ss = new ShippingService(new ShippingDatabase());
|
||||
var ms =
|
||||
new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
EmployeeHandle eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
QueueDatabase qdb =
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
var qdb =
|
||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
Commander c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
@ -142,7 +137,7 @@ public class AppQueueFailCases {
|
||||
*/
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
AppQueueFailCases aqfc = new AppQueueFailCases();
|
||||
var aqfc = new AppQueueFailCases();
|
||||
//aqfc.queuePaymentTaskDatabaseUnavailableCase();
|
||||
//aqfc.queueMessageTaskDatabaseUnavailableCase();
|
||||
//aqfc.queueEmployeeDbTaskDatabaseUnavailableCase();
|
||||
|
@ -42,75 +42,69 @@ import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
*/
|
||||
|
||||
public class AppShippingFailCases {
|
||||
final int numOfRetries = 3;
|
||||
final long retryDuration = 30000;
|
||||
final long queueTime = 240000; //4 mins
|
||||
final long queueTaskTime = 60000; //1 min
|
||||
final long paymentTime = 120000; //2 mins
|
||||
final long messageTime = 150000; //2.5 mins
|
||||
final long employeeTime = 240000; //4 mins
|
||||
private final int numOfRetries = 3;
|
||||
private final long retryDuration = 30000;
|
||||
private final long queueTime = 240000; //4 mins
|
||||
private final long queueTaskTime = 60000; //1 min
|
||||
private final long paymentTime = 120000; //2 mins
|
||||
private final long messageTime = 150000; //2.5 mins
|
||||
private final long employeeTime = 240000; //4 mins
|
||||
|
||||
void itemUnavailableCase() throws Exception {
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
||||
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,
|
||||
var ps = new PaymentService(new PaymentDatabase());
|
||||
var ss = new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
||||
var ms = new MessagingService(new MessagingDatabase());
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
var qdb = new QueueDatabase();
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void shippingNotPossibleCase() throws Exception {
|
||||
PaymentService ps = new PaymentService(new PaymentDatabase());
|
||||
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,
|
||||
var ps = new PaymentService(new PaymentDatabase());
|
||||
var ss = new ShippingService(new ShippingDatabase(), new ShippingNotPossibleException());
|
||||
var ms = new MessagingService(new MessagingDatabase());
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
var qdb = new QueueDatabase();
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
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());
|
||||
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,
|
||||
var ps = new PaymentService(new PaymentDatabase());
|
||||
var ss = new ShippingService(new ShippingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
var ms = new MessagingService(new MessagingDatabase());
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
var qdb = new QueueDatabase();
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
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(),
|
||||
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,
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException());
|
||||
var ss = new ShippingService(new ShippingDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
var ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException());
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
var qdb = new QueueDatabase();
|
||||
var 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);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
@ -121,7 +115,7 @@ public class AppShippingFailCases {
|
||||
*/
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
AppShippingFailCases asfc = new AppShippingFailCases();
|
||||
var asfc = new AppShippingFailCases();
|
||||
//asfc.itemUnavailableCase();
|
||||
//asfc.shippingNotPossibleCase();
|
||||
//asfc.shippingDatabaseUnavailableCase();
|
||||
|
@ -36,7 +36,6 @@ 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;
|
||||
|
||||
@ -86,7 +85,7 @@ public class Commander {
|
||||
private final long messageTime;
|
||||
private final long employeeTime;
|
||||
private boolean finalSiteMsgShown;
|
||||
static final Logger LOG = LoggerFactory.getLogger(Commander.class);
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Commander.class);
|
||||
//we could also have another db where it stores all orders
|
||||
|
||||
Commander(EmployeeHandle empDb, PaymentService paymentService, ShippingService shippingService,
|
||||
@ -113,7 +112,7 @@ public class Commander {
|
||||
}
|
||||
|
||||
private void sendShippingRequest(Order order) throws Exception {
|
||||
ArrayList<Exception> list = shippingService.exceptionsList;
|
||||
var list = shippingService.exceptionsList;
|
||||
Retry.Operation op = (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
@ -131,7 +130,6 @@ public class Commander {
|
||||
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) -> {
|
||||
if (ShippingNotPossibleException.class.isAssignableFrom(err.getClass())) {
|
||||
@ -153,9 +151,8 @@ public class Commander {
|
||||
LOG.error("Order " + order.id + ": Shipping service unavailable, order not placed..");
|
||||
finalSiteMsgShown = true;
|
||||
}
|
||||
return;
|
||||
};
|
||||
Retry<Order> r = new Retry<Order>(op, handleError, numOfRetries, retryDuration,
|
||||
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
r.perform(list, order);
|
||||
}
|
||||
@ -169,79 +166,69 @@ public class Commander {
|
||||
} //if succeeded or failed, would have been dequeued, no attempt to make payment
|
||||
return;
|
||||
}
|
||||
ArrayList<Exception> list = paymentService.exceptionsList;
|
||||
Thread t = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
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..");
|
||||
} else {
|
||||
LOG.debug("Order " + order.id + ": Error in creating payment request..");
|
||||
}
|
||||
throw l.remove(0);
|
||||
}
|
||||
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);
|
||||
if (!finalSiteMsgShown) {
|
||||
LOG.info("Payment made successfully, thank you for shopping with us!!");
|
||||
finalSiteMsgShown = true;
|
||||
}
|
||||
sendSuccessMessage(order);
|
||||
return;
|
||||
}
|
||||
};
|
||||
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. "
|
||||
+ "Meanwhile, your order has been converted to COD and will be shipped.");
|
||||
finalSiteMsgShown = true;
|
||||
}
|
||||
LOG.error("Order " + order.id + ": Payment details incorrect, failed..");
|
||||
o.paid = PaymentStatus.NotDone;
|
||||
sendPaymentFailureMessage(o);
|
||||
var list = paymentService.exceptionsList;
|
||||
var t = new Thread(() -> {
|
||||
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..");
|
||||
} else {
|
||||
try {
|
||||
if (o.messageSent.equals(MessageSent.NoneSent)) {
|
||||
if (!finalSiteMsgShown) {
|
||||
LOG.info("There was an error in payment. We are on it, and will get back to you "
|
||||
+ "asap. Don't worry, your order has been placed and will be shipped.");
|
||||
finalSiteMsgShown = true;
|
||||
}
|
||||
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);
|
||||
updateQueue(qt);
|
||||
}
|
||||
} catch (InterruptedException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
LOG.debug("Order " + order.id + ": Error in creating payment request..");
|
||||
}
|
||||
return;
|
||||
};
|
||||
Retry<Order> r = new Retry<Order>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, order);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (order.paid.equals(PaymentStatus.Trying)) {
|
||||
var transactionId = paymentService.receiveRequest(order.price);
|
||||
order.paid = PaymentStatus.Done;
|
||||
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;
|
||||
}
|
||||
sendSuccessMessage(order);
|
||||
}
|
||||
};
|
||||
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. "
|
||||
+ "Meanwhile, your order has been converted to COD and will be shipped.");
|
||||
finalSiteMsgShown = true;
|
||||
}
|
||||
LOG.error("Order " + order.id + ": Payment details incorrect, failed..");
|
||||
o.paid = PaymentStatus.NotDone;
|
||||
sendPaymentFailureMessage(o);
|
||||
} else {
|
||||
if (o.messageSent.equals(MessageSent.NoneSent)) {
|
||||
if (!finalSiteMsgShown) {
|
||||
LOG.info("There was an error in payment. We are on it, and will get back to you "
|
||||
+ "asap. Don't worry, your order has been placed and will be shipped.");
|
||||
finalSiteMsgShown = true;
|
||||
}
|
||||
LOG.warn("Order " + order.id + ": Payment error, going to queue..");
|
||||
sendPaymentPossibleErrorMsg(o);
|
||||
}
|
||||
if (o.paid.equals(PaymentStatus.Trying) && System
|
||||
.currentTimeMillis() - o.createdTime < paymentTime) {
|
||||
var qt = new QueueTask(o, TaskType.Payment, -1);
|
||||
updateQueue(qt);
|
||||
}
|
||||
}
|
||||
};
|
||||
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, order);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
}
|
||||
|
||||
private void updateQueue(QueueTask qt) throws InterruptedException {
|
||||
private void updateQueue(QueueTask qt) {
|
||||
if (System.currentTimeMillis() - qt.order.createdTime >= this.queueTime) {
|
||||
// since payment time is lesser than queuetime it would have already failed..
|
||||
// additional check not needed
|
||||
@ -256,97 +243,82 @@ public class Commander {
|
||||
LOG.trace("Order " + qt.order.id + ": Not queueing task since task already done..");
|
||||
return;
|
||||
}
|
||||
ArrayList<Exception> list = queue.exceptionsList;
|
||||
Thread t = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Retry.Operation op = (list) -> {
|
||||
if (!list.isEmpty()) {
|
||||
LOG.warn("Order " + qt.order.id + ": Error in connecting to queue db, trying again..");
|
||||
throw list.remove(0);
|
||||
}
|
||||
queue.add(qt);
|
||||
queueItems++;
|
||||
LOG.info("Order " + qt.order.id + ": " + qt.getType() + " task enqueued..");
|
||||
tryDoingTasksInQueue();
|
||||
return;
|
||||
};
|
||||
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 task of type " + qt.getType()
|
||||
+ ", trying to add to employee handle..");
|
||||
employeeHandleIssue(qt.order);
|
||||
return;
|
||||
};
|
||||
Retry<QueueTask> r = new Retry<QueueTask>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, qt);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
var list = queue.exceptionsList;
|
||||
Thread t = new Thread(() -> {
|
||||
Retry.Operation op = (list1) -> {
|
||||
if (!list1.isEmpty()) {
|
||||
LOG.warn("Order " + qt.order.id + ": Error in connecting to queue db, trying again..");
|
||||
throw list1.remove(0);
|
||||
}
|
||||
queue.add(qt);
|
||||
queueItems++;
|
||||
LOG.info("Order " + qt.order.id + ": " + qt.getType() + " task enqueued..");
|
||||
tryDoingTasksInQueue();
|
||||
};
|
||||
Retry.HandleErrorIssue<QueueTask> handleError = (qt1, err) -> {
|
||||
if (qt1.taskType.equals(TaskType.Payment)) {
|
||||
qt1.order.paid = PaymentStatus.NotDone;
|
||||
sendPaymentFailureMessage(qt1.order);
|
||||
LOG.error("Order " + qt1.order.id + ": Unable to enqueue payment task,"
|
||||
+ " payment failed..");
|
||||
}
|
||||
LOG.error("Order " + qt1.order.id + ": Unable to enqueue task of type " + qt1.getType()
|
||||
+ ", trying to add to employee handle..");
|
||||
employeeHandleIssue(qt1.order);
|
||||
};
|
||||
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, qt);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
}
|
||||
|
||||
private void tryDoingTasksInQueue() { //commander controls operations done to queue
|
||||
ArrayList<Exception> list = queue.exceptionsList;
|
||||
Thread t2 = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Retry.Operation op = (list) -> {
|
||||
if (!list.isEmpty()) {
|
||||
LOG.warn("Error in accessing queue db to do tasks, trying again..");
|
||||
throw list.remove(0);
|
||||
}
|
||||
doTasksInQueue();
|
||||
return;
|
||||
};
|
||||
Retry.HandleErrorIssue<QueueTask> handleError = (o, err) -> {
|
||||
return;
|
||||
};
|
||||
Retry<QueueTask> r = new Retry<QueueTask>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, null);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
var list = queue.exceptionsList;
|
||||
var t2 = new Thread(() -> {
|
||||
Retry.Operation op = (list1) -> {
|
||||
if (!list1.isEmpty()) {
|
||||
LOG.warn("Error in accessing queue db to do tasks, trying again..");
|
||||
throw list1.remove(0);
|
||||
}
|
||||
doTasksInQueue();
|
||||
};
|
||||
Retry.HandleErrorIssue<QueueTask> handleError = (o, err) -> {
|
||||
};
|
||||
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, null);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
});
|
||||
t2.start();
|
||||
}
|
||||
|
||||
private void tryDequeue() {
|
||||
ArrayList<Exception> list = queue.exceptionsList;
|
||||
Thread t3 = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Retry.Operation op = (list) -> {
|
||||
if (!list.isEmpty()) {
|
||||
LOG.warn("Error in accessing queue db to dequeue task, trying again..");
|
||||
throw list.remove(0);
|
||||
}
|
||||
queue.dequeue();
|
||||
queueItems--;
|
||||
return;
|
||||
};
|
||||
Retry.HandleErrorIssue<QueueTask> handleError = (o, err) -> {
|
||||
return;
|
||||
};
|
||||
Retry<QueueTask> r = new Retry<QueueTask>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, null);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
var list = queue.exceptionsList;
|
||||
var t3 = new Thread(() -> {
|
||||
Retry.Operation op = (list1) -> {
|
||||
if (!list1.isEmpty()) {
|
||||
LOG.warn("Error in accessing queue db to dequeue task, trying again..");
|
||||
throw list1.remove(0);
|
||||
}
|
||||
queue.dequeue();
|
||||
queueItems--;
|
||||
};
|
||||
Retry.HandleErrorIssue<QueueTask> handleError = (o, err) -> {
|
||||
};
|
||||
var r = new Retry<QueueTask>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, null);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
});
|
||||
t3.start();
|
||||
@ -357,53 +329,44 @@ public class Commander {
|
||||
LOG.trace("Order " + order.id + ": Message time for order over, returning..");
|
||||
return;
|
||||
}
|
||||
ArrayList<Exception> list = messagingService.exceptionsList;
|
||||
Thread t = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Retry.Operation op = (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
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..");
|
||||
}
|
||||
throw l.remove(0);
|
||||
var list = messagingService.exceptionsList;
|
||||
Thread t = new Thread(() -> {
|
||||
Retry.Operation op = (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
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..");
|
||||
}
|
||||
if (!order.messageSent.equals(MessageSent.PaymentFail)
|
||||
&& !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);
|
||||
}
|
||||
return;
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
try {
|
||||
if ((o.messageSent.equals(MessageSent.NoneSent) || o.messageSent
|
||||
.equals(MessageSent.PaymentTrying))
|
||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||
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..");
|
||||
employeeHandleIssue(order);
|
||||
}
|
||||
} catch (InterruptedException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
return;
|
||||
};
|
||||
Retry<Order> r = new Retry<Order>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, order);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (!order.messageSent.equals(MessageSent.PaymentFail)
|
||||
&& !order.messageSent.equals(MessageSent.PaymentSuccessful)) {
|
||||
var requestId = messagingService.receiveRequest(2);
|
||||
order.messageSent = MessageSent.PaymentSuccessful;
|
||||
LOG.info("Order " + order.id + ": Payment Success message sent,"
|
||||
+ " request Id: " + requestId);
|
||||
}
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
if ((o.messageSent.equals(MessageSent.NoneSent) || o.messageSent
|
||||
.equals(MessageSent.PaymentTrying))
|
||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||
var 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..");
|
||||
employeeHandleIssue(order);
|
||||
}
|
||||
};
|
||||
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, order);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
@ -414,53 +377,44 @@ public class Commander {
|
||||
LOG.trace("Order " + order.id + ": Message time for order over, returning..");
|
||||
return;
|
||||
}
|
||||
ArrayList<Exception> list = messagingService.exceptionsList;
|
||||
Thread t = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Retry.Operation op = (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
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..");
|
||||
}
|
||||
throw l.remove(0);
|
||||
var list = messagingService.exceptionsList;
|
||||
var t = new Thread(() -> {
|
||||
Retry.Operation op = (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
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..");
|
||||
}
|
||||
if (!order.messageSent.equals(MessageSent.PaymentFail)
|
||||
&& !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);
|
||||
}
|
||||
return;
|
||||
};
|
||||
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);
|
||||
updateQueue(qt);
|
||||
LOG.warn("Order " + order.id + ": Error in sending Payment Failure message, "
|
||||
+ "trying to queue task and add to employee handle..");
|
||||
employeeHandleIssue(o);
|
||||
} catch (InterruptedException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
return;
|
||||
}
|
||||
};
|
||||
Retry<Order> r = new Retry<Order>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, order);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (!order.messageSent.equals(MessageSent.PaymentFail)
|
||||
&& !order.messageSent.equals(MessageSent.PaymentSuccessful)) {
|
||||
var requestId = messagingService.receiveRequest(0);
|
||||
order.messageSent = MessageSent.PaymentFail;
|
||||
LOG.info("Order " + order.id + ": Payment Failure message sent successfully,"
|
||||
+ " request Id: " + requestId);
|
||||
}
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
if ((o.messageSent.equals(MessageSent.NoneSent) || o.messageSent
|
||||
.equals(MessageSent.PaymentTrying))
|
||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||
var 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..");
|
||||
employeeHandleIssue(o);
|
||||
}
|
||||
};
|
||||
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, order);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
@ -471,53 +425,44 @@ public class Commander {
|
||||
LOG.trace("Message time for order over, returning..");
|
||||
return;
|
||||
}
|
||||
ArrayList<Exception> list = messagingService.exceptionsList;
|
||||
Thread t = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Retry.Operation op = (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
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..");
|
||||
}
|
||||
throw l.remove(0);
|
||||
var list = messagingService.exceptionsList;
|
||||
var t = new Thread(() -> {
|
||||
Retry.Operation op = (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
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..");
|
||||
}
|
||||
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);
|
||||
}
|
||||
return;
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
try {
|
||||
if (o.messageSent.equals(MessageSent.NoneSent) && order.paid
|
||||
.equals(PaymentStatus.Trying)
|
||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||
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..");
|
||||
employeeHandleIssue(o);
|
||||
}
|
||||
} catch (InterruptedException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
return;
|
||||
};
|
||||
Retry<Order> r = new Retry<Order>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, order);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (order.paid.equals(PaymentStatus.Trying) && order.messageSent
|
||||
.equals(MessageSent.NoneSent)) {
|
||||
var requestId = messagingService.receiveRequest(1);
|
||||
order.messageSent = MessageSent.PaymentTrying;
|
||||
LOG.info("Order " + order.id + ": Payment Error message sent successfully,"
|
||||
+ " request Id: " + requestId);
|
||||
}
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
if (o.messageSent.equals(MessageSent.NoneSent) && order.paid
|
||||
.equals(PaymentStatus.Trying)
|
||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||
var 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..");
|
||||
employeeHandleIssue(o);
|
||||
}
|
||||
};
|
||||
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, order);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
@ -528,45 +473,35 @@ public class Commander {
|
||||
LOG.trace("Order " + order.id + ": Employee handle time for order over, returning..");
|
||||
return;
|
||||
}
|
||||
ArrayList<Exception> list = employeeDb.exceptionsList;
|
||||
Thread t = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Retry.Operation op = (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
LOG.warn("Order " + order.id + ": Error in connecting to employee handle,"
|
||||
+ " trying again..");
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (!order.addedToEmployeeHandle) {
|
||||
employeeDb.receiveRequest(order);
|
||||
order.addedToEmployeeHandle = true;
|
||||
LOG.info("Order " + order.id + ": Added order to employee database");
|
||||
}
|
||||
return;
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
try {
|
||||
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..");
|
||||
return;
|
||||
}
|
||||
} catch (InterruptedException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
return;
|
||||
};
|
||||
Retry<Order> r = new Retry<Order>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, order);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
var list = employeeDb.exceptionsList;
|
||||
var t = new Thread(() -> {
|
||||
Retry.Operation op = (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
LOG.warn("Order " + order.id + ": Error in connecting to employee handle,"
|
||||
+ " trying again..");
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (!order.addedToEmployeeHandle) {
|
||||
employeeDb.receiveRequest(order);
|
||||
order.addedToEmployeeHandle = true;
|
||||
LOG.info("Order " + order.id + ": Added order to employee database");
|
||||
}
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
if (!o.addedToEmployeeHandle && System
|
||||
.currentTimeMillis() - order.createdTime < employeeTime) {
|
||||
var qt = new QueueTask(order, TaskType.EmployeeDb, -1);
|
||||
updateQueue(qt);
|
||||
LOG.warn("Order " + order.id + ": Error in adding to employee db,"
|
||||
+ " trying to queue task..");
|
||||
}
|
||||
};
|
||||
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, order);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
@ -574,7 +509,7 @@ public class Commander {
|
||||
|
||||
private void doTasksInQueue() throws Exception {
|
||||
if (queueItems != 0) {
|
||||
QueueTask qt = queue.peek(); //this should probably be cloned here
|
||||
var qt = queue.peek(); //this should probably be cloned here
|
||||
//this is why we have retry for doTasksInQueue
|
||||
LOG.trace("Order " + qt.order.id + ": Started doing task of type " + qt.getType());
|
||||
if (qt.firstAttemptTime == -1) {
|
||||
|
@ -36,14 +36,10 @@ public class Order { //can store all transactions ids also
|
||||
NotDone, Trying, Done
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
enum MessageSent {
|
||||
NoneSent, PaymentFail, PaymentTrying, PaymentSuccessful
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
final User user;
|
||||
final String item;
|
||||
public final String id;
|
||||
@ -74,7 +70,7 @@ public class Order { //can store all transactions ids also
|
||||
this.addedToEmployeeHandle = false;
|
||||
}
|
||||
|
||||
String createUniqueId() {
|
||||
private String createUniqueId() {
|
||||
StringBuilder random = new StringBuilder();
|
||||
while (random.length() < 12) { // length of the random string.
|
||||
int index = (int) (RANDOM.nextFloat() * ALL_CHARS.length());
|
||||
|
@ -25,6 +25,7 @@ package com.iluwatar.commander;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Predicate;
|
||||
@ -42,7 +43,7 @@ public class Retry<T> {
|
||||
*/
|
||||
|
||||
public interface Operation {
|
||||
void operation(ArrayList<Exception> list) throws Exception;
|
||||
void operation(List<Exception> list) throws Exception;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -63,9 +64,9 @@ public class Retry<T> {
|
||||
private final long maxDelay;
|
||||
private final AtomicInteger attempts;
|
||||
private final Predicate<Exception> test;
|
||||
private final ArrayList<Exception> errors;
|
||||
private final List<Exception> errors;
|
||||
|
||||
Retry(Operation op, HandleErrorIssue handleError, int maxAttempts,
|
||||
Retry(Operation op, HandleErrorIssue<T> handleError, int maxAttempts,
|
||||
long maxDelay, Predicate<Exception>... ignoreTests) {
|
||||
this.op = op;
|
||||
this.handleError = handleError;
|
||||
@ -83,7 +84,7 @@ public class Retry<T> {
|
||||
* @param obj is the parameter to be passed into handleIsuue method
|
||||
*/
|
||||
|
||||
public void perform(ArrayList<Exception> list, T obj) throws Exception {
|
||||
public void perform(List<Exception> list, T obj) {
|
||||
do {
|
||||
try {
|
||||
op.operation(list);
|
||||
@ -97,7 +98,7 @@ public class Retry<T> {
|
||||
try {
|
||||
long testDelay =
|
||||
(long) Math.pow(2, this.attempts.intValue()) * 1000 + RANDOM.nextInt(1000);
|
||||
long delay = testDelay < this.maxDelay ? testDelay : maxDelay;
|
||||
long delay = Math.min(testDelay, this.maxDelay);
|
||||
Thread.sleep(delay);
|
||||
} catch (InterruptedException f) {
|
||||
//ignore
|
||||
|
@ -36,7 +36,7 @@ public class EmployeeDatabase extends Database<Order> {
|
||||
private Hashtable<String, Order> data;
|
||||
|
||||
public EmployeeDatabase() {
|
||||
this.data = new Hashtable<String, Order>();
|
||||
this.data = new Hashtable<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -28,8 +28,8 @@ 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 com.iluwatar.commander.Commander} and
|
||||
* {@link EmployeeDatabase}.
|
||||
*/
|
||||
|
||||
public class EmployeeHandle extends Service {
|
||||
@ -39,11 +39,11 @@ public class EmployeeHandle extends Service {
|
||||
}
|
||||
|
||||
public String receiveRequest(Object... parameters) throws DatabaseUnavailableException {
|
||||
return updateDb((Order) parameters[0]);
|
||||
return updateDb(parameters[0]);
|
||||
}
|
||||
|
||||
protected String updateDb(Object... parameters) throws DatabaseUnavailableException {
|
||||
Order o = (Order) parameters[0];
|
||||
var o = (Order) parameters[0];
|
||||
if (database.get(o.id) == null) {
|
||||
database.add(o);
|
||||
return o.id; //true rcvd - change addedToEmployeeHandle to true else dont do anything
|
||||
|
@ -36,16 +36,16 @@ public class MessagingDatabase extends Database<MessageRequest> {
|
||||
private Hashtable<String, MessageRequest> data;
|
||||
|
||||
public MessagingDatabase() {
|
||||
this.data = new Hashtable<String, MessageRequest>();
|
||||
this.data = new Hashtable<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageRequest add(MessageRequest r) throws DatabaseUnavailableException {
|
||||
public MessageRequest add(MessageRequest r) {
|
||||
return data.put(r.reqId, r);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageRequest get(String requestId) throws DatabaseUnavailableException {
|
||||
public MessageRequest get(String requestId) {
|
||||
return data.get(requestId);
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,7 @@ 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}.
|
||||
* is added to the {@link com.iluwatar.commander.employeehandle.EmployeeDatabase}.
|
||||
*/
|
||||
|
||||
public class MessagingService extends Service {
|
||||
@ -41,8 +41,6 @@ public class MessagingService extends Service {
|
||||
PaymentFail, PaymentTrying, PaymentSuccessful
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
class MessageRequest {
|
||||
String reqId;
|
||||
MessageToSend msg;
|
||||
@ -58,12 +56,12 @@ public class MessagingService extends Service {
|
||||
}
|
||||
|
||||
/**
|
||||
* Public method which will receive request from {@link Commander}.
|
||||
* Public method which will receive request from {@link com.iluwatar.commander.Commander}.
|
||||
*/
|
||||
public String receiveRequest(Object... parameters) throws DatabaseUnavailableException {
|
||||
int messageToSend = (int) parameters[0];
|
||||
String id = generateId();
|
||||
MessageToSend msg = null;
|
||||
var messageToSend = (int) parameters[0];
|
||||
var id = generateId();
|
||||
MessageToSend msg;
|
||||
if (messageToSend == 0) {
|
||||
msg = MessageToSend.PaymentFail;
|
||||
} else if (messageToSend == 1) {
|
||||
@ -71,12 +69,12 @@ public class MessagingService extends Service {
|
||||
} else { //messageToSend == 2
|
||||
msg = MessageToSend.PaymentSuccessful;
|
||||
}
|
||||
MessageRequest req = new MessageRequest(id, msg);
|
||||
var req = new MessageRequest(id, msg);
|
||||
return updateDb(req);
|
||||
}
|
||||
|
||||
protected String updateDb(Object... parameters) throws DatabaseUnavailableException {
|
||||
MessageRequest req = (MessageRequest) parameters[0];
|
||||
var req = (MessageRequest) parameters[0];
|
||||
if (this.database.get(req.reqId) == null) { //idempotence, in case db fails here
|
||||
database.add(req); //if successful:
|
||||
LOGGER.info(sendMessage(req.msg));
|
||||
|
@ -37,17 +37,17 @@ public class PaymentDatabase extends Database<PaymentRequest> {
|
||||
private Hashtable<String, PaymentRequest> data;
|
||||
|
||||
public PaymentDatabase() {
|
||||
this.data = new Hashtable<String, PaymentRequest>();
|
||||
this.data = new Hashtable<>();
|
||||
//0-fail, 1-error, 2-success
|
||||
}
|
||||
|
||||
@Override
|
||||
public PaymentRequest add(PaymentRequest r) throws DatabaseUnavailableException {
|
||||
public PaymentRequest add(PaymentRequest r) {
|
||||
return data.put(r.transactionId, r);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PaymentRequest get(String requestId) throws DatabaseUnavailableException {
|
||||
public PaymentRequest get(String requestId) {
|
||||
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 com.iluwatar.commander.Commander} and
|
||||
* adds to the {@link PaymentDatabase}.
|
||||
*/
|
||||
|
||||
public class PaymentService extends Service {
|
||||
@ -50,18 +50,18 @@ public class PaymentService extends Service {
|
||||
}
|
||||
|
||||
/**
|
||||
* Public method which will receive request from {@link Commander}.
|
||||
* Public method which will receive request from {@link com.iluwatar.commander.Commander}.
|
||||
*/
|
||||
|
||||
public String receiveRequest(Object... parameters) throws DatabaseUnavailableException {
|
||||
//it could also be sending a userid, payment details here or something, not added here
|
||||
String id = generateId();
|
||||
PaymentRequest req = new PaymentRequest(id, (float) parameters[0]);
|
||||
var id = generateId();
|
||||
var req = new PaymentRequest(id, (float) parameters[0]);
|
||||
return updateDb(req);
|
||||
}
|
||||
|
||||
protected String updateDb(Object... parameters) throws DatabaseUnavailableException {
|
||||
PaymentRequest req = (PaymentRequest) parameters[0];
|
||||
var req = (PaymentRequest) parameters[0];
|
||||
if (database.get(req.transactionId) == null || !req.paid) {
|
||||
database.add(req);
|
||||
req.paid = true;
|
||||
|
@ -33,15 +33,15 @@ import com.iluwatar.commander.exceptions.IsEmptyException;
|
||||
|
||||
public class Queue<T> {
|
||||
|
||||
Node<T> front;
|
||||
Node<T> rear;
|
||||
public int size = 0;
|
||||
private Node<T> front;
|
||||
private Node<T> rear;
|
||||
private int size;
|
||||
|
||||
class Node<T> {
|
||||
T value;
|
||||
Node<T> next;
|
||||
static class Node<V> {
|
||||
V value;
|
||||
Node<V> next;
|
||||
|
||||
Node(T obj, Node<T> b) {
|
||||
Node(V obj, Node<V> b) {
|
||||
value = obj;
|
||||
next = b;
|
||||
}
|
||||
@ -57,19 +57,15 @@ public class Queue<T> {
|
||||
}
|
||||
|
||||
boolean isEmpty() {
|
||||
if (size == 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
void enqueue(T obj) {
|
||||
if (front == null) {
|
||||
front = new Node(obj, null);
|
||||
front = new Node<>(obj, null);
|
||||
rear = front;
|
||||
} else {
|
||||
Node temp = new Node(obj, null);
|
||||
var temp = new Node<>(obj, null);
|
||||
rear.next = temp;
|
||||
rear = temp;
|
||||
}
|
||||
@ -80,10 +76,10 @@ public class Queue<T> {
|
||||
if (isEmpty()) {
|
||||
throw new IsEmptyException();
|
||||
} else {
|
||||
Node temp = front;
|
||||
var temp = front;
|
||||
front = front.next;
|
||||
size = size - 1;
|
||||
return (T) temp.value;
|
||||
return temp.value;
|
||||
}
|
||||
}
|
||||
|
||||
@ -91,7 +87,7 @@ public class Queue<T> {
|
||||
if (isEmpty()) {
|
||||
throw new IsEmptyException();
|
||||
} else {
|
||||
return (T) front.value;
|
||||
return front.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ import java.util.List;
|
||||
public class QueueDatabase extends Database<QueueTask> {
|
||||
|
||||
private Queue<QueueTask> data;
|
||||
public ArrayList<Exception> exceptionsList;
|
||||
public List<Exception> exceptionsList;
|
||||
|
||||
public QueueDatabase(Exception... exc) {
|
||||
this.data = new Queue<>();
|
||||
@ -44,7 +44,7 @@ public class QueueDatabase extends Database<QueueTask> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueueTask add(QueueTask t) throws DatabaseUnavailableException {
|
||||
public QueueTask add(QueueTask t) {
|
||||
data.enqueue(t);
|
||||
return t;
|
||||
//even if same thing queued twice, it is taken care of in other dbs
|
||||
@ -55,12 +55,10 @@ public class QueueDatabase extends Database<QueueTask> {
|
||||
*
|
||||
* @return object at front of queue
|
||||
* @throws IsEmptyException if queue is empty
|
||||
* @throws DatabaseUnavailableException if queue db is unavailable
|
||||
*/
|
||||
|
||||
public QueueTask peek() throws IsEmptyException, DatabaseUnavailableException {
|
||||
QueueTask qt = this.data.peek();
|
||||
return qt;
|
||||
public QueueTask peek() throws IsEmptyException {
|
||||
return this.data.peek();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -68,16 +66,14 @@ public class QueueDatabase extends Database<QueueTask> {
|
||||
*
|
||||
* @return object at front of queue
|
||||
* @throws IsEmptyException if queue is empty
|
||||
* @throws DatabaseUnavailableException if queue db is unavailable
|
||||
*/
|
||||
|
||||
public QueueTask dequeue() throws IsEmptyException, DatabaseUnavailableException {
|
||||
QueueTask qt = this.data.dequeue();
|
||||
return qt;
|
||||
public QueueTask dequeue() throws IsEmptyException {
|
||||
return this.data.dequeue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueueTask get(String taskId) throws DatabaseUnavailableException {
|
||||
public QueueTask get(String taskId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -39,8 +39,6 @@ public class QueueTask {
|
||||
Messaging, Payment, EmployeeDb
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
public Order order;
|
||||
public TaskType taskType;
|
||||
public int messageType; //0-fail, 1-error, 2-success
|
||||
@ -69,7 +67,6 @@ public class QueueTask {
|
||||
*
|
||||
* @return String representing type of task
|
||||
*/
|
||||
|
||||
public String getType() {
|
||||
if (!this.taskType.equals(TaskType.Messaging)) {
|
||||
return this.taskType.toString();
|
||||
|
@ -37,15 +37,15 @@ public class ShippingDatabase extends Database<ShippingRequest> {
|
||||
private Hashtable<String, ShippingRequest> data;
|
||||
|
||||
public ShippingDatabase() {
|
||||
this.data = new Hashtable<String, ShippingRequest>();
|
||||
this.data = new Hashtable<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ShippingRequest add(ShippingRequest r) throws DatabaseUnavailableException {
|
||||
public ShippingRequest add(ShippingRequest r) {
|
||||
return data.put(r.transactionId, r);
|
||||
}
|
||||
|
||||
public ShippingRequest get(String trasnactionId) throws DatabaseUnavailableException {
|
||||
public ShippingRequest get(String trasnactionId) {
|
||||
return data.get(trasnactionId);
|
||||
}
|
||||
|
||||
|
@ -27,13 +27,13 @@ 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 com.iluwatar.commander.Commander} class and
|
||||
* adds it to the {@link ShippingDatabase}.
|
||||
*/
|
||||
|
||||
public class ShippingService extends Service {
|
||||
|
||||
class ShippingRequest {
|
||||
static class ShippingRequest {
|
||||
String transactionId;
|
||||
String item;
|
||||
String address;
|
||||
@ -50,18 +50,19 @@ public class ShippingService extends Service {
|
||||
}
|
||||
|
||||
/**
|
||||
* Public method which will receive request from {@link Commander}.
|
||||
* Public method which will receive request from {@link com.iluwatar.commander.Commander}.
|
||||
*/
|
||||
|
||||
public String receiveRequest(Object... parameters) throws DatabaseUnavailableException {
|
||||
String id = generateId();
|
||||
ShippingRequest req =
|
||||
new ShippingRequest(id, (String) parameters[0] /*item*/, (String) parameters[1]/*address*/);
|
||||
var id = generateId();
|
||||
var item = (String) parameters[0];
|
||||
var address = (String) parameters[1];
|
||||
var req = new ShippingRequest(id, item, address);
|
||||
return updateDb(req);
|
||||
}
|
||||
|
||||
protected String updateDb(Object... parameters) throws DatabaseUnavailableException {
|
||||
ShippingRequest req = (ShippingRequest) parameters[0];
|
||||
var req = (ShippingRequest) parameters[0];
|
||||
if (this.database.get(req.transactionId) == null) {
|
||||
database.add(req);
|
||||
return req.transactionId;
|
||||
|
@ -23,43 +23,38 @@
|
||||
|
||||
package com.iluwatar.commander;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||
import com.iluwatar.commander.exceptions.ItemUnavailableException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class RetryTest {
|
||||
|
||||
@Test
|
||||
void performTest() {
|
||||
Retry.Operation op = (l) -> {
|
||||
Retry.Operation op = (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
throw l.remove(0);
|
||||
}
|
||||
return;
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o,e) -> {
|
||||
return;
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, e) -> {
|
||||
};
|
||||
Retry<Order> r1 = new Retry<>(op, handleError, 3, 30000,
|
||||
var r1 = new Retry<>(op, handleError, 3, 30000,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
Retry<Order> r2 = new Retry<>(op, handleError, 3, 30000,
|
||||
var r2 = new Retry<>(op, handleError, 3, 30000,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
User user = new User("Jim", "ABCD");
|
||||
Order order = new Order(user, "book", 10f);
|
||||
ArrayList<Exception> arr1 = new ArrayList<>(List.of(
|
||||
new ItemUnavailableException(), new DatabaseUnavailableException()));
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
var arr1 = new ArrayList<>(List.of(new ItemUnavailableException(), new DatabaseUnavailableException()));
|
||||
try {
|
||||
r1.perform(arr1, order);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
ArrayList<Exception> arr2 = new ArrayList<>(List.of(
|
||||
new DatabaseUnavailableException(), new ItemUnavailableException()));
|
||||
var arr2 = new ArrayList<>(List.of(new DatabaseUnavailableException(), new ItemUnavailableException()));
|
||||
try {
|
||||
r2.perform(arr2, order);
|
||||
} catch (Exception e1) {
|
||||
|
Reference in New Issue
Block a user