Java Code Examples for com.amazonaws.services.sqs.AmazonSQS#sendMessage()

The following examples show how to use com.amazonaws.services.sqs.AmazonSQS#sendMessage() . 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: IntegrationTest.java    From amazon-sqs-java-temporary-queues-client with Apache License 2.0 6 votes vote down vote up
protected AmazonSQS getBuddyPrincipalClient() {
    AWSCredentialsProvider credentialsProvider = getBuddyCredentials();
    AmazonSQS client = AmazonSQSClientBuilder.standard()
            .withRegion("us-west-2")
            .withCredentials(credentialsProvider)
            .build();

    // Assume that the principal is not able to send messages to arbitrary queues
    String queueUrl = sqs.createQueue(queueNamePrefix + "TestQueue").getQueueUrl();
    try {
        client.sendMessage(queueUrl, "Haxxors!!");
        assumeTrue("The buddy credentials should not authorize sending to arbitrary queues", false);
    } catch (AmazonSQSException e) {
        // Access Denied
        assumeThat(e.getStatusCode(), equalTo(403));
    } finally {
        sqs.deleteQueue(queueUrl);
    }

    return client;
}
 
Example 2
Source File: LocalstackContainerTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void sqsTestOverBridgeNetwork() {
    AmazonSQS sqs = AmazonSQSClientBuilder.standard()
        .withEndpointConfiguration(localstack.getEndpointConfiguration(SQS))
        .withCredentials(localstack.getDefaultCredentialsProvider())
        .build();

    CreateQueueResult queueResult = sqs.createQueue("baz");
    String fooQueueUrl = queueResult.getQueueUrl();
    assertThat("Created queue has external hostname URL", fooQueueUrl,
        containsString("http://" + DockerClientFactory.instance().dockerHostIpAddress() + ":" + localstack.getMappedPort(SQS.getPort())));

    sqs.sendMessage(fooQueueUrl, "test");
    final long messageCount = sqs.receiveMessage(fooQueueUrl).getMessages().stream()
        .filter(message -> message.getBody().equals("test"))
        .count();
    assertEquals("the sent message can be received", 1L, messageCount);
}
 
Example 3
Source File: VisibilityTimeout.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)
{
    final String queue_name = "testQueue" + new Date().getTime();
    AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();

    // first, create a queue (unless it exists already)
    try {
        CreateQueueResult cq_result = sqs.createQueue(queue_name);
    } catch (AmazonSQSException e) {
        if (!e.getErrorCode().equals("QueueAlreadyExists")) {
            throw e;
        }
    }

    final String queue_url = sqs.getQueueUrl(queue_name).getQueueUrl();

    // Send some messages to the queue
    for (int i = 0; i < 20; i++) {
        sqs.sendMessage(queue_url, "This is message " + i);
    }

    // change visibility timeout (single)
    changeMessageVisibilitySingle(queue_url, 60 * 60); //1 hour

    // change visibility timeout (multiple)
    changeMessageVisibilityMultiple(queue_url, 30 * 60 ); //30 minutes
}
 
Example 4
Source File: SendReceiveMessages.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)
{
    final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();

    try {
        CreateQueueResult create_result = sqs.createQueue(QUEUE_NAME);
    } catch (AmazonSQSException e) {
        if (!e.getErrorCode().equals("QueueAlreadyExists")) {
            throw e;
        }
    }

    String queueUrl = sqs.getQueueUrl(QUEUE_NAME).getQueueUrl();

    SendMessageRequest send_msg_request = new SendMessageRequest()
            .withQueueUrl(queueUrl)
            .withMessageBody("hello world")
            .withDelaySeconds(5);
    sqs.sendMessage(send_msg_request);


    // Send multiple messages to the queue
    SendMessageBatchRequest send_batch_request = new SendMessageBatchRequest()
            .withQueueUrl(queueUrl)
            .withEntries(
                    new SendMessageBatchRequestEntry(
                            "msg_1", "Hello from message 1"),
                    new SendMessageBatchRequestEntry(
                            "msg_2", "Hello from message 2")
                            .withDelaySeconds(10));
    sqs.sendMessageBatch(send_batch_request);

    // receive messages from the queue
    List<Message> messages = sqs.receiveMessage(queueUrl).getMessages();

    // delete messages from the queue
    for (Message m : messages) {
        sqs.deleteMessage(queueUrl, m.getReceiptHandle());
    }
}
 
