def test_revive(self):
     chan = self.connection.channel()
     p = Producer(chan)
     chan2 = self.connection.channel()
     p.revive(chan2)
     self.assertIs(p.channel, chan2)
     self.assertIs(p.exchange.channel, chan2)
Beispiel #2
0
    def _send_dhcp_notification(self, context, data, methodname):
        #self._dhcp_agent_notifier.notify(context, data, methodname)
        task_exchange = Exchange('nspagent', type='topic')   
	create_result = {"oslo.message" :{ 
			  #'message_id': '5c329c20-d435-444c-8fa2-e1b54592219c',
			  #'publisher_id': 'compute.host1',
			  'method': 'network_create_end',
			  #'_context_user_id': None,
		 	  #'_context_project_id': None,
			  #'_context_is_admin': True,
			  "args" : {"payload": {"network": network
					#{"id": "24783e1a-63fe-43d5-9989-e1515c24eecd"}
				      }		
				   }
		         },
			"oslo.version":'2.0',
		      }
	create_result["oslo.message" ] = json.dumps(create_result["oslo.message"])
	connection = Connection('amqp://*****:*****@192.168.49.22:5672//')  
	channel = connection.channel()  
   
	message=Message(channel,body= 'subenet_create_end')  
   
	# produce  
	producer = Producer(channel,serializer='json') 
        print message.body, task_exchange 
	producer.publish(create_result, routing_key='dhcp_agent')  
Beispiel #3
0
class UdpService(os_service.Service):
    """Listener for the udpservice service."""
    def start(self):
        """Bind the UDP socket and handle incoming data."""
        # ensure dispatcher is configured before starting other services
        super(UdpService, self).start()
         
        if cfg.CONF.udpservice.udp_address:
            self.tg.add_thread(self.start_udp)

    def convert_sample_to_event_data(self,msg):
        event_data = {'event_type': 'infra','message_id':six.text_type(uuid.uuid4()),'publisher_id': 'cpe_publisher_id','timestamp':datetime.datetime.now().isoformat(),'priority':'INFO','payload':msg}
        return event_data
    
    def setup_rabbit_mq_channel(self):
        service_exchange = Exchange(cfg.CONF.udpservice.acord_control_exchange, "topic", durable=False)
        rabbit_host = cfg.CONF.udpservice.rabbit_hosts
        rabbit_user = cfg.CONF.udpservice.rabbit_userid 
        rabbit_password = cfg.CONF.udpservice.rabbit_password
        # connections/channels
        connection = BrokerConnection(rabbit_host, rabbit_user, rabbit_password)
        print 'Connection to RabbitMQ server successful'
        channel = connection.channel()
        # produce
        self.producer = Producer(channel, exchange=service_exchange, routing_key='notifications.info')

    def start_udp(self):
        address_family = socket.AF_INET
        if netutils.is_valid_ipv6(cfg.CONF.udpservice.udp_address):
            address_family = socket.AF_INET6
        udp = socket.socket(address_family, socket.SOCK_DGRAM)
        udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        udp.bind((cfg.CONF.udpservice.udp_address,
                  cfg.CONF.udpservice.udp_port))

        self.setup_rabbit_mq_channel()
        self.udp_run = True
        while self.udp_run:
            # NOTE(jd) Arbitrary limit of 64K because that ought to be
            # enough for anybody.
            data, source = udp.recvfrom(64 * units.Ki)
            try:
                sample = msgpack.loads(data, encoding='utf-8')
            except Exception:
                LOG.warn(_("UDP: Cannot decode data sent by %s"), source)
            else:
                try:
                    if sample.has_key("event_type"):
                         LOG.debug(_("recevied event  :%s"),sample)
                         self.producer.publish(sample)
                    else:
                         LOG.debug(_("recevied Sample  :%s"),sample)
                         msg = self.convert_sample_to_event_data(sample)
                         self.producer.publish(msg)
                except Exception:
                    LOG.exception(_("UDP: Unable to store meter"))

    def stop(self):
        self.udp_run = False
        super(UdpService, self).stop()
 def test_revive(self):
     chan = self.connection.channel()
     p = Producer(chan)
     chan2 = self.connection.channel()
     p.revive(chan2)
     self.assertIs(p.channel, chan2)
     self.assertIs(p.exchange.channel, chan2)
    def test_publish__consume(self):
        connection = BrokerConnection(transport=Transport)
        channel = connection.channel()
        producer = Producer(channel, self.exchange, routing_key="test_Redis")
        consumer = Consumer(channel, self.queue)

        producer.publish({"hello2": "world2"})
        _received = []

        def callback(message_data, message):
            _received.append(message_data)
            message.ack()

        consumer.register_callback(callback)
        consumer.consume()

        self.assertTrue(channel._poller._can_start())
        try:
            connection.drain_events(timeout=1)
            self.assertTrue(_received)
            self.assertFalse(channel._poller._can_start())
            self.assertRaises(socket.timeout,
                              connection.drain_events, timeout=0.01)
        finally:
            channel.close()
    def test_publish__consume(self):
        connection = BrokerConnection(transport=Transport)
        channel = connection.channel()
        producer = Producer(channel, self.exchange, routing_key="test_Redis")
        consumer = Consumer(channel, self.queue)

        producer.publish({"hello2": "world2"})
        _received = []

        def callback(message_data, message):
            _received.append(message_data)
            message.ack()

        consumer.register_callback(callback)
        consumer.consume()

        self.assertIn(channel, channel.connection.cycle._channels)
        try:
            connection.drain_events(timeout=1)
            self.assertTrue(_received)
            self.assertRaises(socket.timeout,
                              connection.drain_events,
                              timeout=0.01)
        finally:
            channel.close()
