com.amazonaws.services.sqs.model.Message类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(12.5k)|赞(0)|评价(0)|浏览(150)

本文整理了Java中com.amazonaws.services.sqs.model.Message类的一些代码示例,展示了Message类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Message类的具体详情如下:
包路径:com.amazonaws.services.sqs.model.Message
类名称:Message

Message介绍

[英]An Amazon SQS message.
[中]亚马逊SQS信息。

代码示例

代码示例来源:origin: aws/aws-sdk-java

@Override
public int hashCode() {
  final int prime = 31;
  int hashCode = 1;
  hashCode = prime * hashCode + ((getMessageId() == null) ? 0 : getMessageId().hashCode());
  hashCode = prime * hashCode + ((getReceiptHandle() == null) ? 0 : getReceiptHandle().hashCode());
  hashCode = prime * hashCode + ((getMD5OfBody() == null) ? 0 : getMD5OfBody().hashCode());
  hashCode = prime * hashCode + ((getBody() == null) ? 0 : getBody().hashCode());
  hashCode = prime * hashCode + ((getAttributes() == null) ? 0 : getAttributes().hashCode());
  hashCode = prime * hashCode + ((getMD5OfMessageAttributes() == null) ? 0 : getMD5OfMessageAttributes().hashCode());
  hashCode = prime * hashCode + ((getMessageAttributes() == null) ? 0 : getMessageAttributes().hashCode());
  return hashCode;
}

代码示例来源:origin: aws/aws-sdk-java

/**
 * Throw an exception if the MD5 checksums included in the ReceiveMessageResult do not match the
 * client-side calculation on the received messages.
 */
private static void receiveMessageResultMd5Check(ReceiveMessageResult receiveMessageResult) {
  if (receiveMessageResult.getMessages() != null) {
    for (Message messageReceived : receiveMessageResult.getMessages()) {
      String messageBody = messageReceived.getBody();
      String bodyMd5Returned = messageReceived.getMD5OfBody();
      String clientSideBodyMd5 = calculateMessageBodyMd5(messageBody);
      if (!clientSideBodyMd5.equals(bodyMd5Returned)) {
        throw new AmazonClientException(String.format(MD5_MISMATCH_ERROR_MESSAGE, MESSAGE_BODY,
            clientSideBodyMd5, bodyMd5Returned));
      }
      Map<String, MessageAttributeValue> messageAttr = messageReceived.getMessageAttributes();
      if (messageAttr != null && !messageAttr.isEmpty()) {
        String attrMd5Returned = messageReceived.getMD5OfMessageAttributes();
        String clientSideAttrMd5 = calculateMessageAttributesMd5(messageAttr);
        if (!clientSideAttrMd5.equals(attrMd5Returned)) {
          throw new AmazonClientException(String.format(MD5_MISMATCH_ERROR_MESSAGE, MESSAGE_ATTRIBUTES,
              clientSideAttrMd5, attrMd5Returned));
        }
      }
    }
  }
}

代码示例来源:origin: Netflix/conductor

@VisibleForTesting
List<Message> receiveMessages() {
  try {
    ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest()
        .withQueueUrl(queueURL)
        .withVisibilityTimeout(visibilityTimeoutInSeconds)
        .withMaxNumberOfMessages(batchSize);
    ReceiveMessageResult result = client.receiveMessage(receiveMessageRequest);
    List<Message> messages = result.getMessages().stream()
        .map(msg -> new Message(msg.getMessageId(), msg.getBody(), msg.getReceiptHandle()))
        .collect(Collectors.toList());
    Monitors.recordEventQueueMessagesProcessed(QUEUE_TYPE, this.queueName, messages.size());
    return messages;
  } catch (Exception e) {
    logger.error("Exception while getting messages from SQS", e);
    Monitors.recordObservableQMessageReceivedErrors(QUEUE_TYPE);
  }
  return new ArrayList<>();
}

代码示例来源:origin: OpenNMS/opennms

