[[AWS-SQS-SQSComponent]]
SQS Component
~~~~~~~~~~~~~

*Available as of Camel 2.6*

The sqs component supports sending and receiving messages to
http://aws.amazon.com/sqs[Amazon's SQS] service.

Prerequisites

You must have a valid Amazon Web Services developer account, and be
signed up to use Amazon SQS. More information are available at
http://aws.amazon.com/sqs[Amazon SQS].

[[AWS-SQS-URIFormat]]
URI Format
^^^^^^^^^^

[source,java]
------------------------------
aws-sqs://queue-name[?options]
------------------------------

The queue will be created if they don't already exists. +
 You can append query options to the URI in the following format,
?options=value&option2=value&...

[[AWS-SQS-URIOptions]]
URI Options
^^^^^^^^^^^

[width="100%",cols="10%,10%,10%,70%",options="header",]
|=======================================================================
|Name |Default Value |Context |Description

|amazonSQSClient |`null` |Shared |Reference to a `com.amazonaws.services.sqs.AmazonSQS` in the
link:registry.html[Registry].

|accessKey |`null` |Shared |Amazon AWS Access Key

|secretKey |`null` |Shared |Amazon AWS Secret Key

|amazonSQSEndpoint |`null` |Shared |The region with which the AWS-SQS client wants to work with. Only works
if Camel creates the AWS-SQS client, i.e., if you explicitly set
amazonSQSClient, then this setting will have no effect. You would have
to set it on the client you create directly

|attributeNames |`null` |Consumer |A list of attributes to set in the
`com.amazonaws.services.sqs.model.ReceiveMessageRequest`.

|concurrentConsumers |1 |Consumer |(*as of 2.15.0*) Allows you to use multiple threads to poll the sqs
queue to increase throughput

|defaultVisibilityTimeout |`null` |Shared |The visibility timeout (in seconds) to set in the
`com.amazonaws.services.sqs.model.CreateQueueRequest`.

|deleteAfterRead |`true` |Consumer |Delete message from SQS after it has been read (and processed by the
route)

|deleteIfFiltered |`true` |Consumer |*Camel 2.12.2,2.13.0* Whether or not to send the DeleteMessage to the
SQS queue if an exchange fails to get through a filter. If 'false' and
exchange does not make it through a Camel filter upstream in the route,
then don't send DeleteMessage.

|maxMessagesPerPoll |`null` |Consumer |The maximum number of messages which can be received in one poll to set
in the `com.amazonaws.services.sqs.model.ReceiveMessageRequest`.

|visibilityTimeout |`null` |Shared |The duration (in seconds) that the received messages are hidden from
subsequent retrieve requests after being retrieved by a ReceiveMessage
request. This only make sense if its different from
`defaultVisibilityTimeout`.

|extendMessageVisibility |`false` |Consumer |*Camel 2.10:* If enabled a scheduled background task will keep extending
the message visibility on SQS. This is needed if it takes a long time to
process the message. If set to true` visibilityTimeout` must be set. See
details at
http://docs.amazonwebservices.com/AWSSimpleQueueService/latest/APIReference/Query_QueryChangeMessageVisibility.html[Amazon
docs].

|maximumMessageSize |`null` |Shared |*Camel 2.8:* The maximumMessageSize (in bytes) an SQS message can
contain for this queue, to set in the
`com.amazonaws.services.sqs.model.SetQueueAttributesRequest`.

|messageRetentionPeriod |`null` |Shared |*Camel 2.8:* The messageRetentionPeriod (in seconds) a message will be
retained by SQS for this queue, to set in the
`com.amazonaws.services.sqs.model.SetQueueAttributesRequest`.

|policy |`null` |Shared |*Camel 2.8:* The policy for this queue to set in the
`com.amazonaws.services.sqs.model.SetQueueAttributesRequest`.

|delaySeconds |`null` |Producer |*Camel 2.9.3:* Delay sending messages for a number of seconds.

|waitTimeSeconds |`0` |Producer |*Camel 2.11:* Duration in seconds (0 to 20) that the ReceiveMessage
action call will wait until a message is in the queue to include in the
response.

|receiveMessageWaitTimeSeconds |`0` |Shared |*Camel 2.11:* If you do not specify WaitTimeSeconds in the request, the
queue attribute ReceiveMessageWaitTimeSeconds is used to determine how
long to wait.

|queueOwnerAWSAccountId |`null` |Shared |*Camel 2.12:* Specify the queue owner aws account id when you need to
connect the queue with different account owner.

|region |`null` |Shared |*Camel 2.12.3:* Specify the queue region which could be used with
`queueOwnerAWSAccountId` to build the service URL.

|redrivePolicy |`null` |Shared |*Camel 2.15*: Specify the policy that send message to DeadLetter queue.
See detail at
http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sqs-queues-redrivepolicy.html[Amazon
docs].

