diff --git a/payment/src/main/java/payment/services/PaymentServiceImpl.java b/payment/src/main/java/payment/services/PaymentServiceImpl.java index 3a3bf221..57e28513 100644 --- a/payment/src/main/java/payment/services/PaymentServiceImpl.java +++ b/payment/src/main/java/payment/services/PaymentServiceImpl.java @@ -95,7 +95,7 @@ public boolean cancelPayment(UUID paymentId) { Payment payment = paymentRepository.findById(paymentId); if (payment != null && payment.getPaymentStatus() == PaymentStatus.PENDING) { payment.setPaymentStatus(PaymentStatus.FAILED); - paymentRepository.savePayment(payment); + paymentRepository.updatePayment(payment); return true; } return false; diff --git a/payment/src/main/java/payment/services/outBoxProcessor/PaymentOutBoxProcessorImpl.java b/payment/src/main/java/payment/services/outBoxProcessor/PaymentOutBoxProcessorImpl.java index 1ff0ff17..8c10a219 100644 --- a/payment/src/main/java/payment/services/outBoxProcessor/PaymentOutBoxProcessorImpl.java +++ b/payment/src/main/java/payment/services/outBoxProcessor/PaymentOutBoxProcessorImpl.java @@ -40,15 +40,15 @@ public class PaymentOutBoxProcessorImpl implements PaymentOutBoxProcessor { BankClient bankClient; @Override - @Retry(maxRetries = 2, delay = 500, jitter = 200) // Retry mechanism for failures - @Timeout(500) // Timeout after 1 second + @Retry(delay = 1000, maxRetries = 3) // Retry mechanism for failures + @Timeout(1000) // 1s is the time out public Response processPaymentWithRetry(BankPaymentRequest request) { return bankClient.makeNewPayment(request); } int i = 0 ; @Override @Scheduled(every = "10s") - @Transactional(Transactional.TxType.REQUIRES_NEW) + @Transactional public void processOutboxEvents() { List unprocessedEvents = boxRepository.findUnprocessedEvents(); int maxEventsToProcess = 200; @@ -57,9 +57,7 @@ public void processOutboxEvents() { for (PaymentOutBox event : unprocessedEvents) { if (processedCount >= maxEventsToProcess) { i++ ; - System.out.println("oppa 33la sel3t oropa"+i); - - + System.out.println("rate limiter"+i); break; // Exit loop if we have processed the maximum number of events } JsonObject payloadJson = Json.createReader(new StringReader(event.getPayload())).readObject(); @@ -87,30 +85,20 @@ public void processOutboxEvents() { cardNumber, cardCode ); - - // Process the payment with retry and timeout mechanisms Response response = processPaymentWithRetry(bankPaymentRequest); - if (response.getStatus() == Response.Status.OK.getStatusCode()) { paymentService.completePayment(payment.getPaymentId()); } - else if (response.getStatus() == Response.Status.CONFLICT.getStatusCode()) { - paymentService.cancelPayment(payment.getPaymentId()) ; - System.out.println("oooooh ya 3alm"); - } else { - paymentService.cancelPayment(payment.getPaymentId()); - } - - // Mark the event as processed event.setProcessed(true); boxRepository.update(event); processedCount++; } catch (Exception e) { - System.err.println("Error processing payment: " + e.getMessage()); + paymentService.cancelPayment(event.getPaymentId()); event.setProcessed(true); boxRepository.update(event); + System.out.println(e); processedCount++; } } diff --git a/payment/src/main/resources/application.properties b/payment/src/main/resources/application.properties index fdb7d2cb..c9edb3e6 100644 --- a/payment/src/main/resources/application.properties +++ b/payment/src/main/resources/application.properties @@ -17,7 +17,7 @@ quarkus.rate-limiter.buckets.group1.limits[0].period: 1S # fair use quarkus.rate-limiter.buckets.group1.limits[1].permitted-uses: 100 quarkus.rate-limiter.buckets.group1.limits[1].period: 5M -quarkus.transaction-manager.default-transaction-timeout=120 +