messages = sqs.receiveMessage(queueUrl).getMessages();
} catch (RuntimeException e) {
  LOG.error("Unexpected exception while receiving messages from " + queueUrl, e);
  for (com.amazonaws.services.sqs.model.Message m : messages) {
    try {
      LOG.debug("Received SQS message with ID {} from {}", m.getMessageId(), queueUrl);
      final Message msg = module.unmarshal(m.getBody().getBytes(StandardCharsets.UTF_8));
      dispatch(module, msg);
      LOG.debug("Message with ID {} successfully dispatched.", m.getMessageId(), queueUrl);
    } catch (RuntimeException e) {
      final String msg = String.format("Unexpected exception while dispatching message with ID %s from %s", m.getMessageId(), queueUrl);
      LOG.warn(msg, e);
    } finally {
      LOG.debug("Deleting SQS message receipt handle {} from {}", m.getReceiptHandle(), queueUrl);
      sqs.deleteMessage(queueUrl, m.getReceiptHandle());

代码示例来源:origin: aws/aws-sdk-java

private void deleteMessage(Message message) {
  try {
    sqs.deleteMessage(new DeleteMessageRequest(queueUrl, message.getReceiptHandle()));
  } catch (Exception e) {}
}

代码示例来源:origin: awsdocs/aws-doc-sdk-examples

CreateQueueResult create_result = sqs.createQueue(QUEUE_NAME);
} catch (AmazonSQSException e) {
  if (!e.getErrorCode().equals("QueueAlreadyExists")) {
String queueUrl = sqs.getQueueUrl(QUEUE_NAME).getQueueUrl();
    .withMessageBody("hello world")
    .withDelaySeconds(5);
sqs.sendMessage(send_msg_request);
  sqs.deleteMessage(queueUrl, m.getReceiptHandle());

代码示例来源:origin: apache/nifi

for (final Map.Entry<String, String> entry : message.getAttributes().entrySet()) {
  attributes.put("sqs." + entry.getKey(), entry.getValue());
for (final Map.Entry<String, MessageAttributeValue> entry : message.getMessageAttributes().entrySet()) {
  attributes.put("sqs." + entry.getKey(), entry.getValue().getStringValue());
attributes.put("hash.value", message.getMD5OfBody());
attributes.put("hash.algorithm", "md5");
attributes.put("sqs.message.id", message.getMessageId());
attributes.put("sqs.receipt.handle", message.getReceiptHandle());
for (final Message message : messages) {
  final DeleteMessageBatchRequestEntry entry = new DeleteMessageBatchRequestEntry();
  entry.setId(message.getMessageId());
  entry.setReceiptHandle(message.getReceiptHandle());
  deleteRequestEntries.add(entry);

代码示例来源:origin: apache/usergrid

final String originalBody = message.getBody();
  final JsonNode bodyNode = mapper.readTree( message.getBody() );
  logger.error( "failed to deserialize message: {}", message.getBody(), e );
  throw new RuntimeException( e );
LegacyQueueMessage queueMessage = new LegacyQueueMessage( message.getMessageId(), message.getReceiptHandle(), payload,
  message.getAttributes().get( "type" ) );
queueMessage.setStringBody( originalBody );
int receiveCount = Integer.valueOf(message.getAttributes().get("ApproximateReceiveCount"));
queueMessage.setReceiveCount( receiveCount );
queueMessages.add( queueMessage );

代码示例来源:origin: apache/nifi

@Override
  public void process(final OutputStream out) throws IOException {
    out.write(message.getBody().getBytes(charset));
  }
});

代码示例来源:origin: org.symphonyoss.s2.fugue/aws-fugue

@Override
public void run()
 try(ITraceContextTransaction traceTransaction = traceFactory_.createTransaction("PubSub:SQS", message_.getMessageId(), tenantId_))
  long retryTime = manager_.handleMessage(consumer_, message_.getBody(), trace, message_.getMessageId());
    sqsClient_.deleteMessage(queueUrl_, message_.getReceiptHandle());
    traceTransaction.finished();
    sqsClient_.changeMessageVisibility(queueUrl_, message_.getReceiptHandle(), visibilityTimout);
    traceTransaction.aborted();

代码示例来源:origin: pl.chilldev.commons/commons-aws

/**
 * Processes queue messages.
 */
public void process() {
  for (Message message : this.sqs.receiveMessage(this.queueUrl).getMessages()) {
    this.messageHandler.accept(message);
    this.sqs.deleteMessage(this.queueUrl, message.getReceiptHandle());
  }
}

代码示例来源:origin: aws/aws-sdk-java

List<Message> messages = sqs.receiveMessage(new ReceiveMessageRequest(queueUrl)).getMessages();
for (Message message : messages) {
  String messageBody = message.getBody();
  if (!messageBody.startsWith("{")) {
    messageBody = new String(BinaryUtils.fromBase64(messageBody));

代码示例来源:origin: bazaarvoice/emodb

@Override
  public ScanRangeComplete apply(Message message) {
    QueueScanRangeComplete completion = JsonHelper.fromJson(message.getBody(), QueueScanRangeComplete.class);
    completion.setMessageId(message.getReceiptHandle());
    return completion;
  }
})

代码示例来源:origin: Comcast/cmb

Map<String, String> attributes = receivedMessage.getAttributes();
out.println("<td>" + receivedMessage.getReceiptHandle() + "</td>");
out.println("<td>" + receivedMessage.getMD5OfBody() + "</td>");
String messageBody=receivedMessage.getBody();
String messageBodyPart1=null;
String messageBodyPart2=null;
  out.println("</td>");
} else {
  out.println("<td>"+ receivedMessage.getBody() + "</td>");
out.println("<td>"+ receivedMessage.getMessageAttributes().size() + "</td>");
out.println("<td>"+ timeSent + "</td>");
out.println("<td>"+ timeReceived + "</td>");
out.println("<td>"+ attributes.get("ApproximateReceiveCount") + "</td>");
out.println("<td>"+ attributes.get("SenderId") + "</td>");
out.println("<td><form action=\"/webui/cqsuser/message/?userId="+user.getUserId()+"&queueName="+queueName+"&receiptHandle="+receivedMessage.getReceiptHandle()+"\" method=POST><input type='submit' value='Delete' name='Delete'/><input type='hidden' name='queueUrl' value='"+ queueUrl+ "' /></form></td></tr>");
out.println("</table>");

代码示例来源:origin: awsdocs/aws-doc-sdk-examples

public static void changeMessageVisibilitySingle(
    String queue_url, int timeout)
{
  AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
  // Get the receipt handle for the first message in the queue.
  String receipt = sqs.receiveMessage(queue_url)
            .getMessages()
            .get(0)
            .getReceiptHandle();
  sqs.changeMessageVisibility(queue_url, receipt, timeout);
}

代码示例来源:origin: com.github.davidmoten/rxjava2-aws

static Optional<SqsMessage> getNextMessage(Message message, String queueUrl, Optional<String> bucketName,
    Optional<AmazonS3> s3, AmazonSQS sqs, Service service) {
  if (bucketName.isPresent()) {
    final String s3Id = message.getBody();
    if (!s3.get().doesObjectExist(bucketName.get(), s3Id)) {
      sqs.deleteMessage(queueUrl, message.getReceiptHandle());
      return Optional.empty();
    } else {
      final S3Object object = s3.get().getObject(bucketName.get(), s3Id);
      final byte[] content = readAndClose(object.getObjectContent());
      final long timestamp = object.getObjectMetadata().getLastModified().getTime();
      final SqsMessage mb = new SqsMessage(message.getReceiptHandle(), content, timestamp, Optional.of(s3Id),
          service);
      return Optional.of(mb);
    }
  } else {
    final SqsMessage mb = new SqsMessage(message.getReceiptHandle(),
        message.getBody().getBytes(StandardCharsets.UTF_8), System.currentTimeMillis(), Optional.empty(),
        service);
    return Optional.of(mb);
  }
}

代码示例来源:origin: awslabs/amazon-sqs-java-extended-client-lib

MessageAttributeValue largePayloadAttributeValue = message.getMessageAttributes().get(
    SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME);
if (largePayloadAttributeValue != null) {
  String messageBody = message.getBody();
  LOG.info("S3 object read, Bucket name: " + s3MsgBucketName + ", Object key: " + s3MsgKey + ".");
  message.setBody(origMsgBody);
  message.getMessageAttributes().remove(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME);
  String modifiedReceiptHandle = embedS3PointerInReceiptHandle(message.getReceiptHandle(),
      s3MsgBucketName, s3MsgKey);
  message.setReceiptHandle(modifiedReceiptHandle);

代码示例来源:origin: awsdocs/aws-doc-sdk-examples

public static void changeMessageVisibilityMultiple(
    String queue_url, int timeout)
{
  AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
  List<ChangeMessageVisibilityBatchRequestEntry> entries =
    new ArrayList<ChangeMessageVisibilityBatchRequestEntry>();
  entries.add(new ChangeMessageVisibilityBatchRequestEntry(
        "unique_id_msg1",
        sqs.receiveMessage(queue_url)
          .getMessages()
          .get(0)
          .getReceiptHandle())
      .withVisibilityTimeout(timeout));
  entries.add(new ChangeMessageVisibilityBatchRequestEntry(
        "unique_id_msg2",
        sqs.receiveMessage(queue_url)
          .getMessages()
          .get(0)
          .getReceiptHandle())
      .withVisibilityTimeout(timeout + 200));
  sqs.changeMessageVisibilityBatch(queue_url, entries);
}

代码示例来源:origin: aws/aws-sdk-java

/**
 * Nacks and clears all messages remaining in the batch.
 */
synchronized void clear() {
  if (!open) {
    throw new IllegalStateException("batch is not open");
  }
  if (!isExpired()) {
    ChangeMessageVisibilityBatchRequest batchRequest = new ChangeMessageVisibilityBatchRequest()
        .withQueueUrl(qUrl);
    ResultConverter.appendUserAgent(batchRequest, AmazonSQSBufferedAsyncClient.USER_AGENT);
    List<ChangeMessageVisibilityBatchRequestEntry> entries = new ArrayList<ChangeMessageVisibilityBatchRequestEntry>(
        messages.size());
    int i = 0;
    for (Message m : messages) {
      entries.add(new ChangeMessageVisibilityBatchRequestEntry().withId(Integer.toString(i))
          .withReceiptHandle(m.getReceiptHandle()).withVisibilityTimeout(0));
      ++i;
    }
    try {
      batchRequest.setEntries(entries);
      sqsClient.changeMessageVisibilityBatch(batchRequest);
    } catch (AmazonClientException e) {
      // Log and ignore.
      log.warn("ReceiveMessageBatchTask: changeMessageVisibility failed " + e);
    }
  }
  messages.clear();
}

代码示例来源:origin: io.macgyver.rx-aws/rx-aws

protected void deleteMessageIfNecessary(Message m) {
  if (isAutoDeleteEnabled()) {
    if (logger.isDebugEnabled()) {
      logger.debug("deleting message: {}", m.getReceiptHandle());
    }
    client.deleteMessageAsync(getQueueUrl(), m.getReceiptHandle());
  }
}

相关文章