Beispiel #7
0
 def enable(self):
     self.publisher = Producer(self.channel or self.connection.channel(),
                               exchange=event_exchange,
                               serializer=self.serializer)
     self.enabled = True
     for callback in self.on_enabled:
         callback()
Beispiel #8
0
 def enable(self):
     conf = self.app.conf
     self.enabled = True
     channel = self.channel or self.connection.channel()
     self.publisher = Producer(channel,
                               exchange=event_exchange,
                               serializer=conf.CELERY_EVENT_SERIALIZER)
Beispiel #9
0
 def _InitConnect(self):
     if self.connection is None or self.connection.connected==False:
         self.connection = Connection(hostname=self.server,port=self.port,userid=self.usr,password=self.psw,virtual_host=self.path)
         self.channel = self.connection.channel()
         self.producer=Producer(self.channel)
         self.smsExchange=Exchange("sys.sms",type='topic',channel=self.channel,durable=True,delivery_mode=2)
         self.smsCodeProduce=Producer(self.channel,self.smsExchange,routing_key='sms.code')
Beispiel #10
0
class QueuePush(object):
    def __init__(self,Queue_Server,Queue_Port,Queue_User,Queue_PassWord,Queue_Path):
        self.usr=Queue_User
        self.psw=Queue_PassWord
        self.server=Queue_Server
        self.port=Queue_Port
        self.path=Queue_Path
        self.connection=None
        self.smsExchange=None
    def _InitConnect(self):
        if self.connection is None or self.connection.connected==False:
            self.connection = Connection(hostname=self.server,port=self.port,userid=self.usr,password=self.psw,virtual_host=self.path)
            self.channel = self.connection.channel()
            self.producer=Producer(self.channel)
            self.smsExchange=Exchange("sys.sms",type='topic',channel=self.channel,durable=True,delivery_mode=2)
            self.smsCodeProduce=Producer(self.channel,self.smsExchange,routing_key='sms.code')
    def Push(self,queueid,connectid,body):
        self.rawPush(queueid,{'connid':connectid},body)
    def Close(self,queueid,connectid):
        self.rawPush(queueid,{'connid':connectid,'close_connect':'1'},'close')
    def rawPush(self,routing_key,headers,body):
        self._InitConnect()
        self.producer.publish(body=body,delivery_mode=2,headers=headers,
                              routing_key=routing_key,retry=True,compression='gzip')
    def sendCode(self,phone,code):
        self._InitConnect()
        json_str=json.dumps({'phone':str(phone),"content":u"您的莱信验证码为:%s,请在5分钟内输入完成验证。【莱福思】"%str(code)},ensure_ascii=False)
        self.smsCodeProduce.publish(body=json_str,retry=True,compression='gzip')
Beispiel #11
0
 def enable(self):
     conf = self.app.conf
     self.enabled = True
     channel = self.channel or self.connection.channel()
     self.publisher = Producer(channel,
                               exchange=event_exchange,
                               serializer=self.serializer)
