Skip to content

Instantly share code, notes, and snippets.

@jalp
Created April 7, 2014 07:41
Show Gist options
  • Save jalp/10016196 to your computer and use it in GitHub Desktop.
Save jalp/10016196 to your computer and use it in GitHub Desktop.
Activemq simple client with stompy
import sys
import logging
from stompy.stomp import Stomp, ConnectionError, ConnectionTimeoutError
class QueueConsumer(object):
""" Queue consumer
"""
def __init__(self, host='localhost', port=61613):
self.logger = logging.getLogger('connect-logger')
self.host = host
self.port = port
self.connection = None
def connect(self):
""" Connect to activemq
"""
self.logger.debug("Connecting to host {}:{}".format(self.host, self.port))
try:
self.connection = Stomp(self.host, self.port)
except ConnectionError as ce:
self.logger.exception(ce)
sys.exit(1)
except ConnectionTimeoutError as cte:
self.logger.exception(cte)
sys.exit(1)
self.connection.connect()
self.logger.debug("Connected to host {}".format(self.host, self.port))
def subscribe(self, queue_name):
""" Subscribe action to a queue
"""
self.connection.subscribe({'destination': '/queue/{}'.format(queue_name), 'ack': 'client'})
self.logger.debug("Subscribed to {}".format(queue_name))
def unsubscribe(self, queue_name):
""" Unsubscribe action
"""
self.connection.unsubscribe({'destination': '/queue/{}'.format(queue_name)})
self.logger.debug("Unsubscribed from {}".format(queue_name))
def disconnect(self):
""" Disconnect from activemq
"""
if self.connection and self.connection.connected:
try:
self.connection.disconnect()
self.logger.debug("Disconnected from activemq {}:{}".format(self.host, self.port))
except Exception as ex:
self.logger.error("Error while closing stomp connection: {}".format(ex))
finally:
self.connection.connected = False
def get_message(self):
""" Get queue message
"""
return self.connection.receive_frame()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment