AmazonSQS Connector Example¶
The WSO2 Amazon SQS connector allows you to access the exposed Amazon SQS API from an integration sequence.
What you'll build¶
This example explains how to use Amazon SQS Connector to create a queue in the Amazon SQS, send a message to the queue, forward it to Simple Stock Quote Service Backend and send the response to the user.
It has a single HTTP API resource, which is sendToQueue
.
If you do not want to configure this yourself, you can simply get the project and run it.
Setting up the environment¶
-
Please follow the steps mentioned in the Setting up the Amazon S3 Environment document in order to create a Amazon account and obtain access key id and secret access key. Keep them saved to be used in the next steps.
-
In this example we will be using XPath 2.0 which needs to be enabled in the product as shown below before starting the integration service.
If you are using the Micro Integrator of EI 7 or APIM 4.0.0, you need to enable this property by adding the following to the PRODUCT-HOME/conf/deployment.toml file. You can further refer to the Product Configurations.
[mediation] synapse.enable_xpath_dom_failover="true"
If you are using EI 6, you can enable this property by uncommenting synapse.xpath.dom.failover.enabled=true property in PRODUCT-HOME/conf/synapse.properties file.
-
In this example we use the SimpleStockQuote service backend. Therefore, the SimpleStockQuote service needs to be started.
Configure the connector in WSO2 Integration Studio¶
Follow these steps to set up the Integration Project and the Connector Exporter Project.
-
Open WSO2 Integration Studio and create an Integration Project.
-
Right-click the project that you created and click on Add or Remove Connector -> Add Connector. You will get directed to the WSO2 Connector Store.
-
Search for the specific connector required for your integration scenario and download it to the workspace.
-
Click Finish, and your Integration Project is ready. The downloaded connector is displayed on the side palette with its operations.
-
You can drag and drop the operations to the design canvas and build your integration logic.
-
Right click on the created Integration Project and select New -> Rest API to create the REST API.
-
First let's create the following sequences, which are buildMessage, createQueue, sendMessage and ReceiveAndForwardMessage. Right click on the created Integration Project and select, -> New -> Sequence to create the Sequence.
-
Provide the Sequence name as buildMessage. You can go to the source view of the XML configuration file of the API and copy the following configuration. In this sequence we are taking the user's input
companyName
and we build the message using a Payload Factory Mediator.<?xml version="1.0" encoding="UTF-8"?> <sequence name="buildMessage" trace="disable" xmlns="http://ws.apache.org/ns/synapse"> <property expression="json-eval($.companyName)" name="companyName" scope="default" type="STRING"/> <payloadFactory media-type="xml"> <format> <m0:getQuote xmlns:m0="http://services.samples"> <m0:request> <m0:symbol>$1</m0:symbol> </m0:request> </m0:getQuote> </format> <args> <arg evaluator="xml" expression="get-property('companyName')"/> </args> </payloadFactory> <header name="Action" scope="default" value="urn:getQuote"/> <enrich> <source clone="true" type="body"/> <target property="target_property" type="property"/> </enrich> </sequence>
-
Create the createQueue sequence as shown below. In this sequence, we create a queue in the Amazon SQS instance.
<?xml version="1.0" encoding="UTF-8"?> <sequence name="createQueue" trace="disable" xmlns="http://ws.apache.org/ns/synapse"> <amazonsqs.init> <accessKeyId>AKIAJRM3ROHOPXQ4V6QA</accessKeyId> <secretAccessKey>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</secretAccessKey> <version>2009-02-01</version> <region>us-east-2</region> </amazonsqs.init> <amazonsqs.createQueue> <queueName>{$ctx:queueName}</queueName> </amazonsqs.createQueue> <property expression="json-eval($.CreateQueueResponse.CreateQueueResult.QueueUrl)" name="queueURL" scope="default" type="STRING"/> <log level="custom"> <property expression="$ctx:queueURL" name="queueURL"/> </log> <property expression="fn:substring($ctx:queueURL,39,12)" name="queueId" scope="default" type="STRING" xmlns:fn="http://www.w3.org/2005/xpath-functions"/> <log level="custom"> <property expression="$ctx:queueId" name="queueId"/> </log> </sequence>
-
Create sendMessage sequence as shown below. In this sequence, we send the message that we built in step 1 to the Amazon SQS Queue.
<?xml version="1.0" encoding="UTF-8"?> <sequence name="sendMessage" trace="disable" xmlns="http://ws.apache.org/ns/synapse"> <amazonsqs.init> <accessKeyId>AKIAJRM3ROJKJJXQ4V6QA</accessKeyId> <secretAccessKey>r7hfmtqVjdwieILi4mq7MJ2kkUZ2GlcCkBNg</secretAccessKey> <version>2009-02-01</version> <region>us-east-2</region> </amazonsqs.init> <amazonsqs.sendMessage> <queueId>{$ctx:queueId}</queueId> <queueName>{$ctx:queueName}</queueName> <messageBody>{$ctx:target_property}</messageBody> </amazonsqs.sendMessage> </sequence>
-
Create the ReceiveAndForwardMessage sequence as shown below. In this sequence, we will receive the message from the Amazon SQS queue and forward it into the StockQuote Endpoint.
<?xml version="1.0" encoding="UTF-8"?> <sequence name="ReceiveAndForwardMessage" trace="disable" xmlns="http://ws.apache.org/ns/synapse"> <amazonsqs.init> <accessKeyId>AKIAJRM3ROJKJJXQ4V6QA</accessKeyId> <secretAccessKey>r7hfmtqVjdwieILi4mq7MJ2kkUZ2GlcCkBNg</secretAccessKey> <version>2009-02-01</version> <region>us-east-2</region> </amazonsqs.init> <amazonsqs.receiveMessage> <maxNumberOfMessages>5</maxNumberOfMessages> <queueId>{$ctx:queueId}</queueId> <queueName>{$ctx:queueName}</queueName> </amazonsqs.receiveMessage> <property expression="json-eval($.ReceiveMessageResponse.ReceiveMessageResult.Message.Body)" name="messageBody" scope="default" type="STRING"/> <payloadFactory media-type="xml"> <format> <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> <soapenv:Body>$1</soapenv:Body> </soapenv:Envelope> </format> <args> <arg evaluator="xml" expression="$ctx:messageBody"/> </args> </payloadFactory> <header name="Action" scope="default" value="urn:getQuote"/> <call> <endpoint key="SimpleStockQuote"/> </call> </sequence>
-
Now right click on the created Integration Project and select New -> Rest API to create the REST API.
-
Provide the API name as SQSAPI and the API context as
/sqs
. You can go to the source view of the XML configuration file of the API and copy the following configuration.<?xml version="1.0" encoding="UTF-8"?> <api context="/sqs" name="SQSAPI" xmlns="http://ws.apache.org/ns/synapse"> <resource methods="POST" uri-template="/sendToQueue"> <inSequence> <property expression="json-eval($.queueName)" name="queueName" scope="default" type="STRING"/> <sequence key="buildMessage"/> <sequence key="createQueue"/> <sequence key="sendMessage"/> <sequence key="ReceiveAndForwardMessage"/> <respond/> </inSequence> <outSequence/> <faultSequence/> </resource> </api>
Exporting Integration Logic as a CApp¶
CApp (Carbon Application) is the deployable artifact on the integration runtime. Let us see how we can export integration logic we developed into a CApp along with the connector.
Creating Connector Exporter Project¶
To bundle a Connector into a CApp, a Connector Exporter Project
is required.
-
Navigate to File -> New -> Other -> WSO2 -> Extensions -> Project Types -> Connector Exporter Project.
-
Enter a name for the Connector Exporter Project.
-
In the next screen select, Specify the parent from workspace and select the specific Integration Project you created from the dropdown.
-
Now you need to add the Connector to Connector Exporter Project that you just created. Right-click the Connector Exporter Project and select, New -> Add Remove Connectors -> Add Connector -> Add from Workspace -> Connector
-
Once you are directed to the workspace, it displays all the connectors that exist in the workspace. You can select the relevant connector and click Ok.
Creating a Composite Application Project¶
To export the Integration Project
as a CApp, a Composite Application Project
needs to be created. Usually, when an Integration project is created, this project can be created as part of that project by Integration Studio. If not, you can specifically create it by navigating to File -> New -> Other -> WSO2 -> Distribution -> Composite Application Project.
Exporting the Composite Application Project¶
-
Right-click the Composite Application Project and click Export Composite Application Project.
-
Select an Export Destination where you want to save the .car file.
-
In the next Create a deployable CAR file screen, select both the created Integration Project and the Connector Exporter Project to save and click Finish. The CApp is created at the specified location provided at the previous step.
Get the project¶
You can download the ZIP file and extract the contents to get the project code.
Tip
You may need to update the value of the access key and make other such changes before deploying and running this project.
Deployment¶
Follow these steps to deploy the exported CApp in the integration runtime.
Deploying on Micro Integrator
You can copy the composite application to the <PRODUCT-HOME>/repository/deployment/server/carbonapps
folder and start the server. Micro Integrator will be started and the composite application will be deployed.
You can further refer the application deployed through the CLI tool. See the instructions on managing integrations from the CLI.
Click here for instructions on deploying on WSO2 Enterprise Integrator 6
-
You can copy the composite application to the
<PRODUCT-HOME>/repository/deployment/server/carbonapps
folder and start the server. -
WSO2 EI server starts and you can login to the Management Console https://localhost:9443/carbon/ URL. Provide login credentials. The default credentials will be admin/admin.
-
You can see that the API is deployed under the API section.
Testing¶
- Create a file called data.json with the following payload.
{ "companyName":"WSO2", "queueName":"Queue1" }
- Invoke the API as shown below using the curl command. Curl Application can be downloaded from here.
curl -H "Content-Type: application/json" --request POST --data @body.json http://localhost:8290/sqs/sendToQueue
Expected Response:
You should get the following response with the 'sys_id' and keep it saved.
<ns:getQuoteResponse xmlns:ns="http://services.samples">
<ns:return xmlns:ax21="http://services.samples/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:GetQuoteResponse">
<ax21:change>4.233604086603518</ax21:change>
<ax21:earnings>-8.707965767387106</ax21:earnings>
<ax21:high>-150.5908765590026</ax21:high>
<ax21:last>153.98353327622493</ax21:last>
<ax21:lastTradeTimestamp>Wed Apr 08 10:38:56 IST 2020</ax21:lastTradeTimestamp>
<ax21:low>158.9975778178183</ax21:low>
<ax21:marketCap>-565228.6001002677</ax21:marketCap>
<ax21:name>WSO2 Company</ax21:name>
<ax21:open>-151.38099715271312</ax21:open>
<ax21:peRatio>23.761940918708092</ax21:peRatio>
<ax21:percentageChange>-2.8310759126772127</ax21:percentageChange>
<ax21:prevClose>-149.5404650806414</ax21:prevClose>
<ax21:symbol>WSO2</ax21:symbol>
<ax21:volume>9834</ax21:volume>
</ns:return>
</ns:getQuoteResponse>
What's Next¶
- You can deploy and run your project on Docker or Kubernetes. See the instructions in Running the Micro Integrator on Containers.