mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-16 08:58:49 +00:00
fix: Fix sonar issues (#2925)
* Remove unused member which was also causing a false positive sonar issue. Fixes sonar issue https://sonarcloud.io/project/issues?open=AY3gHwu5DIZTZkppqVEG&id=iluwatar_java-design-patterns * Fixes sonar issue https://sonarcloud.io/project/issues?open=AXK0OzDA-CiGJS70dLki&id=iluwatar_java-design-patterns related to "Refactor the code of the lambda to not have multiple invocations throwing the same checked exception." Also, updated the code to use Instant and Duration to deal with time instead of int. Added the awaitility library to perform assertions in test which is more reliable than using Thread.sleep directly to wait for events to happen. * checkstyle fix * Add sneaky throws to fix sonar lint issue. This is fine as the newFile method is not being tested but instead the new SimpleFileWriter(...) is. * The first booking needs to happen outside the assertions. Fixed other warnings * Use records to pass around related objects instead of using a large number of individual params, which sonar did not like. * Checkstyle fixes * checkstyle fixes * Remove complexity to keep sonar happy. * Split into different methods to reduce complexity. Could be broken down even further but currently Sonar is happy. * Move files to correct package * Add valid assertions to tests * rename constants to avoid confusion * Sonar warning related to cognitive complexity can be suppressed as the methods are quite generic and not functional enough to be separated out. * Use constants to keep Sonar happy * Use correct constant naming conventions * Use correct constant naming conventions * Use lombok to define noargsconstructor * Use a single method to do the logging * Remove unused constructor and redundant method * Use a reusable method for logging
This commit is contained in:
@@ -41,15 +41,11 @@ import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
* available/unavailable.
|
||||
*/
|
||||
public class AppEmployeeDbFailCases {
|
||||
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
|
||||
private static final RetryParams retryParams = RetryParams.DEFAULT;
|
||||
|
||||
void employeeDatabaseUnavailableCase() throws Exception {
|
||||
private static final TimeLimits timeLimits = TimeLimits.DEFAULT;
|
||||
|
||||
void employeeDatabaseUnavailableCase() {
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
@@ -64,22 +60,20 @@ public class AppEmployeeDbFailCases {
|
||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void employeeDbSuccessCase() throws Exception {
|
||||
void employeeDbSuccessCase() {
|
||||
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);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
@@ -91,9 +85,8 @@ public class AppEmployeeDbFailCases {
|
||||
* @param args command line args
|
||||
*/
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
public static void main(String[] args) {
|
||||
var aefc = new AppEmployeeDbFailCases();
|
||||
//aefc.employeeDatabaseUnavailableCase();
|
||||
aefc.employeeDbSuccessCase();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,15 +41,12 @@ import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
*/
|
||||
|
||||
public class AppMessagingFailCases {
|
||||
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
|
||||
private static final RetryParams retryParams = RetryParams.DEFAULT;
|
||||
|
||||
void messagingDatabaseUnavailableCasePaymentSuccess() throws Exception {
|
||||
private static final TimeLimits timeLimits = TimeLimits.DEFAULT;
|
||||
|
||||
|
||||
void messagingDatabaseUnavailableCasePaymentSuccess() {
|
||||
//rest is successful
|
||||
var ps = new PaymentService(new PaymentDatabase());
|
||||
var ss = new ShippingService(new ShippingDatabase());
|
||||
@@ -59,14 +56,13 @@ public class AppMessagingFailCases {
|
||||
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);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void messagingDatabaseUnavailableCasePaymentError() throws Exception {
|
||||
void messagingDatabaseUnavailableCasePaymentError() {
|
||||
//rest is successful
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
@@ -84,14 +80,13 @@ public class AppMessagingFailCases {
|
||||
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);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void messagingDatabaseUnavailableCasePaymentFailure() throws Exception {
|
||||
void messagingDatabaseUnavailableCasePaymentFailure() {
|
||||
//rest is successful
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
@@ -107,15 +102,13 @@ public class AppMessagingFailCases {
|
||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
var c =
|
||||
new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration, queueTime, queueTaskTime,
|
||||
paymentTime, messageTime, employeeTime);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void messagingSuccessCase() throws Exception {
|
||||
void messagingSuccessCase() {
|
||||
//done here
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
@@ -126,8 +119,7 @@ public class AppMessagingFailCases {
|
||||
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);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
@@ -139,11 +131,8 @@ public class AppMessagingFailCases {
|
||||
* @param args command line args
|
||||
*/
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
public static void main(String[] args) {
|
||||
var amfc = new AppMessagingFailCases();
|
||||
//amfc.messagingDatabaseUnavailableCasePaymentSuccess();
|
||||
//amfc.messagingDatabaseUnavailableCasePaymentError();
|
||||
//amfc.messagingDatabaseUnavailableCasePaymentFailure();
|
||||
amfc.messagingSuccessCase();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,29 +41,24 @@ import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
*/
|
||||
|
||||
public class AppPaymentFailCases {
|
||||
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
|
||||
private static final RetryParams retryParams = RetryParams.DEFAULT;
|
||||
|
||||
void paymentNotPossibleCase() throws Exception {
|
||||
private static final TimeLimits timeLimits = TimeLimits.DEFAULT;
|
||||
|
||||
void paymentNotPossibleCase() {
|
||||
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);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void paymentDatabaseUnavailableCase() throws Exception {
|
||||
void paymentDatabaseUnavailableCase() {
|
||||
//rest is successful
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
@@ -73,14 +68,13 @@ public class AppPaymentFailCases {
|
||||
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);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void paymentSuccessCase() throws Exception {
|
||||
void paymentSuccessCase() {
|
||||
//goes to message after 2 retries maybe - rest is successful for now
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException());
|
||||
@@ -89,8 +83,7 @@ public class AppPaymentFailCases {
|
||||
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);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
@@ -102,10 +95,8 @@ public class AppPaymentFailCases {
|
||||
* @param args command line args
|
||||
*/
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
public static void main(String[] args) {
|
||||
var apfc = new AppPaymentFailCases();
|
||||
//apfc.paymentNotPossibleCase();
|
||||
//apfc.paymentDatabaseUnavailableCase();
|
||||
apfc.paymentSuccessCase();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,15 +41,12 @@ import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
*/
|
||||
|
||||
public class AppQueueFailCases {
|
||||
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
|
||||
private static final RetryParams retryParams = RetryParams.DEFAULT;
|
||||
|
||||
void queuePaymentTaskDatabaseUnavailableCase() throws Exception {
|
||||
private static final TimeLimits timeLimits = TimeLimits.DEFAULT;
|
||||
|
||||
|
||||
void queuePaymentTaskDatabaseUnavailableCase() {
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
@@ -61,14 +58,13 @@ public class AppQueueFailCases {
|
||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void queueMessageTaskDatabaseUnavailableCase() throws Exception {
|
||||
void queueMessageTaskDatabaseUnavailableCase() {
|
||||
var ps = new PaymentService(new PaymentDatabase());
|
||||
var ss = new ShippingService(new ShippingDatabase());
|
||||
var ms = new MessagingService(new MessagingDatabase(), new DatabaseUnavailableException(),
|
||||
@@ -80,14 +76,13 @@ public class AppQueueFailCases {
|
||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void queueEmployeeDbTaskDatabaseUnavailableCase() throws Exception {
|
||||
void queueEmployeeDbTaskDatabaseUnavailableCase() {
|
||||
var ps = new PaymentService(new PaymentDatabase());
|
||||
var ss = new ShippingService(new ShippingDatabase(), new ItemUnavailableException());
|
||||
var ms = new MessagingService(new MessagingDatabase());
|
||||
@@ -105,14 +100,13 @@ public class AppQueueFailCases {
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void queueSuccessCase() throws Exception {
|
||||
void queueSuccessCase() {
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
new DatabaseUnavailableException(), new DatabaseUnavailableException(),
|
||||
@@ -124,8 +118,7 @@ public class AppQueueFailCases {
|
||||
var eh = new EmployeeHandle(new EmployeeDatabase());
|
||||
var qdb =
|
||||
new QueueDatabase(new DatabaseUnavailableException(), new DatabaseUnavailableException());
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, numOfRetries, retryDuration,
|
||||
queueTime, queueTaskTime, paymentTime, messageTime, employeeTime);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
@@ -137,11 +130,8 @@ public class AppQueueFailCases {
|
||||
* @param args command line args
|
||||
*/
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
public static void main(String[] args) {
|
||||
var aqfc = new AppQueueFailCases();
|
||||
//aqfc.queuePaymentTaskDatabaseUnavailableCase();
|
||||
//aqfc.queueMessageTaskDatabaseUnavailableCase();
|
||||
//aqfc.queueEmployeeDbTaskDatabaseUnavailableCase();
|
||||
aqfc.queueSuccessCase();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,41 +43,35 @@ import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
*/
|
||||
|
||||
public class AppShippingFailCases {
|
||||
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
|
||||
private static final RetryParams retryParams = RetryParams.DEFAULT;
|
||||
|
||||
void itemUnavailableCase() throws Exception {
|
||||
private static final TimeLimits timeLimits = TimeLimits.DEFAULT;
|
||||
|
||||
void itemUnavailableCase() {
|
||||
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);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void shippingNotPossibleCase() throws Exception {
|
||||
void shippingNotPossibleCase() {
|
||||
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);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void shippingDatabaseUnavailableCase() throws Exception {
|
||||
void shippingDatabaseUnavailableCase() {
|
||||
//rest is successful
|
||||
var ps = new PaymentService(new PaymentDatabase());
|
||||
var ss = new ShippingService(new ShippingDatabase(), new DatabaseUnavailableException(),
|
||||
@@ -87,14 +81,13 @@ public class AppShippingFailCases {
|
||||
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);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
}
|
||||
|
||||
void shippingSuccessCase() throws Exception {
|
||||
void shippingSuccessCase() {
|
||||
//goes to payment after 2 retries maybe - rest is successful for now
|
||||
var ps = new PaymentService(new PaymentDatabase(), new DatabaseUnavailableException());
|
||||
var ss = new ShippingService(new ShippingDatabase(), new DatabaseUnavailableException(),
|
||||
@@ -102,8 +95,7 @@ public class AppShippingFailCases {
|
||||
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);
|
||||
var c = new Commander(eh, ps, ss, ms, qdb, retryParams, timeLimits);
|
||||
var user = new User("Jim", "ABCD");
|
||||
var order = new Order(user, "book", 10f);
|
||||
c.placeOrder(order);
|
||||
@@ -115,11 +107,8 @@ public class AppShippingFailCases {
|
||||
* @param args command line args
|
||||
*/
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
public static void main(String[] args) {
|
||||
var asfc = new AppShippingFailCases();
|
||||
//asfc.itemUnavailableCase();
|
||||
//asfc.shippingNotPossibleCase();
|
||||
//asfc.shippingDatabaseUnavailableCase();
|
||||
asfc.shippingSuccessCase();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ 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.IsEmptyException;
|
||||
import com.iluwatar.commander.exceptions.ItemUnavailableException;
|
||||
import com.iluwatar.commander.exceptions.PaymentDetailsErrorException;
|
||||
import com.iluwatar.commander.exceptions.ShippingNotPossibleException;
|
||||
@@ -88,6 +89,7 @@ public class Commander {
|
||||
private final long messageTime;
|
||||
private final long employeeTime;
|
||||
private boolean finalSiteMsgShown;
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Commander.class);
|
||||
//we could also have another db where it stores all orders
|
||||
|
||||
@@ -98,32 +100,32 @@ public class Commander {
|
||||
private static final String TRY_CONNECTING_MSG_SVC =
|
||||
": Trying to connect to messaging service..";
|
||||
|
||||
private static final String DEFAULT_EXCEPTION_MESSAGE = "An exception occurred";
|
||||
|
||||
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) {
|
||||
MessagingService messagingService, QueueDatabase qdb, RetryParams retryParams, TimeLimits timeLimits) {
|
||||
this.paymentService = paymentService;
|
||||
this.shippingService = shippingService;
|
||||
this.messagingService = messagingService;
|
||||
this.employeeDb = empDb;
|
||||
this.queue = qdb;
|
||||
this.numOfRetries = numOfRetries;
|
||||
this.retryDuration = retryDuration;
|
||||
this.queueTime = queueTime;
|
||||
this.queueTaskTime = queueTaskTime;
|
||||
this.paymentTime = paymentTime;
|
||||
this.messageTime = messageTime;
|
||||
this.employeeTime = employeeTime;
|
||||
this.numOfRetries = retryParams.numOfRetries();
|
||||
this.retryDuration = retryParams.retryDuration();
|
||||
this.queueTime = timeLimits.queueTime();
|
||||
this.queueTaskTime = timeLimits.queueTaskTime();
|
||||
this.paymentTime = timeLimits.paymentTime();
|
||||
this.messageTime = timeLimits.messageTime();
|
||||
this.employeeTime = timeLimits.employeeTime();
|
||||
this.finalSiteMsgShown = false;
|
||||
}
|
||||
|
||||
void placeOrder(Order order) throws Exception {
|
||||
void placeOrder(Order order) {
|
||||
sendShippingRequest(order);
|
||||
}
|
||||
|
||||
private void sendShippingRequest(Order order) {
|
||||
var list = shippingService.exceptionsList;
|
||||
Retry.Operation op = (l) -> {
|
||||
Retry.Operation op = l -> {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
LOG.debug(ORDER_ID + ": Error in connecting to shipping service, "
|
||||
@@ -178,67 +180,86 @@ public class Commander {
|
||||
}
|
||||
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_ID + ": Error in connecting to payment service,"
|
||||
+ " trying again..", order.id);
|
||||
} else {
|
||||
LOG.debug(ORDER_ID + ": Error in creating payment request..", order.id);
|
||||
}
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (order.paid.equals(PaymentStatus.TRYING)) {
|
||||
var transactionId = paymentService.receiveRequest(order.price);
|
||||
order.paid = PaymentStatus.DONE;
|
||||
LOG.info(ORDER_ID + ": Payment successful, transaction Id: {}",
|
||||
order.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_ID + ": Payment details incorrect, failed..", order.id);
|
||||
o.paid = PaymentStatus.NOT_DONE;
|
||||
sendPaymentFailureMessage(o);
|
||||
} else {
|
||||
if (o.messageSent.equals(MessageSent.NONE_SENT)) {
|
||||
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_ID + ": Payment error, going to queue..", order.id);
|
||||
sendPaymentPossibleErrorMsg(o);
|
||||
}
|
||||
if (o.paid.equals(PaymentStatus.TRYING) && System
|
||||
.currentTimeMillis() - o.createdTime < paymentTime) {
|
||||
var qt = new QueueTask(o, TaskType.PAYMENT, -1);
|
||||
updateQueue(qt);
|
||||
}
|
||||
}
|
||||
};
|
||||
Retry.Operation op = getRetryOperation(order);
|
||||
|
||||
Retry.HandleErrorIssue<Order> handleError = getRetryHandleErrorIssue(order);
|
||||
|
||||
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, order);
|
||||
} catch (Exception e1) {
|
||||
LOG.error("An exception occurred", e1);
|
||||
LOG.error(DEFAULT_EXCEPTION_MESSAGE, e1);
|
||||
}
|
||||
});
|
||||
|
||||
t.start();
|
||||
}
|
||||
|
||||
private Retry.HandleErrorIssue<Order> getRetryHandleErrorIssue(Order order) {
|
||||
return (o, err) -> {
|
||||
if (PaymentDetailsErrorException.class.isAssignableFrom(err.getClass())) {
|
||||
handlePaymentDetailsError(order.id, o);
|
||||
} else {
|
||||
if (o.messageSent.equals(MessageSent.NONE_SENT)) {
|
||||
handlePaymentError(order.id, o);
|
||||
}
|
||||
if (o.paid.equals(PaymentStatus.TRYING) && System
|
||||
.currentTimeMillis() - o.createdTime < paymentTime) {
|
||||
var qt = new QueueTask(o, TaskType.PAYMENT, -1);
|
||||
updateQueue(qt);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void handlePaymentError(String orderId, Order o) {
|
||||
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_ID + ": Payment error, going to queue..", orderId);
|
||||
sendPaymentPossibleErrorMsg(o);
|
||||
}
|
||||
|
||||
private void handlePaymentDetailsError(String orderId, Order o) {
|
||||
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_ID + ": Payment details incorrect, failed..", orderId);
|
||||
o.paid = PaymentStatus.NOT_DONE;
|
||||
sendPaymentFailureMessage(o);
|
||||
}
|
||||
|
||||
private Retry.Operation getRetryOperation(Order order) {
|
||||
return l -> {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
LOG.debug(ORDER_ID + ": Error in connecting to payment service,"
|
||||
+ " trying again..", order.id);
|
||||
} else {
|
||||
LOG.debug(ORDER_ID + ": Error in creating payment request..", order.id);
|
||||
}
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (order.paid.equals(PaymentStatus.TRYING)) {
|
||||
var transactionId = paymentService.receiveRequest(order.price);
|
||||
order.paid = PaymentStatus.DONE;
|
||||
LOG.info(ORDER_ID + ": Payment successful, transaction Id: {}",
|
||||
order.id, transactionId);
|
||||
if (!finalSiteMsgShown) {
|
||||
LOG.info("Payment made successfully, thank you for shopping with us!!");
|
||||
finalSiteMsgShown = true;
|
||||
}
|
||||
sendSuccessMessage(order);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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..
|
||||
@@ -256,7 +277,7 @@ public class Commander {
|
||||
}
|
||||
var list = queue.exceptionsList;
|
||||
Thread t = new Thread(() -> {
|
||||
Retry.Operation op = (list1) -> {
|
||||
Retry.Operation op = list1 -> {
|
||||
if (!list1.isEmpty()) {
|
||||
LOG.warn(ORDER_ID + ": Error in connecting to queue db, trying again..", qt.order.id);
|
||||
throw list1.remove(0);
|
||||
@@ -282,7 +303,7 @@ public class Commander {
|
||||
try {
|
||||
r.perform(list, qt);
|
||||
} catch (Exception e1) {
|
||||
LOG.error("An exception occurred", e1);
|
||||
LOG.error(DEFAULT_EXCEPTION_MESSAGE, e1);
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
@@ -291,7 +312,7 @@ public class Commander {
|
||||
private void tryDoingTasksInQueue() { //commander controls operations done to queue
|
||||
var list = queue.exceptionsList;
|
||||
var t2 = new Thread(() -> {
|
||||
Retry.Operation op = (list1) -> {
|
||||
Retry.Operation op = list1 -> {
|
||||
if (!list1.isEmpty()) {
|
||||
LOG.warn("Error in accessing queue db to do tasks, trying again..");
|
||||
throw list1.remove(0);
|
||||
@@ -305,7 +326,7 @@ public class Commander {
|
||||
try {
|
||||
r.perform(list, null);
|
||||
} catch (Exception e1) {
|
||||
LOG.error("An exception occurred", e1);
|
||||
LOG.error(DEFAULT_EXCEPTION_MESSAGE, e1);
|
||||
}
|
||||
});
|
||||
t2.start();
|
||||
@@ -314,7 +335,7 @@ public class Commander {
|
||||
private void tryDequeue() {
|
||||
var list = queue.exceptionsList;
|
||||
var t3 = new Thread(() -> {
|
||||
Retry.Operation op = (list1) -> {
|
||||
Retry.Operation op = list1 -> {
|
||||
if (!list1.isEmpty()) {
|
||||
LOG.warn("Error in accessing queue db to dequeue task, trying again..");
|
||||
throw list1.remove(0);
|
||||
@@ -329,7 +350,7 @@ public class Commander {
|
||||
try {
|
||||
r.perform(list, null);
|
||||
} catch (Exception e1) {
|
||||
LOG.error("An exception occurred", e1);
|
||||
LOG.error(DEFAULT_EXCEPTION_MESSAGE, e1);
|
||||
}
|
||||
});
|
||||
t3.start();
|
||||
@@ -343,15 +364,13 @@ public class Commander {
|
||||
var list = messagingService.exceptionsList;
|
||||
Thread t = new Thread(() -> {
|
||||
Retry.Operation op = handleSuccessMessageRetryOperation(order);
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
handleSuccessMessageErrorIssue(order, o);
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> handleSuccessMessageErrorIssue(order, o);
|
||||
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, order);
|
||||
} catch (Exception e1) {
|
||||
LOG.error("An exception occurred", e1);
|
||||
LOG.error(DEFAULT_EXCEPTION_MESSAGE, e1);
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
@@ -370,7 +389,7 @@ public class Commander {
|
||||
}
|
||||
|
||||
private Retry.Operation handleSuccessMessageRetryOperation(Order order) {
|
||||
return (l) -> {
|
||||
return l -> {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
LOG.debug(ORDER_ID + ERROR_CONNECTING_MSG_SVC
|
||||
@@ -398,18 +417,14 @@ public class Commander {
|
||||
}
|
||||
var list = messagingService.exceptionsList;
|
||||
var t = new Thread(() -> {
|
||||
Retry.Operation op = (l) -> {
|
||||
handlePaymentFailureRetryOperation(order, l);
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
handlePaymentErrorIssue(order, o);
|
||||
};
|
||||
Retry.Operation op = l -> handlePaymentFailureRetryOperation(order, l);
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> handlePaymentErrorIssue(order, o);
|
||||
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, order);
|
||||
} catch (Exception e1) {
|
||||
LOG.error("An exception occurred", e1);
|
||||
LOG.error(DEFAULT_EXCEPTION_MESSAGE, e1);
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
@@ -427,16 +442,14 @@ public class Commander {
|
||||
}
|
||||
}
|
||||
|
||||
private void handlePaymentFailureRetryOperation(Order order, List<Exception> l) throws Exception {
|
||||
private void handlePaymentFailureRetryOperation(Order order, List<Exception> l) throws IndexOutOfBoundsException, DatabaseUnavailableException {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
LOG.debug(ORDER_ID + ERROR_CONNECTING_MSG_SVC
|
||||
+ "(Payment Failure msg), trying again..", order.id);
|
||||
LOG.debug(ORDER_ID + ERROR_CONNECTING_MSG_SVC + "(Payment Failure msg), trying again..", order.id);
|
||||
} else {
|
||||
LOG.debug(ORDER_ID + ": Error in creating Payment Failure"
|
||||
+ " message request..", order.id);
|
||||
LOG.debug(ORDER_ID + ": Error in creating Payment Failure" + " message request..", order.id);
|
||||
}
|
||||
throw l.remove(0);
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
if (!order.messageSent.equals(MessageSent.PAYMENT_FAIL)
|
||||
&& !order.messageSent.equals(MessageSent.PAYMENT_SUCCESSFUL)) {
|
||||
@@ -454,18 +467,14 @@ public class Commander {
|
||||
}
|
||||
var list = messagingService.exceptionsList;
|
||||
var t = new Thread(() -> {
|
||||
Retry.Operation op = (l) -> {
|
||||
handlePaymentPossibleErrorMsgRetryOperation(order, l);
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
handlePaymentPossibleErrorMsgErrorIssue(order, o);
|
||||
};
|
||||
Retry.Operation op = l -> handlePaymentPossibleErrorMsgRetryOperation(order, l);
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> handlePaymentPossibleErrorMsgErrorIssue(order, o);
|
||||
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
try {
|
||||
r.perform(list, order);
|
||||
} catch (Exception e1) {
|
||||
LOG.error("An exception occurred", e1);
|
||||
LOG.error(DEFAULT_EXCEPTION_MESSAGE, e1);
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
@@ -484,7 +493,7 @@ public class Commander {
|
||||
}
|
||||
|
||||
private void handlePaymentPossibleErrorMsgRetryOperation(Order order, List<Exception> l)
|
||||
throws Exception {
|
||||
throws IndexOutOfBoundsException, DatabaseUnavailableException {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
LOG.debug(ORDER_ID + ERROR_CONNECTING_MSG_SVC
|
||||
@@ -493,7 +502,7 @@ public class Commander {
|
||||
LOG.debug(ORDER_ID + ": Error in creating Payment Error"
|
||||
+ " messaging request..", order.id);
|
||||
}
|
||||
throw l.remove(0);
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
if (order.paid.equals(PaymentStatus.TRYING) && order.messageSent
|
||||
.equals(MessageSent.NONE_SENT)) {
|
||||
@@ -511,7 +520,7 @@ public class Commander {
|
||||
}
|
||||
var list = employeeDb.exceptionsList;
|
||||
var t = new Thread(() -> {
|
||||
Retry.Operation op = (l) -> {
|
||||
Retry.Operation op = l -> {
|
||||
if (!l.isEmpty()) {
|
||||
LOG.warn(ORDER_ID + ": Error in connecting to employee handle,"
|
||||
+ " trying again..", order.id);
|
||||
@@ -537,18 +546,18 @@ public class Commander {
|
||||
try {
|
||||
r.perform(list, order);
|
||||
} catch (Exception e1) {
|
||||
LOG.error("An exception occurred", e1);
|
||||
LOG.error(DEFAULT_EXCEPTION_MESSAGE, e1);
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
}
|
||||
|
||||
private void doTasksInQueue() throws Exception {
|
||||
private void doTasksInQueue() throws IsEmptyException, InterruptedException {
|
||||
if (queueItems != 0) {
|
||||
var qt = queue.peek(); //this should probably be cloned here
|
||||
//this is why we have retry for doTasksInQueue
|
||||
LOG.trace(ORDER_ID + ": Started doing task of type {}", qt.order.id, qt.getType());
|
||||
if (qt.getFirstAttemptTime() == -1) {
|
||||
if (qt.isFirstAttempt()) {
|
||||
qt.setFirstAttemptTime(System.currentTimeMillis());
|
||||
}
|
||||
if (System.currentTimeMillis() - qt.getFirstAttemptTime() >= queueTaskTime) {
|
||||
@@ -556,43 +565,11 @@ public class Commander {
|
||||
LOG.trace(ORDER_ID + ": This queue task of type {}"
|
||||
+ " does not need to be done anymore (timeout), dequeue..", qt.order.id, qt.getType());
|
||||
} else {
|
||||
if (qt.taskType.equals(TaskType.PAYMENT)) {
|
||||
if (!qt.order.paid.equals(PaymentStatus.TRYING)) {
|
||||
tryDequeue();
|
||||
LOG.trace(ORDER_ID + ": This payment task already done, dequeueing..", qt.order.id);
|
||||
} else {
|
||||
sendPaymentRequest(qt.order);
|
||||
LOG.debug(ORDER_ID + ": Trying to connect to payment service..", qt.order.id);
|
||||
}
|
||||
} else if (qt.taskType.equals(TaskType.MESSAGING)) {
|
||||
if (qt.order.messageSent.equals(MessageSent.PAYMENT_FAIL)
|
||||
|| qt.order.messageSent.equals(MessageSent.PAYMENT_SUCCESSFUL)) {
|
||||
tryDequeue();
|
||||
LOG.trace(ORDER_ID + ": This messaging task already done, dequeue..", qt.order.id);
|
||||
} else if (qt.messageType == 1 && (!qt.order.messageSent.equals(MessageSent.NONE_SENT)
|
||||
|| !qt.order.paid.equals(PaymentStatus.TRYING))) {
|
||||
tryDequeue();
|
||||
LOG.trace(ORDER_ID + ": This messaging task does not need to be done,"
|
||||
+ " dequeue..", qt.order.id);
|
||||
} else if (qt.messageType == 0) {
|
||||
sendPaymentFailureMessage(qt.order);
|
||||
LOG.debug(ORDER_ID + TRY_CONNECTING_MSG_SVC, qt.order.id);
|
||||
} else if (qt.messageType == 1) {
|
||||
sendPaymentPossibleErrorMsg(qt.order);
|
||||
LOG.debug(ORDER_ID + TRY_CONNECTING_MSG_SVC, qt.order.id);
|
||||
} else if (qt.messageType == 2) {
|
||||
sendSuccessMessage(qt.order);
|
||||
LOG.debug(ORDER_ID + TRY_CONNECTING_MSG_SVC, qt.order.id);
|
||||
}
|
||||
} else if (qt.taskType.equals(TaskType.EMPLOYEE_DB)) {
|
||||
if (qt.order.addedToEmployeeHandle) {
|
||||
tryDequeue();
|
||||
LOG.trace(ORDER_ID + ": This employee handle task already done,"
|
||||
+ " dequeue..", qt.order.id);
|
||||
} else {
|
||||
employeeHandleIssue(qt.order);
|
||||
LOG.debug(ORDER_ID + ": Trying to connect to employee handle..", qt.order.id);
|
||||
}
|
||||
switch (qt.taskType) {
|
||||
case PAYMENT -> doPaymentTask(qt);
|
||||
case MESSAGING -> doMessagingTask(qt);
|
||||
case EMPLOYEE_DB -> doEmployeeDbTask(qt);
|
||||
default -> throw new IllegalArgumentException("Unknown task type");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -604,4 +581,47 @@ public class Commander {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
private void doEmployeeDbTask(QueueTask qt) {
|
||||
if (qt.order.addedToEmployeeHandle) {
|
||||
tryDequeue();
|
||||
LOG.trace(ORDER_ID + ": This employee handle task already done,"
|
||||
+ " dequeue..", qt.order.id);
|
||||
} else {
|
||||
employeeHandleIssue(qt.order);
|
||||
LOG.debug(ORDER_ID + ": Trying to connect to employee handle..", qt.order.id);
|
||||
}
|
||||
}
|
||||
|
||||
private void doMessagingTask(QueueTask qt) {
|
||||
if (qt.order.messageSent.equals(MessageSent.PAYMENT_FAIL)
|
||||
|| qt.order.messageSent.equals(MessageSent.PAYMENT_SUCCESSFUL)) {
|
||||
tryDequeue();
|
||||
LOG.trace(ORDER_ID + ": This messaging task already done, dequeue..", qt.order.id);
|
||||
} else if (qt.messageType == 1 && (!qt.order.messageSent.equals(MessageSent.NONE_SENT)
|
||||
|| !qt.order.paid.equals(PaymentStatus.TRYING))) {
|
||||
tryDequeue();
|
||||
LOG.trace(ORDER_ID + ": This messaging task does not need to be done,"
|
||||
+ " dequeue..", qt.order.id);
|
||||
} else if (qt.messageType == 0) {
|
||||
sendPaymentFailureMessage(qt.order);
|
||||
LOG.debug(ORDER_ID + TRY_CONNECTING_MSG_SVC, qt.order.id);
|
||||
} else if (qt.messageType == 1) {
|
||||
sendPaymentPossibleErrorMsg(qt.order);
|
||||
LOG.debug(ORDER_ID + TRY_CONNECTING_MSG_SVC, qt.order.id);
|
||||
} else if (qt.messageType == 2) {
|
||||
sendSuccessMessage(qt.order);
|
||||
LOG.debug(ORDER_ID + TRY_CONNECTING_MSG_SVC, qt.order.id);
|
||||
}
|
||||
}
|
||||
|
||||
private void doPaymentTask(QueueTask qt) {
|
||||
if (!qt.order.paid.equals(PaymentStatus.TRYING)) {
|
||||
tryDequeue();
|
||||
LOG.trace(ORDER_ID + ": This payment task already done, dequeueing..", qt.order.id);
|
||||
} else {
|
||||
sendPaymentRequest(qt.order);
|
||||
LOG.debug(ORDER_ID + ": Trying to connect to payment service..", qt.order.id);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.iluwatar.commander;
|
||||
|
||||
/**
|
||||
* Record to hold the parameters related to retries.
|
||||
* @param numOfRetries number of retries
|
||||
* @param retryDuration retry duration
|
||||
*/
|
||||
public record RetryParams(int numOfRetries, long retryDuration) {
|
||||
public static final RetryParams DEFAULT = new RetryParams(3, 30000L);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.iluwatar.commander;
|
||||
|
||||
/**
|
||||
* Record to hold parameters related to time limit
|
||||
* for various tasks.
|
||||
* @param queueTime time limit for queue
|
||||
* @param queueTaskTime time limit for queuing task
|
||||
* @param paymentTime time limit for payment error message
|
||||
* @param messageTime time limit for message time order
|
||||
* @param employeeTime time limit for employee handle time
|
||||
*/
|
||||
public record TimeLimits(long queueTime, long queueTaskTime, long paymentTime,
|
||||
long messageTime, long employeeTime) {
|
||||
|
||||
public static final TimeLimits DEFAULT = new TimeLimits(240000L, 60000L, 120000L, 150000L, 240000L);
|
||||
}
|
||||
@@ -72,4 +72,8 @@ public class QueueTask {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isFirstAttempt() {
|
||||
return this.firstAttemptTime == -1L;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user