Amazon SQS examples using SDK for SAP ABAP - AWS SDK for SAP ABAP

Amazon SQS examples using SDK for SAP ABAP

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for SAP ABAP with Amazon SQS.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Scenarios are code examples that show you how to accomplish specific tasks by calling multiple functions within a service or combined with other AWS services.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Actions

The following code example shows how to use CreateQueue.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

Create an Amazon SQS standard queue.

TRY. oo_result = lo_sqs->createqueue( iv_queuename = iv_queue_name ). " oo_result is returned for testing purposes. " MESSAGE 'SQS queue created.' TYPE 'I'. CATCH /aws1/cx_sqsqueuedeldrecently. MESSAGE 'After deleting a queue, wait 60 seconds before creating another queue with the same name.' TYPE 'E'. CATCH /aws1/cx_sqsqueuenameexists. MESSAGE 'A queue with this name already exists.' TYPE 'E'. ENDTRY.

Create an Amazon SQS queue that waits for a message to arrive.

TRY. DATA lt_attributes TYPE /aws1/cl_sqsqueueattrmap_w=>tt_queueattributemap. DATA ls_attribute TYPE /aws1/cl_sqsqueueattrmap_w=>ts_queueattributemap_maprow. ls_attribute-key = 'ReceiveMessageWaitTimeSeconds'. " Time in seconds for long polling, such as how long the call waits for a message to arrive in the queue before returning. " ls_attribute-value = NEW /aws1/cl_sqsqueueattrmap_w( iv_value = iv_wait_time ). INSERT ls_attribute INTO TABLE lt_attributes. oo_result = lo_sqs->createqueue( " oo_result is returned for testing purposes. " iv_queuename = iv_queue_name it_attributes = lt_attributes ). MESSAGE 'SQS queue created.' TYPE 'I'. CATCH /aws1/cx_sqsqueuedeldrecently. MESSAGE 'After deleting a queue, wait 60 seconds before creating another queue with the same name.' TYPE 'E'. CATCH /aws1/cx_sqsqueuenameexists. MESSAGE 'A queue with this name already exists.' TYPE 'E'. ENDTRY.
  • For API details, see CreateQueue in AWS SDK for SAP ABAP API reference.

The following code example shows how to use DeleteQueue.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. lo_sqs->deletequeue( iv_queueurl = iv_queue_url ). MESSAGE 'SQS queue deleted' TYPE 'I'. ENDTRY.
  • For API details, see DeleteQueue in AWS SDK for SAP ABAP API reference.

The following code example shows how to use GetQueueUrl.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. oo_result = lo_sqs->getqueueurl( iv_queuename = iv_queue_name ). " oo_result is returned for testing purposes. " MESSAGE 'Queue URL retrieved.' TYPE 'I'. CATCH /aws1/cx_sqsqueuedoesnotexist. MESSAGE 'The requested queue does not exist.' TYPE 'E'. ENDTRY.
  • For API details, see GetQueueUrl in AWS SDK for SAP ABAP API reference.

The following code example shows how to use ListQueues.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. oo_result = lo_sqs->listqueues( ). " oo_result is returned for testing purposes. " MESSAGE 'Retrieved list of queues.' TYPE 'I'. ENDTRY.
  • For API details, see ListQueues in AWS SDK for SAP ABAP API reference.

The following code example shows how to use ReceiveMessage.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

Receive a message from an Amazon SQS queue.

TRY. oo_result = lo_sqs->receivemessage( iv_queueurl = iv_queue_url ). " oo_result is returned for testing purposes. " DATA(lt_messages) = oo_result->get_messages( ). MESSAGE 'Message received from SQS queue.' TYPE 'I'. CATCH /aws1/cx_sqsoverlimit. MESSAGE 'Maximum number of in-flight messages reached.' TYPE 'E'. ENDTRY.

Receive a message from an Amazon SQS queue using long-poll support.

TRY. oo_result = lo_sqs->receivemessage( " oo_result is returned for testing purposes. " iv_queueurl = iv_queue_url iv_waittimeseconds = iv_wait_time " Time in seconds for long polling, such as how long the call waits for a message to arrive in the queue before returning. " ). DATA(lt_messages) = oo_result->get_messages( ). MESSAGE 'Message received from SQS queue.' TYPE 'I'. CATCH /aws1/cx_sqsoverlimit. MESSAGE 'Maximum number of in-flight messages reached.' TYPE 'E'. ENDTRY.
  • For API details, see ReceiveMessage in AWS SDK for SAP ABAP API reference.

