List of usage examples for com.amazonaws.services.sqs.model SendMessageRequest SendMessageRequest
public SendMessageRequest(String queueUrl, String messageBody)
From source file:com.amazon.sqs.javamessaging.SQSMessageProducer.java
License:Open Source License
void sendInternal(Queue queue, Message message) throws JMSException { checkClosed();/* w ww. j a va 2 s .co m*/ String sqsMessageBody = null; String messageType = null; if (message instanceof SQSMessage) { 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; } } else { throw new MessageFormatException( "Unrecognized message type. Messages have to be one of: SQSBytesMessage, SQSObjectMessage, or SQSTextMessage"); } if (sqsMessageBody == null || sqsMessageBody.isEmpty()) { throw new JMSException("Message body cannot be null or empty"); } Map<String, MessageAttributeValue> messageAttributes = propertyToMessageAttribute((SQSMessage) message); addMessageTypeReservedAttribute(messageAttributes, (SQSMessage) message, messageType); SendMessageRequest sendMessageRequest = new SendMessageRequest(((SQSQueueDestination) queue).getQueueUrl(), sqsMessageBody); sendMessageRequest.setMessageAttributes(messageAttributes); String messageId = amazonSQSClient.sendMessage(sendMessageRequest).getMessageId(); LOG.info("Message sent to SQS with SQS-assigned messageId: " + messageId); /** TODO: Do not support disableMessageID for now.*/ message.setJMSMessageID(String.format(SQSMessagingClientConstants.MESSAGE_ID_FORMAT, messageId)); ((SQSMessage) message).setSQSMessageId(messageId); }
From source file:com.athena.sqs.MessageDispatcher.java
License:Apache License
/** * Send message to amazon sqs/*from ww w . ja v a 2 s.c o m*/ * @param queueName * @param messages * @throws MessageException */ public void doSend(String queueName, String jsonString) throws MessageException { String transactionId = UUID.randomUUID().toString(); try { logger.debug("Getting Queue URL from Amazon [" + queueName + "]"); String queueUrl = messageContext.getQueue(queueName); logger.debug("Sending a message to [" + queueName + "][" + queueUrl + "]"); // if message is small enough to be sent as one message, do it if (jsonString.getBytes(MessageSplitter.UTF_8).length <= MessageSplitter.SQS_MAX_MESSAGE_SIZE) { String header = makeHeader(MessageTransferType.JSON, "athena", transactionId, true, 1, 1); logger.debug("This is smaller message"); logger.debug("[HEADER] : " + header); String singleMessage = header + encoder.encodeBuffer(jsonString.getBytes()); client.sendMessage(new SendMessageRequest(queueUrl, singleMessage)); logger.debug("Single message sent successfully"); } else { logger.debug("This is larger than " + MessageSplitter.SQS_MAX_MESSAGE_SIZE); List<String> messageList = MessageSplitter.split(encoder.encodeBuffer(jsonString.getBytes())); int current = 1; int total = messageList.size(); String header = null; String chunkedMessage = null; for (String target : messageList) { header = makeHeader(MessageTransferType.JSON, "athena", transactionId, false, current++, total); chunkedMessage = header + target; client.sendMessage(new SendMessageRequest(queueUrl, chunkedMessage)); logger.debug(chunkedMessage); } logger.debug("Complex message sent successfully"); } } catch (AmazonServiceException ase) { logger.error("Caught an AmazonServiceException, which means your request made it " + "to Amazon SQS, but was rejected with an error response for some reason."); logger.error("Error Message: " + ase.getMessage()); logger.error("HTTP Status Code: " + ase.getStatusCode()); logger.error("AWS Error Code: " + ase.getErrorCode()); logger.error("Error Type: " + ase.getErrorType()); logger.error("Request ID: " + ase.getRequestId()); throw new MessageException( MessageFormat.format(MessageErrors.AMAZON_ERROR.getDescription(), ase.getMessage())); } catch (AmazonClientException ace) { logger.error("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with SQS, such as not " + "being able to access the network."); logger.error("Error Message: " + ace.getMessage()); throw new MessageException( MessageFormat.format(MessageErrors.AMAZON_ERROR.getDescription(), ace.getMessage())); } catch (IOException ioe) { throw new MessageException( MessageFormat.format(MessageErrors.INTERNAL_ERROR.getDescription(), ioe.getMessage())); } catch (Exception e) { throw new MessageException( MessageFormat.format(MessageErrors.INTERNAL_ERROR.getDescription(), e.getMessage())); } finally { } }
From source file:com.blacklocus.qs.worker.aws.AmazonSQSAsyncPutTaskService.java
License:Apache License
public void putTask(QSTaskModel task) { try {/*w w w. ja v a 2 s . com*/ LOG.info("Queueing task: {}", task); String messageBody = objectMapper.writeValueAsString(task); sqs.sendMessage(new SendMessageRequest(queueUrl, messageBody)); LOG.debug("Queued to {}, message\n\t{}", queueUrl, messageBody); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.blacklocus.qs.worker.aws.AmazonSQSLogService.java
License:Apache License
@Override public void log(QSLogModel log) { sqs.sendMessage(new SendMessageRequest(queueUrl, thing(log))); }
From source file:com.blacklocus.qs.worker.aws.AmazonSQSTaskService.java
License:Apache License
@Override public void putTask(QSTaskModel task) { try {/*from w w w . j a v a 2s . co m*/ LOG.info("Queueing task: {}", task); String messageBody = objectMapper.writeValueAsString(task); sqs.sendMessage(new SendMessageRequest(queueUrl, messageBody)); LOG.debug("Queued to {}, message\n\t{}", queueUrl, messageBody); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.blacklocus.qs.worker.aws.AmazonSQSTaskService.java
License:Apache License
@Override public void resetTask(QSTaskModel task) { try {//from w w w . j a v a2s .c o m // Re-queue the task in its current state - remainingAttempts has been changed by the WorkerQueueItemHandler. String message = objectMapper.writeValueAsString(task); sqs.sendMessage(new SendMessageRequest(queueUrl, message)); // Remove the currently in-flight task. closeTask(task); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.boundary.aws.sqs.Sample.java
License:Open Source License
public static void main(String[] args) throws Exception { String queueName = "boundary-sqs-demo-queue"; /*//from w w w . j ava 2 s . c o m * The ProfileCredentialsProvider will return your [default] credential * profile by reading from the credentials file located at * (HOME/.aws/credentials). */ AWSCredentials credentials = null; try { credentials = new ProfileCredentialsProvider("default").getCredentials(); } catch (Exception e) { throw new AmazonClientException("Cannot load the credentials from the credential profiles file. ", e); } AmazonSQS sqs = new AmazonSQSClient(credentials); Region usWest2 = Region.getRegion(Regions.US_WEST_2); sqs.setRegion(usWest2); try { // Create a queue System.out.printf("Creating queue: %s.\n", queueName); CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName); String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl(); int messageCount = 100; // Send a messages for (int count = 1; count <= messageCount; count++) { System.out.printf("Sending message %3d to %s.\n", count, queueName); sqs.sendMessage(new SendMessageRequest(myQueueUrl, new Date() + ": This is my message text.")); } for (int count = 1; count <= messageCount; count++) { ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl); List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages(); for (Message msg : messages) { System.out.printf("Received message: %s queue: %s body: %s\n", msg.getMessageId(), queueName, msg.getBody()); System.out.printf("Deleting message: %s queue: %s\n", msg.getMessageId(), queueName); String messageRecieptHandle = msg.getReceiptHandle(); sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageRecieptHandle)); } } System.out.printf("Deleting queue: %s\n", queueName); sqs.deleteQueue(new DeleteQueueRequest(myQueueUrl)); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon SQS, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with SQS, such as not " + "being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } sqs.shutdown(); }
From source file:com.clicktravel.infrastructure.messaging.aws.sqs.SqsQueueResource.java
License:Apache License
/** * Sends a message to the AWS SQS queue/*from w w w . j a v a 2 s . com*/ * @param messageBody Body of the message to send */ public void sendMessage(final String messageBody) throws AmazonClientException { final SendMessageRequest sendMessageRequest = new SendMessageRequest(queueUrl, messageBody); doSendRequest(sendMessageRequest); }
From source file:com.clicktravel.infrastructure.messaging.aws.sqs.SqsQueueResource.java
License:Apache License
/** * Sends a delayed message to the AWS SQS queue * @param messageBody Body of the message to send * @param delaySeconds Number of seconds to delay visibility of the sent message *//*ww w .j a v a 2s . c om*/ public void sendDelayedMessage(final String messageBody, final int delaySeconds) throws AmazonClientException { final SendMessageRequest sendMessageRequest = new SendMessageRequest(queueUrl, messageBody) .withDelaySeconds(delaySeconds); doSendRequest(sendMessageRequest); }
From source file:com.cloud.Assignment.QueueSender.java
License:Open Source License
public boolean sendToQueue(String queue, String message) throws Exception { Properties props = new Properties(); AWSCredentials credentials = null;/* ww w . ja v a 2s . co m*/ String myQueueUrl = null; System.out.println("Queue :" + queue + " Message :" + message); try { credentials = new ClasspathPropertiesFileCredentialsProvider("AwsCredentials.properties") .getCredentials(); ClassLoader loader = Thread.currentThread().getContextClassLoader(); props.load(loader.getResourceAsStream("/QueueURL.properties")); } catch (Exception e) { throw new AmazonClientException("Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (/Users/prabhus/.aws/credentials), and is in valid format.", e); } AmazonSQS sqs = new AmazonSQSClient(credentials); Region usWest2 = Region.getRegion(Regions.US_EAST_1); sqs.setRegion(usWest2); try { myQueueUrl = props.getProperty(queue); System.out.println("URL :" + myQueueUrl); System.out.println("Sending " + message); sqs.sendMessage(new SendMessageRequest(myQueueUrl, message)); isMessagePosted = true; } catch (AmazonServiceException ase) { throw new AmazonServiceException("Caught an AmazonServiceException, which means your request made it " + "to Amazon SQS, but was rejected with an error response for some reason."); } catch (AmazonClientException ace) { System.out.println("Error Message: " + ace.getMessage()); throw new AmazonClientException("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with SQS, such as not " + "being able to access the network."); } return isMessagePosted; }