def consume_amqp_message(self, message): self.message_accept(message) try: topic = message.get('delivery_properties').routing_key except AttributeError: # If we receive an AMQP message without a toipc, don't proxy it to STOMP return if self.stomp_broker: StompHub.send_message(self, topic.encode('utf8'), message.body.encode('utf8'))
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())