Amazon SQS Cloud Connector

Amazon SQS (Simple Queue Service) is a hosted and highly scalable distributed message queue service.

To get the correct version of the artifact to use in your application (since it depends on a specific version of the Amazon Web Services Java SDK), please follow the instructions detailed in Versioning.

Once deployed, a message similar to this one should be printed out to the server’s output:

[fish.payara.cloud.connectors.amazonsqs.api.inbound.AmazonSQSResourceAdapter] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1495398495490] [levelValue: 800] Amazon SQS Resource Adapter Started..

Sending messages

Sending messages to Amazon SQS can be done via the JCA and an Amazon specific API. In order to start using this API to send messages, a resource has to be defined via the JCA API; a connection factory.

The connection factory has to be given a name, which can be any name that is valid for JNDI. The java:app namespace is typically recommended to be used. The type of the connection factory to be used for Amazon SQS is fish.payara.cloud.connectors.amazonsqs.api.AmazonSQSConnectionFactory, and we have to specify the resource adapter name which is here amazon-sqs-rar-0.8.0.rar.

Finally the AWS account details have to be specified in the properties of the corresponding JMS resource definitions.

The following gives an example:

@ConnectionFactoryDefinition (
  name = "java:app/amazonsqs/factory",
  interfaceName = "fish.payara.cloud.connectors.amazonsqs.api.AmazonSQSConnectionFactory",
  resourceAdapter = "amazon-sqs-rar-0.8.0"
  properties = {"awsAccessKeyId=<accessKeyID>", "awsSecretKey=<secretKey>", "region=eu-west-2"}
)

With the above definition in place the following code shows an example of sending a message:

@Singleton
@Startup
public class SendSQSMessage {

 @Resource(lookup = "java:app/amazonsqs/factory")
 private AmazonSQSConnectionFactory factory;

 @PostConstruct
 public void init() {
    try (AmazonSQSConnection connection = factory.createConnection()) {
        connection.sendMessage(new SendMessageRequest("<queueURL>", "Hello World"));
    }
    catch (Exception ex) {
    }
 }
}

Receiving messages

Messages can be received from Amazon SQS by creating an MDB (Message Driven Bean) that implements the fish.payara.cloud.connectors.amazonsqs.api.AmazonSQSListener marker interface and has a single method annotated with @OnSQSMessage and the method signature void method(Message message).

The following gives an example:

@MessageDriven(activationConfig = {
 @ActivationConfigProperty(propertyName = "awsAccessKeyId", propertyValue = "someKey"),
 @ActivationConfigProperty(propertyName = "awsSecretKey", propertyValue = "someSecretKey"),
 @ActivationConfigProperty(propertyName = "queueURL", propertyValue = "someQueueURL"),
 @ActivationConfigProperty(propertyName = "pollInterval", propertyValue = "1"),
 @ActivationConfigProperty(propertyName = "region", propertyValue = "eu-west-2")
})
public class ReceiveSQSMessage implements AmazonSQSListener {

 @OnSQSMessage
 public void receiveMessage(Message message) {
     // Handle message
 }
}

The full list of config properties is given below:

Config Property Name Type Default Notes

awsAccessKeyId

String

None

Must be set to the access key of your AWS account

awsSecretKey

String

None

Must be set to the secret key of your AWS account

queueURL

String

None

Must be set to the URL for an SQS queue

region

String

None

Must be set to the AWS region name of your queue

maxMessages

Integer

10

The maximum number of messages to download on a poll

initialPollDelay

Integer

1

The delay (in seconds) before polling the queue after MDB activation (MDB only)

pollInterval

Integer

3

How often should the adapter poll for messages (in seconds) (MDB Only)

messageAttributeNames

String

All

The list of message attribute names that should be fetched with the message (MDB Only)

attributeNames

String

All

The list of attribute names that should be fetched with the message (MDB Only)