Skip to content

Commit 766bc33

Browse files
authored
Add MQ broker example (#153)
1 parent 2f43231 commit 766bc33

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

mq-broker/Makefile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export AWS_ACCESS_KEY_ID ?= test
2+
export AWS_SECRET_ACCESS_KEY ?= test
3+
export AWS_DEFAULT_REGION = us-east-1
4+
5+
usage: ## Show this help
6+
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
7+
8+
install: ## Install dependencies
9+
@which localstack > /dev/null || pip install localstack
10+
@which awslocal > /dev/null || pip install awscli-local
11+
12+
run: ## Run test creating a broker and sending a message
13+
./test.sh
14+
15+
start:
16+
localstack start -d
17+
18+
stop:
19+
@echo
20+
localstack stop
21+
ready:
22+
@echo Waiting on the LocalStack container...
23+
@localstack wait -t 30 && echo Localstack is ready to use! || (echo Gave up waiting on LocalStack, exiting. && exit 1)
24+
25+
logs:
26+
@localstack logs > logs.txt
27+
28+
test-ci:
29+
make start install ready run; return_code=`echo $$?`;\
30+
make logs; make stop; exit $$return_code;
31+
32+
.PHONY: usage install start run stop ready logs test-ci
33+

mq-broker/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# LocalStack Demo: MQ Broker
2+
3+
Simple demo application illustrating the use of MQ using LocalStack.
4+
5+
## Prerequisites
6+
7+
* LocalStack
8+
* Docker
9+
* `make`
10+
* [`awslocal`](https://github.com/localstack/awscli-local)
11+
12+
## Installing
13+
14+
To install the dependencies:
15+
```
16+
make install
17+
```
18+
19+
## Running
20+
21+
Make sure that LocalStack is started:
22+
```
23+
LOCALSTACK_API_KEY=... DEBUG=1 localstack start
24+
```
25+
26+
The following command runs the example, which starts up a broker and sends a message to a queue:
27+
```
28+
make run
29+
```
30+
31+
After the test script completes, the logs in your terminal should look similar to the output below:
32+
```
33+
$ make run
34+
Creating MQ broker in LocalStack ...
35+
Created MQ broker with id: b-7dc2ba4a-53a0-41ef-a2ad-92eac3ad879d
36+
Describe broker to get the endpoint
37+
Broker endpoint on http://localhost:4510
38+
Sending message to broker
39+
Message sentCleaning up - deleting broker
40+
Deleted Broker b-7dc2ba4a-53a0-41ef-a2ad-92eac3ad879d
41+
```
42+
43+
## License
44+
45+
This code is available under the Apache 2.0 license.

mq-broker/test.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
echo "Creating MQ broker in LocalStack ..."
4+
UUID=$(echo $RANDOM | md5sum | head -c 20)
5+
broker_name="broker_${UUID}"
6+
broker_id=$(awslocal mq create-broker --broker-name $broker_name --deployment-mode SINGLE_INSTANCE --engine-type ACTIVEMQ --engine-version='5.16.5' --host-instance-type 'mq.t2.micro' --auto-minor-version-upgrade --publicly-accessible --users='{"ConsoleAccess": true, "Groups": ["testgroup"],"Password": "QXwV*$iUM9USHnVv&!^7s3c@", "Username": "admin"}' | jq -r '.BrokerId')
7+
echo "Created MQ broker with id: ${broker_id}"
8+
9+
# let broker fully start up
10+
sleep 1
11+
echo "Describe broker to get the endpoint"
12+
broker_endpoint=$(awslocal mq describe-broker --broker-id $broker_id | jq -r '.BrokerInstances[0].ConsoleURL')
13+
echo "Broker endpoint on ${broker_endpoint}"
14+
15+
echo "Sending message to broker"
16+
curl -XPOST -d "body=message" http://admin:admin@${broker_endpoint:7}/api/message\?destination\=queue://orders.input
17+
18+
echo $"Cleaning up - deleting broker"
19+
broker_id=$(awslocal mq delete-broker --broker-id $broker_id | jq -r '.BrokerId')
20+
echo "Deleted Broker ${broker_id}"

0 commit comments

Comments
 (0)