|proxyHost |`null` |Shared |*Camel 2.16*: Specify a proxy host to be used inside the client
definition.

|proxyPort |`null` |Shared |*Camel 2.16*: Specify a proxy port to be used inside the client
definition.
|=======================================================================

Required SQS component options

You have to provide the amazonSQSClient in the
link:registry.html[Registry] or your accessKey and secretKey to access
the http://aws.amazon.com/sqs[Amazon's SQS].

[[AWS-SQS-BatchConsumer]]
Batch Consumer
^^^^^^^^^^^^^^

This component implements the link:batch-consumer.html[Batch Consumer].

This allows you for instance to know how many messages exists in this
batch and for instance let the link:aggregator.html[Aggregator]
aggregate this number of messages.

[[AWS-SQS-Usage]]
Usage
^^^^^

[[AWS-SQS-MessageheaderssetbytheSQSproducer]]
Message headers set by the SQS producer
+++++++++++++++++++++++++++++++++++++++

[width="100%",cols="10%,10%,80%",options="header",]
|=======================================================================
|Header |Type |Description

|`CamelAwsSqsMD5OfBody` |`String` |The MD5 checksum of the Amazon SQS message.

|`CamelAwsSqsMessageId` |`String` |The Amazon SQS message ID.

|`CamelAwsSqsDelaySeconds` |`Integer` |Since *Camel 2.11*, the delay seconds that the Amazon SQS message can be
see by others.
|=======================================================================

[[AWS-SQS-MessageheaderssetbytheSQSconsumer]]
Message headers set by the SQS consumer
+++++++++++++++++++++++++++++++++++++++

[width="100%",cols="10%,10%,80%",options="header",]
|=======================================================================
|Header |Type |Description

|`CamelAwsSqsMD5OfBody` |`String` |The MD5 checksum of the Amazon SQS message.

|`CamelAwsSqsMessageId` |`String` |The Amazon SQS message ID. 

|`CamelAwsSqsReceiptHandle` |`String` |The Amazon SQS message receipt handle.

|`CamelAwsSqsAttributes` |`Map<String, String>` |The Amazon SQS message attributes.
|=======================================================================

[[AWS-SQS-AdvancedAmazonSQSconfiguration]]
Advanced AmazonSQS configuration
++++++++++++++++++++++++++++++++

If your Camel Application is running behind a firewall or if you need to
have more control over the AmazonSQS instance configuration, you can
create your own instance:

[source,java]
--------------------------------------------------------------------------------------
AWSCredentials awsCredentials = new BasicAWSCredentials("myAccessKey", "mySecretKey");

ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setProxyHost("http://myProxyHost");
clientConfiguration.setProxyPort(8080);

AmazonSQS client = new AmazonSQSClient(awsCredentials, clientConfiguration);

registry.bind("client", client);
--------------------------------------------------------------------------------------

and refer to it in your Camel aws-sqs component configuration:

[source,java]
---------------------------------------------------------------------------------
from("aws-sqs://MyQueue?amazonSQSClient=#client&delay=5000&maxMessagesPerPoll=5")
.to("mock:result");
---------------------------------------------------------------------------------

[[AWS-SQS-Dependencies]]
Dependencies
^^^^^^^^^^^^

Maven users will need to add the following dependency to their pom.xml.

*pom.xml*

[source,xml]
---------------------------------------
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-aws</artifactId>
    <version>${camel-version}</version>
</dependency>
---------------------------------------

where `${camel-version`} must be replaced by the actual version of Camel
(2.6 or higher).

[[AWS-SQS-JMS-styleSelectors]]
JMS-style Selectors
^^^^^^^^^^^^^^^^^^^

SQS does not allow selectors, but you can effectively achieve this by
using the link:message-filter.html[Camel Filter EIP] and setting an
appropriate `visibilityTimeout`. When SQS dispatches a message, it will
wait up to the visibility timeout before it will try to dispatch the
message to a different consumer unless a DeleteMessage is received. By
default, Camel will always send the DeleteMessage at the end of the
route, unless the route ended in failure. To achieve appropriate
filtering and not send the DeleteMessage even on successful completion
of the route, use a Filter:

[source,java]
------------------------------------------------------------------------------------------------------
from("aws-sqs://MyQueue?amazonSQSClient=#client&defaultVisibilityTimeout=5000&deleteIfFiltered=false")
.filter("${header.login} == true")
.to("mock:result");
------------------------------------------------------------------------------------------------------

In the above code, if an exchange doesn't have an appropriate header, it
will not make it through the filter AND also not be deleted from the SQS
queue. After 5000 miliseconds, the message will become visible to other
consumers.

[[AWS-SQS-SeeAlso]]
See Also
^^^^^^^^

* link:configuring-camel.html[Configuring Camel]
* link:component.html[Component]
* link:endpoint.html[Endpoint]
* link:getting-started.html[Getting Started]

* link:aws.html[AWS Component]