Beispiel #12
0
    def _process_attacks_queue(self) -> None:
        conn = Connection(config.get_broker_url())
        with conn.channel() as channel:
            producer = Producer(channel)
            by_label = defaultdict(list)
            while not self._q.empty():
                try:
                    ar: models.AttackResult = self._q.get_nowait()
                except Empty:
                    continue
                else:
                    by_label[ar.get_label_key()].append(ar)

            for ar_list in by_label.values():
                if not ar_list:
                    continue

                monitor_message = {
                    'type': 'flag_submit',
                    'data': ar_list[0].get_label_values(),
                    'value': len(ar_list),
                }

                producer.publish(
                    monitor_message,
                    exchange='',
                    routing_key='forcad-monitoring',
                )
 def test_prepare(self):
     message = {u"the quick brown fox": u"jumps over the lazy dog"}
     channel = self.connection.channel()
     p = Producer(channel, self.exchange, serializer="json")
     m, ctype, cencoding = p._prepare(message, headers={})
     self.assertDictEqual(message, anyjson.loads(m))
     self.assertEqual(ctype, "application/json")
     self.assertEqual(cencoding, "utf-8")
 def test_prepare(self):
     message = {u"the quick brown fox": u"jumps over the lazy dog"}
     channel = self.connection.channel()
     p = Producer(channel, self.exchange, serializer="json")
     m, ctype, cencoding = p._prepare(message, headers={})
     self.assertDictEqual(message, anyjson.loads(m))
     self.assertEqual(ctype, "application/json")
     self.assertEqual(cencoding, "utf-8")
Beispiel #15
0
 def test_prepare(self):
     message = {u'the quick brown fox': u'jumps over the lazy dog'}
     channel = self.connection.channel()
     p = Producer(channel, self.exchange, serializer='json')
     m, ctype, cencoding = p._prepare(message, headers={})
     self.assertDictEqual(message, anyjson.loads(m))
     self.assertEqual(ctype, 'application/json')
     self.assertEqual(cencoding, 'utf-8')
Beispiel #16
0
class QueuePush(object):
    def __init__(self, Queue_Server, Queue_Port, Queue_User, Queue_PassWord,
                 Queue_Path):
        self.usr = Queue_User
        self.psw = Queue_PassWord
        self.server = Queue_Server
        self.port = Queue_Port
        self.path = Queue_Path
        self.connection = None
        self.smsExchange = None

    def _InitConnect(self):
        if self.connection is None or self.connection.connected == False:
            self.connection = Connection(hostname=self.server,
                                         port=self.port,
                                         userid=self.usr,
                                         password=self.psw,
                                         virtual_host=self.path)
            self.channel = self.connection.channel()
            self.producer = Producer(self.channel)
            self.smsExchange = Exchange("sys.sms",
                                        type='topic',
                                        channel=self.channel,
                                        durable=True,
                                        delivery_mode=2)
            self.smsCodeProduce = Producer(self.channel,
                                           self.smsExchange,
                                           routing_key='sms.code')

    def Push(self, queueid, connectid, body):
        self.rawPush(queueid, {'connid': connectid}, body)

    def Close(self, queueid, connectid):
        self.rawPush(queueid, {
            'connid': connectid,
            'close_connect': '1'
        }, 'close')

    def rawPush(self, routing_key, headers, body):
        self._InitConnect()
        self.producer.publish(body=body,
                              delivery_mode=2,
                              headers=headers,
                              routing_key=routing_key,
                              retry=True,
                              compression='gzip')

    def sendCode(self, phone, code):
        self._InitConnect()
        json_str = json.dumps(
            {
                'phone': str(phone),
                "content": u"您的莱信验证码为:%s,请在5分钟内输入完成验证。【莱福思】" % str(code)
            },
            ensure_ascii=False)
        self.smsCodeProduce.publish(body=json_str,
                                    retry=True,
                                    compression='gzip')
 def test_manual_declare(self):
     channel = self.connection.channel()
     p = Producer(channel, self.exchange, auto_declare=False)
     self.assertTrue(p.exchange.is_bound)
     self.assertNotIn("exchange_declare", channel,
                      "auto_declare=False does not declare exchange")
     p.declare()
     self.assertIn("exchange_declare", channel,
                   "p.declare() declares exchange")
 def test_manual_declare(self):
     channel = self.connection.channel()
     p = Producer(channel, self.exchange, auto_declare=False)
     self.assertTrue(p.exchange.is_bound)
     self.assertNotIn("exchange_declare", channel,
                      "auto_declare=False does not declare exchange")
     p.declare()
     self.assertIn("exchange_declare", channel,
                   "p.declare() declares exchange")
