0
votes

I have a backend and an AI part running on different servers. The communication between backend and ai is through AWS SQS**.

  • I have a method named handleSQSMessage() which runs asynchronously to listen to the SQS queue for the data.
  • If data is in the queue it automatically retrieves and process it.

Now i want to implement a timeout of 2 minutes. If data is not received within 2 minutes, i want to inform the frontend about the timeout.

The problem is how can i implement the timout - as handleSQSMessage() is an async method and it will be always running on different thread to listen to the queue.

To send data to the SQS:

 private void sendToSQS(JSONObject jsonObject) {
       
        awsService.sendToSQS(jsonObject);
    }

The method gettingData is a SQSListener and it listens to the queue continuously and sends the data to the handleMessageFromSQS method.

@SqsListener(value = "${aws.sqs.test}",deletionPolicy = SqsMessageDeletionPolicy.NEVER)
    public void gettingData(Acknowledgment acknowledgment,String message) throws JsonParseException, JsonMappingException, IOException, ParseException {
        logger.info("Message received from sqs {}", message);

        try {
            acknowledgment.acknowledge().get();
        } catch (InterruptedException | ExecutionException e) {
            logger.error(e.getMessage());
        }
       
       downloadService.handleMessageFromSQS(message);


    }