Now that we've sent a message to our message queue in our app, let's use another microservice to pluck that from the queue on the other end.
To demonstrate the flexibility of a microservice design, our secondary service will be a Python script that connects to the MQ and listens for messages on the e-mail queue, when it finds one. It will parse the message and send an e-mail. Optionally, it could publish a status message back on the queue or log it, but we won't go down that road for now:
import pika import json import smtplib from email.mime.text import MIMEText connection = pika.BlockingConnection(pika.ConnectionParameters( host='localhost')) channel = connection.channel() channel.queue_declare(queue='email') print ' [*] Waiting for messages. To exit press CTRL+C' def callback(ch, method, properties, body): print " [x] Received %r" % (body,) parsed = json.loads(body) msg = MIMEText() msg['From'] = 'Me' msg['To'] = parsed['email'] msg['Subject'] = parsed['message'] s = smtplib.SMTP('localhost') s.sendmail('Me', parsed['email'], msg.as_string()) s.quit() channel.basic_consume(callback, queue='email', no_ack=True) channel.start_consuming()