Beispiel #19
0
def main():

    connection = Connection('amqp://*****:*****@localhost:5672//')
    _channel = connection.channel()
    _exchange = Exchange('neutron', type='topic')

    pro = Producer(channel=_channel,
                   exchange=_exchange,
                   routing_key='q-plugin')
    pro.publish(MSG)
Beispiel #20
0
 def _publish_reply(self, reply, exchange, routing_key, channel=None):
     chan = channel or self.connection.channel()
     try:
         exchange = Exchange(
             exchange, exchange_type="direct", delivery_mode="transient", durable=False, auto_delete=True
         )
         producer = Producer(chan, exchange=exchange)
         producer.publish(reply, routing_key=routing_key)
     finally:
         channel or chan.close()
Beispiel #21
0
 def _publish(self, type, arguments, destination=None, reply_ticket=None, channel=None):
     message = {"method": type, "arguments": arguments, "destination": destination}
     if reply_ticket:
         message["reply_to"] = {"exchange": self.reply_exchange.name, "routing_key": reply_ticket}
     chan = channel or self.connection.channel()
     producer = Producer(chan, exchange=self.exchange)
     try:
         producer.publish(message)
     finally:
         channel or chan.close()
Beispiel #22
0
    def test_publish__get(self):
        channel = self.connection.channel()
        producer = Producer(channel, self.exchange, routing_key="test_Redis")
        self.queue(channel).declare()

        producer.publish({"hello": "world"})

        self.assertDictEqual(self.queue(channel).get().payload, {"hello": "world"})
        self.assertIsNone(self.queue(channel).get())
        self.assertIsNone(self.queue(channel).get())
        self.assertIsNone(self.queue(channel).get())
    def test_purge(self):
        channel = self.connection.channel()
        producer = Producer(channel, self.exchange, routing_key="test_Redis")
        self.queue(channel).declare()

        for i in range(10):
            producer.publish({"hello": "world-%s" % (i, )})

        self.assertEqual(channel._size("test_Redis"), 10)
        self.assertEqual(self.queue(channel).purge(), 10)
        channel.close()
Beispiel #24
0
 def test_prepare_compression(self):
     message = {u"the quick brown fox": u"jumps over the lazy dog"}
     channel = self.connection.channel()
     p = Producer(channel, self.exchange, serializer="json")
     headers = {}
     m, ctype, cencoding = p._prepare(message, compression="zlib",
                                      headers=headers)
     self.assertEqual(ctype, "application/json")
     self.assertEqual(cencoding, "utf-8")
     self.assertEqual(headers["compression"], "application/x-gzip")
     self.assertEqual(simplejson.loads(m.decode("zlib")), message)
    def test_purge(self):
        channel = self.connection.channel()
        producer = Producer(channel, self.exchange, routing_key="test_Redis")
        self.queue(channel).declare()

        for i in range(10):
            producer.publish({"hello": "world-%s" % (i, )})

        self.assertEqual(channel._size("test_Redis"), 10)
        self.assertEqual(self.queue(channel).purge(), 10)
        channel.close()
Beispiel #26
0
 def setup_rabbit_mq_channel(self):
     service_exchange = Exchange(cfg.CONF.udpservice.acord_control_exchange, "topic", durable=False)
     rabbit_host = cfg.CONF.udpservice.rabbit_hosts
     rabbit_user = cfg.CONF.udpservice.rabbit_userid 
     rabbit_password = cfg.CONF.udpservice.rabbit_password
     # connections/channels
     connection = BrokerConnection(rabbit_host, rabbit_user, rabbit_password)
     print 'Connection to RabbitMQ server successful'
     channel = connection.channel()
     # produce
     self.producer = Producer(channel, exchange=service_exchange, routing_key='notifications.info')
Beispiel #27
0
 def _publish_reply(self, reply, exchange, routing_key, channel=None):
     chan = channel or self.connection.channel()
     try:
         exchange = Exchange(exchange, exchange_type="direct",
                                       delivery_mode="transient",
                                       durable=False,
                                       auto_delete=True)
         producer = Producer(chan, exchange=exchange,
                                   auto_declare=True)
         producer.publish(reply, routing_key=routing_key)
     finally:
         channel or chan.close()
    def test_publish__get(self):
        channel = self.connection.channel()
        producer = Producer(channel, self.exchange, routing_key="test_Redis")
        self.queue(channel).declare()

        producer.publish({"hello": "world"})

        self.assertDictEqual(
            self.queue(channel).get().payload, {"hello": "world"})
        self.assertIsNone(self.queue(channel).get())
        self.assertIsNone(self.queue(channel).get())
        self.assertIsNone(self.queue(channel).get())
