Java Code Examples for com.amazonaws.services.sqs.model.SendMessageRequest#setMessageGroupId()

The following examples show how to use com.amazonaws.services.sqs.model.SendMessageRequest#setMessageGroupId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SqsClient.java    From kafka-connect-sqs with Apache License 2.0 6 votes vote down vote up
/**
 * Send a message to an SQS queue.
 *
 * @param url       SQS queue url.
 * @param body      The message to send.
 * @param groupId   Optional group identifier (fifo queues only).
 * @param messageId Optional message identifier (fifo queues only).
 * @return
 */
public String send(final String url, final String body, final String groupId, final String messageId) {
  log.debug(".send: queue={}, gid={}, mid={}", url, groupId, messageId);

  Guard.verifyValidUrl(url);
  // Guard.verifyNotNullOrEmpty( body, "message body" ) ;
  if (!isValidState()) {
    throw new IllegalStateException("AmazonSQS client is not initialized");
  }
  final boolean fifo = isFifo(url);

  final SendMessageRequest request = new SendMessageRequest(url, body);
  if (fifo) {
    Guard.verifyNotNullOrEmpty(groupId, "groupId");
    Guard.verifyNotNullOrEmpty(messageId, "messageId");
    request.setMessageGroupId(groupId);
    request.setMessageDeduplicationId(messageId);
  }

  final SendMessageResult result = client.sendMessage(request);

  log.debug(".send-message.OK: queue={}, result={}", url, result);

  return fifo ? result.getSequenceNumber() : result.getMessageId();
}
 
Example 2
Source File: QueueMessageChannel.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
private SendMessageRequest prepareSendMessageRequest(Message<?> message) {
	SendMessageRequest sendMessageRequest = new SendMessageRequest(this.queueUrl,
			String.valueOf(message.getPayload()));

	if (message.getHeaders().containsKey(SqsMessageHeaders.SQS_GROUP_ID_HEADER)) {
		sendMessageRequest.setMessageGroupId(message.getHeaders()
				.get(SqsMessageHeaders.SQS_GROUP_ID_HEADER, String.class));
	}

	if (message.getHeaders()
			.containsKey(SqsMessageHeaders.SQS_DEDUPLICATION_ID_HEADER)) {
		sendMessageRequest.setMessageDeduplicationId(message.getHeaders()
				.get(SqsMessageHeaders.SQS_DEDUPLICATION_ID_HEADER, String.class));
	}

	if (message.getHeaders().containsKey(SqsMessageHeaders.SQS_DELAY_HEADER)) {
		sendMessageRequest.setDelaySeconds(message.getHeaders()
				.get(SqsMessageHeaders.SQS_DELAY_HEADER, Integer.class));
	}

	Map<String, MessageAttributeValue> messageAttributes = getMessageAttributes(
			message);
	if (!messageAttributes.isEmpty()) {
		sendMessageRequest.withMessageAttributes(messageAttributes);
	}

	return sendMessageRequest;
}
 
Example 3
Source File: SQSMessageProducer.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 4 votes vote down vote up
void sendInternal(SQSQueueDestination queue, Message rawMessage) throws JMSException {
    checkClosed();
    String sqsMessageBody = null;
    String messageType = null;
    if (!(rawMessage instanceof SQSMessage)) {
        throw new MessageFormatException(
                "Unrecognized message type. Messages have to be one of: SQSBytesMessage, SQSObjectMessage, or SQSTextMessage");            
    }
    
    SQSMessage message = (SQSMessage)rawMessage;
    message.setJMSDestination(queue);
    if (message instanceof SQSBytesMessage) {
        sqsMessageBody = Base64.encodeAsString(((SQSBytesMessage) message).getBodyAsBytes());
        messageType = SQSMessage.BYTE_MESSAGE_TYPE;
    } else if (message instanceof SQSObjectMessage) {
        sqsMessageBody = ((SQSObjectMessage) message).getMessageBody();
        messageType = SQSMessage.OBJECT_MESSAGE_TYPE;
    } else if (message instanceof SQSTextMessage) {            
        sqsMessageBody = ((SQSTextMessage) message).getText();
        messageType = SQSMessage.TEXT_MESSAGE_TYPE;
    }
    
    if (sqsMessageBody == null || sqsMessageBody.isEmpty()) {
        throw new JMSException("Message body cannot be null or empty");
    }
    Map<String, MessageAttributeValue> messageAttributes = propertyToMessageAttribute((SQSMessage) message);

    /**
     * These will override existing attributes if they exist. Everything that
     * has prefix JMS_ is reserved for JMS Provider, but if the user sets that
     * attribute, it will be overwritten.
     */
    addStringAttribute(messageAttributes, SQSMessage.JMS_SQS_MESSAGE_TYPE, messageType);
    addReplyToQueueReservedAttributes(messageAttributes, message);
    addCorrelationIDToQueueReservedAttributes(messageAttributes, message);

    SendMessageRequest sendMessageRequest = new SendMessageRequest(queue.getQueueUrl(), sqsMessageBody);
    sendMessageRequest.setMessageAttributes(messageAttributes);

    if (deliveryDelaySeconds != 0) {
        sendMessageRequest.setDelaySeconds(deliveryDelaySeconds);
    }

    //for FIFO queues, we have to specify both MessageGroupId, which we obtain from standard property JMSX_GROUP_ID
    //and MessageDeduplicationId, which we obtain from a custom provider specific property JMS_SQS_DEDUPLICATION_ID
    //notice that this code does not validate if the values are actually set by the JMS user
    //this means that failure to provide the required values will fail server side and throw a JMSException
    if (queue.isFifo()) {
        sendMessageRequest.setMessageGroupId(message.getSQSMessageGroupId());
        sendMessageRequest.setMessageDeduplicationId(message.getSQSMessageDeduplicationId());
    }

    SendMessageResult sendMessageResult = amazonSQSClient.sendMessage(sendMessageRequest);
    String messageId = sendMessageResult.getMessageId();
    LOG.info("Message sent to SQS with SQS-assigned messageId: " + messageId);
    /** TODO: Do not support disableMessageID for now. */
    message.setSQSMessageId(messageId);

    // if the message was sent to FIFO queue, the sequence number will be
    // set in the response
    // pass it to JMS user through provider specific JMS property
    if (sendMessageResult.getSequenceNumber() != null) {
        message.setSequenceNumber(sendMessageResult.getSequenceNumber());
    }
}