Example usage for com.amazonaws.services.sqs AmazonSQSAsync receiveMessageAsync

List of usage examples for com.amazonaws.services.sqs AmazonSQSAsync receiveMessageAsync

Introduction

In this page you can find the example usage for com.amazonaws.services.sqs AmazonSQSAsync receiveMessageAsync.

Prototype

java.util.concurrent.Future<ReceiveMessageResult> receiveMessageAsync(String queueUrl);

Source Link

Document

Simplified method form for invoking the ReceiveMessage operation.

Usage

From source file:com.streamsets.pipeline.stage.origin.sqs.SqsConsumerWorkerCallable.java

License:Apache License

@Override
public Exception call() throws Exception {
    Exception terminatingException = null;
    final AmazonSQSAsync asyncConsumer = sqsAsync;

    final ArrayBlockingQueue<String> urlQueue = new ArrayBlockingQueue(queueUrlToNamePrefix.size(), false,
            queueUrlToNamePrefix.keySet());

    while (!context.isStopped() && terminatingException == null) {
        String nextQueueUrl = null;
        try {// w  ww . j  av  a 2s.  c  om
            nextQueueUrl = urlQueue.take();
            final ReceiveMessageRequest receiveRequest = new ReceiveMessageRequest()
                    .withMaxNumberOfMessages(numMessagesPerRequest).withQueueUrl(nextQueueUrl);
            if (pollWaitTimeSeconds > 0) {
                receiveRequest.setWaitTimeSeconds(pollWaitTimeSeconds);
            }
            if (messageAttributeNames.size() > 0) {
                receiveRequest.setMessageAttributeNames(messageAttributeNames);
            }
            Future<ReceiveMessageResult> resultFuture = asyncConsumer.receiveMessageAsync(receiveRequest);

            ReceiveMessageResult result = resultFuture.get();
            for (Message message : result.getMessages()) {
                final String recordId = getRecordId(message, nextQueueUrl);
                DataParser parser = null;
                try {
                    parser = context.getService(DataFormatParserService.class).getParser(recordId,
                            message.getBody());
                    Record record = null;
                    do {
                        try {
                            record = parser.parse();
                        } catch (RecoverableDataParserException e) {
                            // log the error and keep trying to parse this message
                            LOG.error(Errors.SQS_04.getMessage(), e.getMessage(), e);
                            terminatingException = new StageException(Errors.SQS_04, e.getMessage(), e);
                        } catch (DataParserException e) {
                            // log the error and stop trying to parse this message
                            LOG.error(Errors.SQS_07.getMessage(), e.getMessage(), e);
                            errorRecordHandler.onError(Errors.SQS_07, e.getMessage(), e);
                            break;
                        }
                    } while (record == null);

                    setSqsAttributesOnRecord(message, record, nextQueueUrl,
                            queueUrlToNamePrefix.get(nextQueueUrl));

                    startBatchIfNeeded();
                    batchContext.getBatchMaker().addRecord(record);
                    commitQueueUrlsToMessages.put(nextQueueUrl, message);

                    if (++batchRecordCount > maxBatchSize) {
                        cycleBatch();
                    }
                } catch (DataParserException e) {
                    LOG.error(Errors.SQS_05.getMessage(), e.getMessage(), e);
                    terminatingException = new StageException(Errors.SQS_05, e.getMessage(), e);
                    break;
                } finally {
                    if (parser != null) {
                        parser.close();
                    }
                }
            }

            boolean batchMaxTimeElapsed = Clock.systemUTC().millis() > lastBatchStartTimestamp
                    + maxBatchWaitTimeMs;
            if (batchMaxTimeElapsed) {
                cycleBatch();
            }
        } catch (InterruptedException e) {
            LOG.error("InterruptedException trying to get SQS messages: {}", e.getMessage(), e);
            Thread.currentThread().interrupt();
            break;
        } finally {
            if (nextQueueUrl != null) {
                urlQueue.put(nextQueueUrl);
            }
        }
    }
    flushBatch();
    Optional.ofNullable(asyncConsumer).ifPresent(AmazonSQSAsync::shutdown);
    return terminatingException;
}