Beispiel #29
0
 def test_prepare_custom_content_type(self):
     message = 'the quick brown fox'.encode('utf-8')
     channel = self.connection.channel()
     p = Producer(channel, self.exchange, serializer='json')
     m, ctype, cencoding = p._prepare(message, content_type='custom')
     self.assertEqual(m, message)
     self.assertEqual(ctype, 'custom')
     self.assertEqual(cencoding, 'binary')
     m, ctype, cencoding = p._prepare(message, content_type='custom',
                                      content_encoding='alien')
     self.assertEqual(m, message)
     self.assertEqual(ctype, 'custom')
     self.assertEqual(cencoding, 'alien')
Beispiel #30
0
 def test_prepare_is_already_unicode(self):
     message = u'the quick brown fox'
     channel = self.connection.channel()
     p = Producer(channel, self.exchange, serializer='json')
     m, ctype, cencoding = p._prepare(message, content_type='text/plain')
     self.assertEqual(m, message.encode('utf-8'))
     self.assertEqual(ctype, 'text/plain')
     self.assertEqual(cencoding, 'utf-8')
     m, ctype, cencoding = p._prepare(message, content_type='text/plain',
                                     content_encoding='utf-8')
     self.assertEqual(m, message.encode('utf-8'))
     self.assertEqual(ctype, 'text/plain')
     self.assertEqual(cencoding, 'utf-8')
Beispiel #31
0
 def test_prepare_compression(self):
     message = {u'the quick brown fox': u'jumps over the lazy dog'}
     channel = self.connection.channel()
     p = Producer(channel, self.exchange, serializer='json')
     headers = {}
     m, ctype, cencoding = p._prepare(message, compression='zlib',
                                      headers=headers)
     self.assertEqual(ctype, 'application/json')
     self.assertEqual(cencoding, 'utf-8')
     self.assertEqual(headers['compression'], 'application/x-gzip')
     import zlib
     self.assertEqual(anyjson.loads(
                         zlib.decompress(m).decode('utf-8')), message)
 def test_prepare_is_already_unicode(self):
     message = u"the quick brown fox"
     channel = self.connection.channel()
     p = Producer(channel, self.exchange, serializer="json")
     m, ctype, cencoding = p._prepare(message, content_type="text/plain")
     self.assertEqual(m, message.encode("utf-8"))
     self.assertEqual(ctype, "text/plain")
     self.assertEqual(cencoding, "utf-8")
     m, ctype, cencoding = p._prepare(message, content_type="text/plain",
                                     content_encoding="utf-8")
     self.assertEqual(m, message.encode("utf-8"))
     self.assertEqual(ctype, "text/plain")
     self.assertEqual(cencoding, "utf-8")
 def test_prepare_custom_content_type(self):
     message = "the quick brown fox".encode("utf-8")
     channel = self.connection.channel()
     p = Producer(channel, self.exchange, serializer="json")
     m, ctype, cencoding = p._prepare(message, content_type="custom")
     self.assertEqual(m, message)
     self.assertEqual(ctype, "custom")
     self.assertEqual(cencoding, "binary")
     m, ctype, cencoding = p._prepare(message, content_type="custom",
                                      content_encoding="alien")
     self.assertEqual(m, message)
     self.assertEqual(ctype, "custom")
     self.assertEqual(cencoding, "alien")
Beispiel #34
0
 def _publish(self, type, arguments, destination=None, reply_ticket=None,
         channel=None):
     message = {"method": type,
                "arguments": arguments,
                "destination": destination}
     if reply_ticket:
         message["reply_to"] = {"exchange": self.reply_exchange.name,
                                "routing_key": reply_ticket}
     chan = channel or self.connection.channel()
     producer = Producer(chan, exchange=self.exchange)
     try:
         producer.publish(message)
     finally:
         channel or chan.close()