Example 5
Source File: SqsIOTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testRead() {
  final AmazonSQS client = embeddedSqsRestServer.getClient();
  final String queueUrl = embeddedSqsRestServer.getQueueUrl();

  final PCollection<Message> output =
      pipeline.apply(SqsIO.read().withQueueUrl(queueUrl).withMaxNumRecords(100));

  PAssert.thatSingleton(output.apply(Count.globally())).isEqualTo(100L);

  for (int i = 0; i < 100; i++) {
    client.sendMessage(queueUrl, "This is a test");
  }
  pipeline.run();
}
 
Example 6
Source File: SqsOperationsImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
@Override
public SendMessageResult sendMessage(String queueName, String messageText, Map<String, MessageAttributeValue> messageAttributes, AmazonSQS amazonSQS)
{
    try
    {
        return amazonSQS.sendMessage(new SendMessageRequest().withQueueUrl(amazonSQS.getQueueUrl(queueName).getQueueUrl()).withMessageBody(messageText)
            .withMessageAttributes(messageAttributes));
    }
    catch (QueueDoesNotExistException e)
    {
        throw new IllegalStateException(String.format("AWS SQS queue with \"%s\" name not found.", queueName), e);
    }
}
 
Example 7
Source File: AmazonSQSExtendedClientTest.java    From amazon-sqs-java-extended-client-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void testWhenSendMessageWithLargePayloadSupportDisabledThenS3IsNotUsedAndSqsBackendIsResponsibleToFailIt() {
    int messageLength = MORE_THAN_SQS_SIZE_LIMIT;
    String messageBody = generateStringWithLength(messageLength);
    ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
            .withLargePayloadSupportDisabled();
    AmazonSQS sqsExtended = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfiguration));

    SendMessageRequest messageRequest = new SendMessageRequest(SQS_QUEUE_URL, messageBody);
    sqsExtended.sendMessage(messageRequest);

    verify(mockS3, never()).putObject(isA(PutObjectRequest.class));
    verify(mockSqsBackend).sendMessage(eq(messageRequest));
}
 
Example 8
Source File: AmazonSQSExtendedClientTest.java    From amazon-sqs-java-extended-client-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void testWhenSendMessageWithAlwaysThroughS3AndMessageIsSmallThenItIsStillStoredInS3() {
    int messageLength = LESS_THAN_SQS_SIZE_LIMIT;
    String messageBody = generateStringWithLength(messageLength);
    ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
            .withLargePayloadSupportEnabled(mockS3, S3_BUCKET_NAME).withAlwaysThroughS3(true);
    AmazonSQS sqsExtended = spy(new AmazonSQSExtendedClient(mock(AmazonSQSClient.class), extendedClientConfiguration));

    SendMessageRequest messageRequest = new SendMessageRequest(SQS_QUEUE_URL, messageBody);
    sqsExtended.sendMessage(messageRequest);

    verify(mockS3, times(1)).putObject(isA(PutObjectRequest.class));
}
 
Example 9
Source File: AmazonSQSExtendedClientTest.java    From amazon-sqs-java-extended-client-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void testWhenSendMessageWithSetMessageSizeThresholdThenThresholdIsHonored() {
    int messageLength = ARBITRATY_SMALLER_THRESSHOLD * 2;
    String messageBody = generateStringWithLength(messageLength);
    ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
            .withLargePayloadSupportEnabled(mockS3, S3_BUCKET_NAME).withMessageSizeThreshold(ARBITRATY_SMALLER_THRESSHOLD);

    AmazonSQS sqsExtended = spy(new AmazonSQSExtendedClient(mock(AmazonSQSClient.class), extendedClientConfiguration));

    SendMessageRequest messageRequest = new SendMessageRequest(SQS_QUEUE_URL, messageBody);
    sqsExtended.sendMessage(messageRequest);
    verify(mockS3, times(1)).putObject(isA(PutObjectRequest.class));
}
 
Example 10
Source File: SpringCloudSQSLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenMessageSent_whenMessageReceived_thenSuccess() throws InterruptedException {
    CountDownLatch countDownLatch = new CountDownLatch(5);
    springCloudSQS.setCountDownLatch(countDownLatch);

    AmazonSQS amazonSQS = SpringCloudAwsTestUtil.amazonSQS();
    for (int i = 0; i < 5; i++) {
        amazonSQS.sendMessage(receiveQueueUrl, "Hello World " + i);
        logger.info("Sent message {}, waiting for 1 second", i + 1);
        Thread.sleep(1000L);
    }
    countDownLatch.await();
}