def create_topic(self, topic): if self.amqp_broker: AMQPHub.create_queue(topic) # @@ remove this when we keep track of this in a DB if topic not in self.topics: self.topics[topic] = []
def send_message(self, topic, message, jsonify=True): """ Send a message to a specific topic. :topic: A topic or list of topics to send the message to. :message: The message body. Can be a string, list, or dict. :jsonify: To automatically encode non-strings to JSON """ if not isinstance(topic, list): topics = [topic] else: topics = topic for topic in topics: if jsonify: message = json.encode(message) if self.amqp_broker: AMQPHub.send_message(self, topic, message, routing_key=topic) elif self.stomp_broker: StompHub.send_message(self, topic, message)
def __init__(self, topics=None): global config self.amqp_broker = config.get('amqp_broker', None) self.stomp_broker = config.get('stomp_broker', None) # If we're running outside of middleware and hub, load config if not self.amqp_broker and not self.stomp_broker: config = get_moksha_appconfig() self.amqp_broker = config.get('amqp_broker', None) self.stomp_broker = config.get('stomp_broker', None) if self.amqp_broker and self.stomp_broker: log.warning("Running with both a STOMP and AMQP broker. " "This mode is experimental and may or may not work") if not self.topics: self.topics = defaultdict(list) if topics: for topic, callbacks in topics.iteritems(): if not isinstance(callbacks, list): callbacks = [callbacks] for callback in callbacks: self.topics[topic].append(callback) if self.amqp_broker: AMQPHub.__init__(self, self.amqp_broker, username=config.get('amqp_broker_user', 'guest'), password=config.get('amqp_broker_pass', 'guest'), ssl=config.get('amqp_broker_ssl', False)) if self.stomp_broker: log.info('Initializing STOMP support') StompHub.__init__(self, self.stomp_broker, port=config.get('stomp_port', 61613), username=config.get('stomp_user', 'guest'), password=config.get('stomp_pass', 'guest'), topics=self.topics.keys())
def close(self): if self.amqp_broker: try: AMQPHub.close(self) except Exception, e: log.warning('Exception when closing AMQPHub: %s' % str(e))