Beispiel #35
0
    def producer(self):
        self.connection = BrokerConnection(hostname=AMQP_IP, port=AMQP_PORT,
                                           userid=AMQP_USER, password=AMQP_PASSWORD,
                                           virtual_host="/",
                                           transport=AMQP_TRANSPORT)
        self.channel = self.connection.channel()
        # produce

        self.media_exchange = Exchange("media", "direct", durable=True)

        producer = Producer(self.channel, exchange=self.media_exchange, serializer="json")
        producer.publish({"name": "/tmp/lolcat1.avi", "size": 1301013})

        print self.connection.get_transport_cls()
 def test_prepare_is_already_unicode(self):
     message = u"the quick brown fox"
     channel = self.connection.channel()
     p = Producer(channel, self.exchange, serializer="json")
     m, ctype, cencoding = p._prepare(message, content_type="text/plain")
     self.assertEqual(m, message.encode("utf-8"))
     self.assertEqual(ctype, "text/plain")
     self.assertEqual(cencoding, "utf-8")
     m, ctype, cencoding = p._prepare(message,
                                      content_type="text/plain",
                                      content_encoding="utf-8")
     self.assertEqual(m, message.encode("utf-8"))
     self.assertEqual(ctype, "text/plain")
     self.assertEqual(cencoding, "utf-8")
 def test_prepare_custom_content_type(self):
     message = "the quick brown fox".encode("utf-8")
     channel = self.connection.channel()
     p = Producer(channel, self.exchange, serializer="json")
     m, ctype, cencoding = p._prepare(message, content_type="custom")
     self.assertEqual(m, message)
     self.assertEqual(ctype, "custom")
     self.assertEqual(cencoding, "binary")
     m, ctype, cencoding = p._prepare(message,
                                      content_type="custom",
                                      content_encoding="alien")
     self.assertEqual(m, message)
     self.assertEqual(ctype, "custom")
     self.assertEqual(cencoding, "alien")
 def test_prepare_compression(self):
     message = {u"the quick brown fox": u"jumps over the lazy dog"}
     channel = self.connection.channel()
     p = Producer(channel, self.exchange, serializer="json")
     headers = {}
     m, ctype, cencoding = p._prepare(message,
                                      compression="zlib",
                                      headers=headers)
     self.assertEqual(ctype, "application/json")
     self.assertEqual(cencoding, "utf-8")
     self.assertEqual(headers["compression"], "application/x-gzip")
     import zlib
     self.assertEqual(anyjson.loads(zlib.decompress(m).decode("utf-8")),
                      message)
    def _send(self, connection, request):
        """
        :param request: JSON serializable dict

        """
        dbutils.exit_if_in_transaction(log)
        log.debug("send %s" % request["request_id"])
        request["response_routing_key"] = self._response_routing_key

        def errback(exc, _):
            log.info(
                "RabbitMQ rpc got a temporary error. May retry. Error: %r",
                exc,
                exc_info=1)

        retry_policy = {"max_retries": 10, "errback": errback}

        with Producer(connection) as producer:
            maybe_declare(_amqp_exchange(), producer.channel, True,
                          **retry_policy)
            producer.publish(
                request,
                serializer="json",
                routing_key=self._request_routing_key,
                delivery_mode=TRANSIENT_DELIVERY_MODE,
                retry=True,
                retry_policy=retry_policy,
            )
Beispiel #40
0
 def Connect(self):
     self.connection = Connection(
         hostname=self.host,
         port=self.port,
         userid=self.usr,
         password=self.psw,
         virtual_host=self.virtual_host,
         transport='kombu.transport.pyamqp:Transport')
     self.channel = self.connection.channel()
     self.producer = Producer(self.channel)
     self.task_count = 0
     if self.needresult:
         self.back_queue = Queue(name=self.result_queuename,
                                 auto_delete=True,
                                 exclusive=True,
                                 durable=False)
         self.consumer = Consumer(self.channel,
                                  self.back_queue,
                                  no_ack=True)
         self.consumer.qos(prefetch_count=1)
         self.consumer.register_callback(self.on_response)
         self.callback_queue = self.consumer.queues[0].name
         self.consumer.consume()
     else:
         self.callback_queue = None
Beispiel #41
0
 def get_producers(self):
     """
         One producer only for all messages, since we have only one exchange.
     """
     return {
         'psms': Producer(self.channel, exchange=self.exchanges['psms'])
     }