The following code example shows how to use SendMessage.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

TRY. oo_result = lo_sqs->sendmessage( " oo_result is returned for testing purposes. " iv_queueurl = iv_queue_url iv_messagebody = iv_message ). MESSAGE 'Message sent to SQS queue.' TYPE 'I'. CATCH /aws1/cx_sqsinvalidmsgconts. MESSAGE 'Message contains non-valid characters.' TYPE 'E'. CATCH /aws1/cx_sqsunsupportedop. MESSAGE 'Operation not supported.' TYPE 'E'. ENDTRY.
  • For API details, see SendMessage in AWS SDK for SAP ABAP API reference.

Scenarios

The following code example shows how to create and publish to a FIFO Amazon SNS topic.

SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

Create a FIFO topic, subscribe an Amazon SQS FIFO queue to the topic, and publish a message to an Amazon SNS topic.

" Creates a FIFO topic. " DATA lt_tpc_attributes TYPE /aws1/cl_snstopicattrsmap_w=>tt_topicattributesmap. DATA ls_tpc_attributes TYPE /aws1/cl_snstopicattrsmap_w=>ts_topicattributesmap_maprow. ls_tpc_attributes-key = 'FifoTopic'. ls_tpc_attributes-value = NEW /aws1/cl_snstopicattrsmap_w( iv_value = 'true' ). INSERT ls_tpc_attributes INTO TABLE lt_tpc_attributes. TRY. DATA(lo_create_result) = lo_sns->createtopic( iv_name = iv_topic_name it_attributes = lt_tpc_attributes ). DATA(lv_topic_arn) = lo_create_result->get_topicarn( ). ov_topic_arn = lv_topic_arn. " ov_topic_arn is returned for testing purposes. " MESSAGE 'FIFO topic created' TYPE 'I'. CATCH /aws1/cx_snstopiclimitexcdex. MESSAGE 'Unable to create more topics. You have reached the maximum number of topics allowed.' TYPE 'E'. ENDTRY. " Subscribes an endpoint to an Amazon Simple Notification Service (Amazon SNS) topic. " " Only Amazon Simple Queue Service (Amazon SQS) FIFO queues can be subscribed to an SNS FIFO topic. " TRY. DATA(lo_subscribe_result) = lo_sns->subscribe( iv_topicarn = lv_topic_arn iv_protocol = 'sqs' iv_endpoint = iv_queue_arn ). DATA(lv_subscription_arn) = lo_subscribe_result->get_subscriptionarn( ). ov_subscription_arn = lv_subscription_arn. " ov_subscription_arn is returned for testing purposes. " MESSAGE 'SQS queue was subscribed to SNS topic.' TYPE 'I'. CATCH /aws1/cx_snsnotfoundexception. MESSAGE 'Topic does not exist.' TYPE 'E'. CATCH /aws1/cx_snssubscriptionlmte00. MESSAGE 'Unable to create subscriptions. You have reached the maximum number of subscriptions allowed.' TYPE 'E'. ENDTRY. " Publish message to SNS topic. " TRY. DATA lt_msg_attributes TYPE /aws1/cl_snsmessageattrvalue=>tt_messageattributemap. DATA ls_msg_attributes TYPE /aws1/cl_snsmessageattrvalue=>ts_messageattributemap_maprow. ls_msg_attributes-key = 'Importance'. ls_msg_attributes-value = NEW /aws1/cl_snsmessageattrvalue( iv_datatype = 'String' iv_stringvalue = 'High' ). INSERT ls_msg_attributes INTO TABLE lt_msg_attributes. DATA(lo_result) = lo_sns->publish( iv_topicarn = lv_topic_arn iv_message = 'The price of your mobile plan has been increased from $19 to $23' iv_subject = 'Changes to mobile plan' iv_messagegroupid = 'Update-2' iv_messagededuplicationid = 'Update-2.1' it_messageattributes = lt_msg_attributes ). ov_message_id = lo_result->get_messageid( ). " ov_message_id is returned for testing purposes. " MESSAGE 'Message was published to SNS topic.' TYPE 'I'. CATCH /aws1/cx_snsnotfoundexception. MESSAGE 'Topic does not exist.' TYPE 'E'. ENDTRY.