Service: SmoothMQ - EyevinnOSC/community GitHub Wiki
The open source project SmoothMQ offers a drop-in-replacement for SQS. This tutorial walks you through using SmoothMQ in Open Source Cloud as a messaging queue in your solution, and using the Python boto3 client to push and read messages of the queue.
- If you have not already done so, sign up for an OSC account.
- Python environment
You will be using the CLI to create the service instances for this solution.
- If not already installed on your machine, download and install NodeJS
- Install OSC CLI using NodeJS package manager
% npm install -g @osaas/cli
- Verify that it is installed
% osc --version
0.11.2
Create two service secrets in the SmoothMQ service in the OSC web user interface.
accesskey=guide
secretkey=guide
The access key and secret to just serve as an example.
Create a message queue service with this command in your terminal. We will refer to the service secrets created in step 2.
% osc create poundifdef-smoothmq guide -o AccessKey="{{secrets.accesskey}}" -o SecretKey="{{secrets.secretkey}}"
The message queue service is now created and available at the endpoint https://<tenant>-guide.poundifdef-smoothmq.auto.prod.osaas.io/
(where <tenant>
is your tenant id).
To verify we will write a small Python application that creates a queue and place a message on the queue. Begin with setup a Python virtual environment.
% python -m venv .venv
% . .venv/bin/activate
Install the Boto3 client.
% python -m pip install boto3
Create a Python script we call send.py
containing:
import boto3
sqs = boto3.client("sqs", endpoint_url="https://<tenant>-guide.poundifdef-smoothmq.auto.prod.osaas.io/")
sqs.create_queue(QueueName='guide')
response = sqs.list_queues()
url = response['QueueUrls'][0]
sqs.send_message(QueueUrl=url, MessageBody="Hello world")
Run the script with the access key and secret that we provided when creating the message queue service.
% export AWS_ACCESS_KEY_ID=guide
% export AWS_SECRET_ACCESS_KEY=guide
python send.py
Let us now develop a script we call recv.py
containing:
import boto3
sqs = boto3.client("sqs", endpoint_url="https://<tenant>-guide.poundifdef-smoothmq.auto.prod.osaas.io/")
response = sqs.list_queues()
url = response['QueueUrls'][0]
msg = sqs.receive_message(QueueUrl=url)
print (msg['Messages'][0]['Body'])
And when running it we would get.
% python recv.py
Hello world