diff --git a/src/main/java/zw/qantra/tm/domain/services/processors/integrations/StewardIntegrationProcessor.java b/src/main/java/zw/qantra/tm/domain/services/processors/integrations/StewardIntegrationProcessor.java index 203093e..87bfbd4 100644 --- a/src/main/java/zw/qantra/tm/domain/services/processors/integrations/StewardIntegrationProcessor.java +++ b/src/main/java/zw/qantra/tm/domain/services/processors/integrations/StewardIntegrationProcessor.java @@ -46,6 +46,10 @@ public class StewardIntegrationProcessor implements TransactionProcessorInterfac @Transactional public Object process(Transaction transaction) throws Exception { + if(settingService.getBooleanSetting("SIMULATE_INTEGRATION_DELAY")){ + Thread.sleep(5000); + } + if(settingService.getBooleanSetting("SIMULATE_INTEGRATION_FAILURE")){ transaction.setStatus(Status.FAILED); transaction.setResponseCode("92"); diff --git a/src/main/java/zw/qantra/tm/domain/workflows/handlers/IntegrationHandler.java b/src/main/java/zw/qantra/tm/domain/workflows/handlers/IntegrationHandler.java index 64646b5..fd750e3 100644 --- a/src/main/java/zw/qantra/tm/domain/workflows/handlers/IntegrationHandler.java +++ b/src/main/java/zw/qantra/tm/domain/workflows/handlers/IntegrationHandler.java @@ -15,6 +15,9 @@ import zw.qantra.tm.domain.services.processors.TransactionProcessorInterface; import zw.qantra.tm.exceptions.ApiException; import zw.qantra.tm.utils.Utils; +import java.util.ArrayList; +import java.util.List; + @Service("integration") @RequiredArgsConstructor public class IntegrationHandler implements HandlerInterface { @@ -26,6 +29,8 @@ public class IntegrationHandler implements HandlerInterface { private final TransactionEventService transactionEventService; private final TransactionService transactionService; + List lock = new ArrayList<>(); + @Override public Object process(Transaction transaction) throws Exception { transaction.setType(RequestType.INTEGRATION); @@ -34,6 +39,14 @@ public class IntegrationHandler implements HandlerInterface { if(transaction.getIntegrationStatus().equals(Status.SUCCESS)) return transaction; + // prevent race condition + if (isLocked(transaction.getId().toString())) { + logger.info("Transaction already processing - {}", transaction.getId()); + transaction.setStatus(Status.PROCESSING); + return transaction; + } + + addLock(transaction.getId().toString()); TransactionEvent transactionEvent = TransactionEvent.builder() .transactionId(transaction.getId().toString()) .event(transaction.getType().name()) @@ -70,6 +83,7 @@ public class IntegrationHandler implements HandlerInterface { transaction.setIntegrationStatus(transaction.getStatus()); + removeLock(transaction.getId().toString()); if(transaction.getStatus() != Status.SUCCESS){ transactionService.save(transaction); throw new ApiException("Transaction failed: " + transaction.getErrorMessage() ); @@ -84,4 +98,16 @@ public class IntegrationHandler implements HandlerInterface { transactionService.save(transaction); return t1; } + + private void addLock(String uid) { + lock.add(uid); + } + + private boolean removeLock(String uid) { + return lock.remove(uid); + } + + private boolean isLocked(String uid) { + return lock.contains(uid); + } } diff --git a/src/main/resources/application-vusa.properties b/src/main/resources/application-vusa.properties index 72f727a..f581428 100644 --- a/src/main/resources/application-vusa.properties +++ b/src/main/resources/application-vusa.properties @@ -1,11 +1,11 @@ -sbz.merchant.url=http://localhost:24000/v1 -#sbz.merchant.url=https://api.stewardpay.co.zw/lab/v2 +#sbz.merchant.url=http://localhost:24000/v1 +sbz.merchant.url=https://api.stewardpay.co.zw/lab/v2 -#spring.datasource.url=jdbc:postgresql://localhost:5432/qpay?useLegacyDatetimeCode=false -#spring.datasource.username=postgres -#spring.datasource.password=example +spring.datasource.url=jdbc:postgresql://localhost:5432/qpay?useLegacyDatetimeCode=false +spring.datasource.username=postgres +spring.datasource.password=example -#nflow.db.postgresql.driver=org.postgresql.Driver -#nflow.db.postgresql.url=jdbc:postgresql://localhost:5432/qpay -#nflow.db.postgresql.user=postgres -#nflow.db.postgresql.password=example \ No newline at end of file +nflow.db.postgresql.driver=org.postgresql.Driver +nflow.db.postgresql.url=jdbc:postgresql://localhost:5432/qpay +nflow.db.postgresql.user=postgres +nflow.db.postgresql.password=example \ No newline at end of file