List of usage examples for com.amazonaws.services.sqs.model ReceiveMessageResult getMessages
public java.util.List<Message> getMessages()
A list of messages.
From source file:com.netflix.spinnaker.front50.model.TemporarySQSQueue.java
License:Apache License
List<Message> fetchMessages() { ReceiveMessageResult receiveMessageResult = amazonSQS .receiveMessage(new ReceiveMessageRequest(temporaryQueue.sqsQueueUrl).withMaxNumberOfMessages(10) .withWaitTimeSeconds(1)); return receiveMessageResult.getMessages(); }
From source file:com.netflix.suro.sink.notice.SQSNotice.java
License:Apache License
@Override public String recv() { ReceiveMessageRequest request = new ReceiveMessageRequest().withQueueUrl(queueUrls.get(0)) .withMaxNumberOfMessages(1); try {/*w w w . ja v a 2s . c o m*/ ReceiveMessageResult result = sqsClient.receiveMessage(request); if (!result.getMessages().isEmpty()) { Message msg = result.getMessages().get(0); recvMessageCount.incrementAndGet(); DeleteMessageRequest deleteReq = new DeleteMessageRequest().withQueueUrl(queueUrls.get(0)) .withReceiptHandle(msg.getReceiptHandle()); sqsClient.deleteMessage(deleteReq); if (enableBase64Encoding) { return new String(Base64.decodeBase64(msg.getBody().getBytes()), Charsets.UTF_8); } else { return msg.getBody(); } } else { return ""; } } catch (Exception e) { log.error("Exception while recving SQS notice: " + e.getMessage(), e); return ""; } }
From source file:com.netflix.suro.sink.notice.SQSNotice.java
License:Apache License
@Override public Pair<String, String> peek() { ReceiveMessageRequest request = new ReceiveMessageRequest().withQueueUrl(queueUrls.get(0)) .withMaxNumberOfMessages(1); try {/*from www . jav a 2 s . co m*/ ReceiveMessageResult result = sqsClient.receiveMessage(request); if (!result.getMessages().isEmpty()) { Message msg = result.getMessages().get(0); recvMessageCount.incrementAndGet(); if (enableBase64Encoding) { return new Pair<String, String>(msg.getReceiptHandle(), new String(Base64.decodeBase64(msg.getBody().getBytes()), Charsets.UTF_8)); } else { return new Pair<String, String>(msg.getReceiptHandle(), msg.getBody()); } } else { return null; } } catch (Exception e) { log.error("Exception while recving SQS notice: " + e.getMessage(), e); return null; } }
From source file:com.pinterest.teletraan.worker.LaunchEventCollector.java
License:Apache License
public void collectEvents() throws Exception { while (true) { ReceiveMessageRequest request = new ReceiveMessageRequest(); request.setMaxNumberOfMessages(10); ReceiveMessageResult result = sqsClient.receiveMessage(request); List<Message> messageList = result.getMessages(); if (messageList.isEmpty()) { LOG.info("No more Launch activity available at the moment."); return; }/*from ww w.j a va2 s. c o m*/ LOG.info(String.format("Collect %d events from AWS SQS.", messageList.size())); ArrayList<DeleteMessageBatchRequestEntry> entries = new ArrayList<>(); for (Message message : messageList) { try { boolean hasProcessed = processMessage(message); if (hasProcessed) { DeleteMessageBatchRequestEntry entry = new DeleteMessageBatchRequestEntry(); entry.setId(message.getMessageId()); entry.setReceiptHandle(message.getReceiptHandle()); entries.add(entry); } } catch (Exception ex) { LOG.error("Failed to process SQS message:", message, ex); } } if (!entries.isEmpty()) { DeleteMessageBatchRequest deleteMessageBatchRequest = new DeleteMessageBatchRequest(); deleteMessageBatchRequest.setEntries(entries); LOG.debug(String.format("Successful process %d messages, deleting them from SQS.", entries.size())); sqsClient.deleteMessageBatch(deleteMessageBatchRequest); } } }
From source file:com.pocketdealhunter.HotDealsMessagesUtil.java
License:Open Source License
public List<Message> getMessageQueue() { try {/* w ww .jav a 2s . co m*/ ReceiveMessageRequest rmr = new ReceiveMessageRequest(this.queueUrl); rmr.setMaxNumberOfMessages(10); rmr.setVisibilityTimeout(2); List<Message> messages = null; ArrayList<Message> allMessages = new ArrayList<Message>(100); do { ReceiveMessageResult result = this.sqsClient.receiveMessage(rmr); messages = result.getMessages(); allMessages.addAll(messages); try { Thread.sleep(1 * 1000); } catch (Exception exception) { } } while (!messages.isEmpty()); return allMessages; } catch (Exception exception) { System.out.println("Exception = " + exception); return Collections.emptyList(); } }
From source file:com.smoketurner.pipeline.application.core.AmazonSQSIterator.java
License:Apache License
@Override public List<Message> next() { LOGGER.debug("Requesting {} messages from SQS (wait time={}, visibility timeout={})", MAX_NUMBER_OF_MESSAGES, WAIT_TIME_SECS, VISIBILITY_TIMEOUT_SECS); receiveRequests.inc();/*from w w w .ja va 2s.co m*/ final ReceiveMessageResult result = client.receiveMessage(request); final int numMessages = result.getMessages().size(); LOGGER.debug("Received {} messages from SQS", numMessages); messageCounts.update(numMessages); return result.getMessages(); }
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 {//from w ww . j a v a 2 s. 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; }
From source file:com.twitter.services.SQS.java
License:Open Source License
public static Message receiveMessage() { // Receive a message if (sqs == null || myQueueUrl.isEmpty()) { createSQS();//ww w .j a v a 2 s .com } ReceiveMessageRequest request = new ReceiveMessageRequest(myQueueUrl); request.setMaxNumberOfMessages(1); ReceiveMessageResult result = sqs.receiveMessage(request); if (result != null && result.getMessages().size() > 0) { return (Message) result.getMessages().get(0); } return null; }
From source file:com.vitembp.embedded.interfaces.AmazonSQSControl.java
License:Open Source License
/** * Processes messages from the device's SQS queue. *///from w w w.ja v a 2 s . co m private void processMessages() { ExecutorService executor = Executors.newFixedThreadPool(this.threadCount); // create processor threads for (int i = 0; i < this.threadCount; i++) { executor.submit(() -> { // parameters of an increasing backoff in case of errors to // prevent excessive retry rate int startErrorBackoff = 200; int errorBackoff = startErrorBackoff; float errorFactor = 2; int errorMax = 5000; while (isRunning) { try { // create the queue in this try-block so if the service starts // when a connection is not available it will cleanly retry this.createQueue(); // check for new commands ReceiveMessageResult result = sqsClient.receiveMessage(queueUrl); // process commands result.getMessages().forEach((msg) -> { // get message text String toProcess = msg.getBody(); // remove message from queue this.sqsClient.deleteMessage(this.queueUrl, msg.getReceiptHandle()); // process the message this.parseMessage(toProcess); }); // reset error backoff on success errorBackoff = startErrorBackoff; } catch (Exception e) { LOGGER.error("Unexpected Exception processing SQS queue.", e); // wait for the backoff period to prevent retry flooding try { LOGGER.error("Backing off for " + Integer.toString(errorBackoff) + "ms."); Thread.sleep(errorBackoff); } catch (InterruptedException ex) { LOGGER.error("Interrupted while waiting for backoff on SQS queue failure.", ex); } // increase backoff factor until it is at the max value errorBackoff *= errorFactor; if (errorBackoff > errorMax) { errorBackoff = errorMax; } } } }); } }
From source file:doug.iotdemo.analyzer.Analyzer.java
License:Open Source License
void run() throws IOException, MqttException { System.out.println("Doing initial scan"); doInitialScan();// w w w . j a va2 s. c o m System.out.println("Done"); String url = AmazonUtils.getTimeQueueURL(); AmazonSQS sqs = new AmazonSQSClient(); while (true) { ReceiveMessageResult msgResult = sqs.receiveMessage(url); for (Message msg : msgResult.getMessages()) { JsonObject request = new JsonParser().parse(msg.getBody()).getAsJsonObject(); String sensor = request.get("sensor").getAsString(); long time = request.get("time").getAsLong(); addTime(sensor, time); saveSensor(sensor); sqs.deleteMessage(url, msg.getReceiptHandle()); } } }