Beispiel #42
0
    def test_publish(self):
        channel = self.connection.channel()
        p = Producer(channel, self.exchange, serializer='json')
        message = {u'the quick brown fox': u'jumps over the lazy dog'}
        ret = p.publish(message, routing_key='process')
        self.assertIn('prepare_message', channel)
        self.assertIn('basic_publish', channel)

        m, exc, rkey = ret
        self.assertDictEqual(message, anyjson.loads(m['body']))
        self.assertDictContainsSubset({'content_type': 'application/json',
                                       'content_encoding': 'utf-8',
                                       'priority': 0}, m)
        self.assertDictContainsSubset({'delivery_mode': 2}, m['properties'])
        self.assertEqual(exc, p.exchange.name)
        self.assertEqual(rkey, 'process')
    def test_publish(self):
        channel = self.connection.channel()
        p = Producer(channel, self.exchange, serializer="json")
        message = {u"the quick brown fox": u"jumps over the lazy dog"}
        ret = p.publish(message, routing_key="process")
        self.assertIn("prepare_message", channel)
        self.assertIn("basic_publish", channel)

        m, exc, rkey = ret
        self.assertDictEqual(message, anyjson.loads(m["body"]))
        self.assertDictContainsSubset({"content_type": "application/json",
                                       "content_encoding": "utf-8",
                                       "priority": 0}, m)
        self.assertDictContainsSubset({"delivery_mode": 2}, m["properties"])
        self.assertEqual(exc, p.exchange.name)
        self.assertEqual(rkey, "process")
Beispiel #44
0
 def enable(self):
     conf = self.app.conf
     self.enabled = True
     channel = self.channel or self.connection.channel()
     self.publisher = Producer(channel,
                               exchange=event_exchange,
                               serializer=conf.CELERY_EVENT_SERIALIZER)
Beispiel #45
0
 def enable(self):
     self.publisher = Producer(self.channel or self.connection,
                               exchange=self.get_exchange(),
                               serializer=self.serializer)
     self.enabled = True
     for callback in self.on_enabled:
         callback()
Beispiel #46
0
 def _InitConnect(self):
     if self.connection is None or self.connection.connected == False:
         self.connection = Connection(hostname=self.server,
                                      port=self.port,
                                      userid=self.usr,
                                      password=self.psw,
                                      virtual_host=self.path)
         self.channel = self.connection.channel()
         self.producer = Producer(self.channel)
         self.smsExchange = Exchange("sys.sms",
                                     type='topic',
                                     channel=self.channel,
                                     durable=True,
                                     delivery_mode=2)
         self.smsCodeProduce = Producer(self.channel,
                                        self.smsExchange,
                                        routing_key='sms.code')
Beispiel #47
0
 def setup_rabbit_mq_channel(self):
     service_exchange = Exchange(self.acord_control_exchange, "topic", durable=False)
     # connections/channels
     connection = BrokerConnection(self.rabbit_host, self.rabbit_user, self.rabbit_password)
     logging.info("Connection to RabbitMQ server successful")
     channel = connection.channel()
     # produce
     self.producer = Producer(channel, exchange=service_exchange, routing_key='notifications.info')
 def test_auto_declare(self):
     channel = self.connection.channel()
     p = Producer(channel, self.exchange, auto_declare=True)
     self.assertIsNot(p.exchange, self.exchange,
                      "creates Exchange clone at bind")
     self.assertTrue(p.exchange.is_bound)
     self.assertIn("exchange_declare", channel,
                   "auto_declare declares exchange")
Beispiel #49
0
    def test_produce_consume_noack(self):
        channel = self.c.channel()
        producer = Producer(channel, self.e)
        consumer = Consumer(channel, self.q, no_ack=True)

        for i in range(10):
            producer.publish({"foo": i}, routing_key="test_transport_memory")

        _received = []

        def callback(message_data, message):
            _received.append(message)

        consumer.register_callback(callback)
        consumer.consume()

        while 1:
            if len(_received) == 10:
                break
            self.c.drain_events()

        self.assertEqual(len(_received), 10)
