How to use CompletableFuture inside a sqs queue listener in spring boot?

Viewed 55

I am working on a spring boot app where I am receiving a callback from a 3rd party api and that 3rd party also expects a response.

There are multiple db operations that I need to perform and to avoid data inconsistency during load I am using a SQS queue to push data and do all the operation.

The Thing is I also want to send the response back to the 3rd party immediately and the DB operations will be running in the background.

How is this possible with the SQS queue and CompletableFuture?

Here's the snippet:

// this method is a SQS Listener. 


 public PaymentCallback acceptPaymentForPendingOrders(String callbackRequest) {
        PaymentCallbackRequest paymentCallbackRequest = decryptCallBackResponse(callbackRequest);
           
        // here's all the DB operations I am doing async.
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(new Runnable() {
            @Override
            @Transactional
            public void run() {
                try {
                    orderSettlingRequest.setPaymentId(savedPayment.getId());
                    ResponseEntity response = orderServiceClient.settleBtoBOrders(orderSettlingRequest);

                } catch (Exception e) {
                    //credit the full amount to wallet of the user
                    log.error("Error occurred while settling b2b orders using credit",e);
                }
            }
        });
       
        //sending response immediately not waiting for asnyc operations.
        return paymentCallbackRepo.save(paymentCallback);

    }

Can I do this when using queue? Is this feasible?

0 Answers
Related