public static List < String > pullMessages ( String queueURL , int numberOfMessages ) { List < String > messages = new ArrayList <> ( ) ; if ( ! StringUtils . isBlank ( queueURL ) ) { try { int batchSteps = 1 ; int maxForBatch = numberOfMessages ; if ( ( numberOfMessages > MAX_MESSAGES ) ) { batchSteps = ( numberOfMessages / MAX_MESSAGES ) + ( ( numberOfMessages % MAX_MESSAGES > 0 ) ? 1 : 0 ) ; maxForBatch = MAX_MESSAGES ; } for ( int i = 0 ; i < batchSteps ; i ++ ) { List < Message > list = getClient ( ) . receiveMessage ( new ReceiveMessageRequest ( queueURL ) . withMaxNumberOfMessages ( maxForBatch ) . withWaitTimeSeconds ( POLLING_INTERVAL ) ) . getMessages ( ) ; if ( list != null && ! list . isEmpty ( ) ) { List < DeleteMessageBatchRequestEntry > del = new ArrayList <> ( ) ; for ( Message msg : list ) { messages . add ( msg . getBody ( ) ) ; del . add ( new DeleteMessageBatchRequestEntry ( msg . getMessageId ( ) , msg . getReceiptHandle ( ) ) ) ; } getClient ( ) . deleteMessageBatch ( queueURL , del ) ; } } } catch ( AmazonServiceException ase ) { logException ( ase ) ; } catch ( AmazonClientException ace ) { logger . error ( "Could not reach SQS. {}" , ace . toString ( ) ) ; } } return messages ; }
Pulls a number of messages from an SQS queue .