Beispiel #50
0
 def run(self):
   amqhost = self.config_parser.get(self.section,'amqhost')
   amqport = int(self.config_parser.get(self.section,'amqport'))
   amquser = self.config_parser.get(self.section,'amquser')
   amqpass = self.config_parser.get(self.section,'amqpass')
   amqvhost = self.config_parser.get(self.section,'amqvhost')
   amqexchange = self.config_parser.get(self.section,'amqexchange')
   routing_key = self.config_parser.get(self.section,'routing_key')
   connection = BrokerConnection(hostname=amqhost, port=amqport,
                           userid=amquser, password=amqpass,
                           virtual_host=amqvhost)
   channel = connection.channel()
   exchange = Exchange(amqexchange,'topic',turable=True)
   producer = Producer(channel,exchange=exchange, 
                          routing_key=routing_key)
   global entire_history
   entire_history = self.config_parser.get(self.section,'entire_history')
   for job in jobs():
      if job['type']=='start':
         job['completion_time']+=job['walltime']
      producer.revive(channel)
      producer.publish(job)
      logging.debug(`job`)
Beispiel #51
0
    def test_publish__consume(self):
        connection = Connection(transport=Transport)
        channel = connection.channel()
        producer = Producer(channel, self.exchange, routing_key='test_Redis')
        consumer = Consumer(channel, self.queue)

        producer.publish({'hello2': 'world2'})
        _received = []

        def callback(message_data, message):
            _received.append(message_data)
            message.ack()

        consumer.register_callback(callback)
        consumer.consume()

        self.assertIn(channel, channel.connection.cycle._channels)
        try:
            connection.drain_events(timeout=1)
            self.assertTrue(_received)
            with self.assertRaises(socket.timeout):
                connection.drain_events(timeout=0.01)
        finally:
            channel.close()
Beispiel #52
0
    def handle(self, *args, **options):
        routing_key = options["routing_key"]
        connection_url = options["connection_url"]
        userid = urlparse(connection_url).username
        payload_file = options["payload_file"]

        exchange_name = "exchange/{}/jobs".format(userid)

        connection = Connection(connection_url)
        exchange = Exchange(exchange_name, type="topic")
        producer = Producer(connection,
                            exchange,
                            routing_key=routing_key,
                            auto_declare=True)

        self.stdout.write("Published to exchange: {}".format(exchange_name))

        with open(payload_file) as f:
            body = f.read()

            try:
                producer.publish(body)
            finally:
                connection.release()
Beispiel #53
0
class EventDispatcher(object):
    """Send events as messages.

    :param connection: Connection to the broker.

    :keyword hostname: Hostname to identify ourselves as,
        by default uses the hostname returned by :func:`socket.gethostname`.

    :keyword enabled: Set to :const:`False` to not actually publish any events,
        making :meth:`send` a noop operation.

    :keyword channel: Can be used instead of `connection` to specify
        an exact channel to use when sending events.

    :keyword buffer_while_offline: If enabled events will be buffered
       while the connection is down. :meth:`flush` must be called
       as soon as the connection is re-established.

    You need to :meth:`close` this after use.

    """

    def __init__(self, connection=None, hostname=None, enabled=True,
            channel=None, buffer_while_offline=True, app=None,
            serializer=None):
        self.app = app_or_default(app)
        self.connection = connection
        self.channel = channel
        self.hostname = hostname or socket.gethostname()
        self.buffer_while_offline = buffer_while_offline
        self.mutex = threading.Lock()
        self.publisher = None
        self._outbound_buffer = deque()
        self.serializer = serializer or self.app.conf.CELERY_EVENT_SERIALIZER
        self.on_enabled = set()
        self.on_disabled = set()

        self.enabled = enabled
        if not connection and channel:
            self.connection = channel.connection.client
        if self.enabled:
            self.enable()

    def __enter__(self):
        return self

    def __exit__(self, *exc_info):
        self.close()

    def get_exchange(self):
        if self.connection:
            return get_exchange(self.connection)
        else:
            return get_exchange(self.channel.connection.client)

    def enable(self):
        self.publisher = Producer(self.channel or self.connection,
                                  exchange=self.get_exchange(),
                                  serializer=self.serializer)
        self.enabled = True
        for callback in self.on_enabled:
            callback()

    def disable(self):
        if self.enabled:
            self.enabled = False
            self.close()
            for callback in self.on_disabled:
                callback()

    def send(self, type, **fields):
        """Send event.

        :param type: Kind of event.
        :keyword \*\*fields: Event arguments.

        """
        if self.enabled:
            with self.mutex:
                event = Event(type, hostname=self.hostname,
                                    clock=self.app.clock.forward(), **fields)
                try:
                    self.publisher.publish(event,
                                           routing_key=type.replace("-", "."))
                except Exception, exc:
                    if not self.buffer_while_offline:
                        raise
                    self._outbound_buffer.append((type, fields, exc))