示例#1
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'])
     }
示例#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')  
示例#3
0
 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)
示例#4
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',
                )
示例#5
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)
示例#6
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)
示例#7
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
示例#8
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()
    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()
    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,
            )
示例#11
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')
示例#12
0
 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")
示例#13
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")
示例#14
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')
示例#15
0
    def test_produce_consume(self):
        channel = self.c.channel()
        producer = Producer(channel, self.e)
        consumer1 = Consumer(channel, self.q)
        consumer2 = Consumer(channel, self.q2)
        self.q2(channel).declare()

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

        _received1 = []
        _received2 = []

        def callback1(message_data, message):
            _received1.append(message)
            message.ack()

        def callback2(message_data, message):
            _received2.append(message)
            message.ack()

        consumer1.register_callback(callback1)
        consumer2.register_callback(callback2)

        consumer1.consume()
        consumer2.consume()

        while 1:
            if len(_received1) + len(_received2) == 20:
                break
            self.c.drain_events()

        self.assertEqual(len(_received1) + len(_received2), 20)

        # compression
        producer.publish({"compressed": True},
                         routing_key="test_transport_memory",
                         compression="zlib")
        m = self.q(channel).get()
        self.assertDictEqual(m.payload, {"compressed": True})

        # queue.delete
        for i in range(10):
            producer.publish({"foo": i}, routing_key="test_transport_memory")
        self.assertTrue(self.q(channel).get())
        self.q(channel).delete()
        self.q(channel).declare()
        self.assertIsNone(self.q(channel).get())

        # queue.purge
        for i in range(10):
            producer.publish({"foo": i}, routing_key="test_transport_memory2")
        self.assertTrue(self.q2(channel).get())
        self.q2(channel).purge()
        self.assertIsNone(self.q2(channel).get())
示例#16
0
 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")
示例#17
0
    def test_on_return(self):
        chan = self.connection.channel()

        def on_return(exception, exchange, routing_key, message):
            pass

        p = Producer(chan, on_return=on_return)
        self.assertTrue(on_return in chan.events["basic_return"])
        self.assertTrue(p.on_return)
示例#18
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')
     self.publish = connection.ensure(self.producer, self.producer.publish, errback=self.errback, max_retries=3)
示例#19
0
    def run(self):
        try:
            result = {
                "result": self.rpc._local_call(self.body["method"], *self.body["args"], **self.body["kwargs"]),
                "request_id": self.body["request_id"],
                "exception": None,
            }
        except Exception as e:
            import sys
            import traceback

            exc_info = sys.exc_info()
            backtrace = "\n".join(traceback.format_exception(*(exc_info or sys.exc_info())))

            # Utility to generate human readable errors
            def translate_error(err):
                from socket import error as socket_error

                if type(err) == socket_error:
                    return "Cannot reach server"

                return str(err)

            result = {
                "request_id": self.body["request_id"],
                "result": None,
                "exception": translate_error(e),
                "exception_type": type(e).__name__,
                "traceback": backtrace,
            }
            log.error("RunOneRpc: exception calling %s: %s" % (self.body["method"], backtrace))
        finally:
            django.db.connection.close()

        with self._response_conn_pool[_amqp_connection()].acquire(block=True) as connection:

            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}

            connection.ensure_connection(**retry_policy)

            with Producer(connection) as producer:

                maybe_declare(_amqp_exchange(), producer.channel, True, **retry_policy)
                producer.publish(
                    result,
                    serializer="json",
                    routing_key=self.body["response_routing_key"],
                    delivery_mode=TRANSIENT_DELIVERY_MODE,
                    retry=True,
                    retry_policy=retry_policy,
                    immedate=True,
                    mandatory=True,
                )
示例#20
0
文件: queue.py 项目: sjl421/codes
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)
 def publish(self, data, timeout=None):
     p = Producer(channel=self.channel,
                  exchange=Exchange(self.target.exchange_name, type=self.EXCHANGE_TYPE,
                                    durable=self.options['durable'],
                                    auto_delete=self.options['auto_delete']),
                  routing_key=self.target.routing_key)
     if timeout:
         p.publish(data, headers={'ttl': (timeout * 1000)})
     else:
         p.publish(data)
示例#22
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')
    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()
示例#24
0
文件: pidbox.py 项目: jason790/kombu
 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())
示例#26
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)
示例#27
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')
示例#28
0
def init(host, port, virtual_host, usr, psw, queue_name):
    global connection, channel, producer, task_queue, consumer
    connection = Connection(hostname=host,
                            port=port,
                            userid=usr,
                            password=psw,
                            virtual_host=virtual_host)
    channel = connection.channel()
    producer = Producer(channel)
    task_queue = Queue(queue_name, durable=True)
    consumer = Consumer(channel, task_queue, no_ack=False)
    consumer.qos(prefetch_count=1)
    consumer.register_callback(RequestCallBack)
示例#29
0
    def _send(self, connection, request):
        """
        :param request: JSON serializable dict

        """
        log.debug("send %s" % request['request_id'])
        request['response_routing_key'] = self._response_routing_key
        with Producer(connection) as producer:
            maybe_declare(_amqp_exchange(), producer.channel)
            producer.publish(request,
                             serializer="json",
                             routing_key=self._request_routing_key,
                             delivery_mode=1)
示例#30
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')