def publish(self,exchange_name,exchange_key,message,mheaders,mexp=0): try : if self.hc.use_pika : #self.logger.debug("publish PIKA is used") if mexp : expms = '%s' % mexp properties = pika.BasicProperties(content_type='text/plain', delivery_mode=1, headers=mheaders,expiration=expms) else: properties = pika.BasicProperties(content_type='text/plain', delivery_mode=1, headers=mheaders) self.channel.basic_publish(exchange_name, exchange_key, message, properties, True ) else: #self.logger.debug("publish AMQPLIB is used") if mexp : expms = '%s' % mexp msg = amqp.Message(message, content_type= 'text/plain',application_headers=mheaders,expiration=expms) else: msg = amqp.Message(message, content_type= 'text/plain',application_headers=mheaders) self.channel.basic_publish(msg, exchange_name, exchange_key ) self.channel.tx_commit() return True except : if self.hc.loop : (stype, value, tb) = sys.exc_info() self.logger.error("sr_amqp/publish: %s, Value: %s" % (stype, value)) self.logger.error("Sleeping 5 seconds ... and reconnecting") time.sleep(5) self.hc.reconnect() if self.hc.asleep : return False return self.publish(exchange_name,exchange_key,message,mheaders,mexp) else: (etype, evalue, tb) = sys.exc_info() self.logger.error("sr_amqp/publish 2 Type: %s, Value: %s" % (etype, evalue)) self.logger.error("could not publish %s %s %s %s" % (exchange_name,exchange_key,message,mheaders)) return False
def publish(self, exchange_name, exchange_key, message, mheaders, mexp=0): ebo=2 connected=True while True: if 'v03.' in exchange_key: ct='application/json' else: ct='text/plain' try: if self.hc.use_amqp: self.logger.debug("publish AMQP is used") if mexp: expms = '%s' % mexp msg = amqp.Message(message, content_type=ct, application_headers=mheaders, expiration=expms) else: msg = amqp.Message(message, content_type=ct, application_headers=mheaders) self.channel.basic_publish(msg, exchange_name, exchange_key) self.channel.tx_commit() elif self.hc.use_amqplib: self.logger.debug("publish AMQPLIB is used") if mexp: expms = '%s' % mexp msg = amqplib_0_8.Message(message, content_type=ct, application_headers=mheaders, expiration=expms) else: msg = amqplib_0_8.Message(message, content_type=ct, application_headers=mheaders) self.channel.basic_publish(msg, exchange_name, exchange_key) self.channel.tx_commit() elif self.hc.use_pika: self.logger.debug("publish PIKA is used") if mexp: expms = '%s' % mexp properties = pika.BasicProperties(content_type=ct, delivery_mode=1, headers=mheaders, expiration=expms) else: properties = pika.BasicProperties(content_type=ct, delivery_mode=1, headers=mheaders) self.channel.basic_publish(exchange_name, exchange_key, message, properties, True) else: self.logger.debug("Couldn't choose an AMQP client library, setting it back to default amqp") self.hc.use_amqp = True raise ConnectionError("No AMQP client library is set") return True except Exception as err: if ebo < 65: ebo = ebo * 2 self.logger.error("sr_amqp/publish: Sleeping %d seconds ... and reconnecting" % ebo) self.logger.debug('Exception details: ', exc_info=True) time.sleep(ebo) connected=False if not connected: self.hc.reconnect()
def say(body, rk='', config=None): if config is not None: headers = {PLUGIN_CONFIG_HEADER: config} self.channel.basic_publish( amqp.Message(body=body, application_headers=headers), exchange, routing_key=rk) else: self.channel.basic_publish(amqp.Message(body=body), exchange, routing_key=rk)
def put(self, key, message, correlationId=None): if not correlationId and self.autoCorrelationId: correlationId = getUuid() # str(uuid.uuid4()) if correlationId: msg = amqp.Message(json.encode(message), content_type='text/json', correlation_id=correlationId) else: msg = amqp.Message(json.encode(message), content_type='text/json') self.ch.basic_publish(msg, self.exchange, key) return correlationId
def main(args=sys.argv[1:], optparse=hammer_options, getpassword=getpass.getpass, connect=amqp.Connection): options, messages = optparse(args) if options.password is None: options.password = getpassword() con = connect(host=options.host, userid=options.userid, password=options.password, virtual_host=options.vhost) try: chan = con.channel() try: for message in messages: for repeat in xrange(options.number): amq_message = amqp.Message(message) amq_message.properties['delivery_mode'] = 2 chan.basic_publish(amq_message, exchange=options.exchange, routing_key=options.routing_key) finally: chan.close() finally: con.close()
def sendMsg(self, msg, routing_key=None, hdrs=None, content_type='text/plain', **kw): #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - assert self.ch is not None, 'sendMsg called when channel not initialized' if hdrs is None: hdrs = {} if routing_key is None: routing_key = self.key assert routing_key is not None, 'sendMsg called with no routing_key nor default routing key' assert self.dbgMsg(msg, routing_key=routing_key, hdrs=hdrs, content_type=content_type, **kw) amsg = amqp.Message(msg, content_type=content_type, application_headers=hdrs, **kw) self.ch.basic_publish(amsg, self.xchg, routing_key=routing_key)
def _rpc_send_response(self, msg, result): #print "responding to",msg.reply_to,msg.message_id,"with",result resp = amqp.Message(body=dumps(("response", result)), message_id=msg.message_id) self._rpc_channel.basic_publish(resp, exchange="amq.direct", routing_key=msg.reply_to)
def send2msg(yml_temp_name, args, alias): conn = amqp.Connection(host="127.0.0.1:5672", userid="guest", password="******", virtual_host="/", insist=False) chan = conn.channel() # cankao http://blog.csdn.net/linvo/article/details/5750987 #插入数据任务状态 print 0 job = Jobs() id = job.Insdate(object=alias, started=datetime.now(), finished="0", template_name=yml_temp_name, args=pickle.dumps(args), status=0) print 1 msg = (yml_temp_name, args, id) print msg msg = amqp.Message(pickle.dumps(msg)) msg.properties["delivery_mode"] = 2 chan.basic_publish(msg, exchange="sorting_room", routing_key="json") chan.close() conn.close()
def sendNow(self, data): """Immediately send a message to the broker.""" json_data = json.dumps(data) msg = amqp.Message(json_data) self.channel.basic_publish(exchange=self.session.exchange, routing_key=self.key, msg=msg)
def _add_item(routing_key, body, message_id=None, delivery_mode=DELIVERY_DURABLE): """adds an item onto a queue. If the connection to amqp is lost it will try to reconnect and then call itself again.""" if not amqp_host: log.error("Ignoring amqp message %r to %r" % (body, routing_key)) return chan = connection_manager.get_channel() msg = amqp.Message(body, timestamp=datetime.now(), delivery_mode=delivery_mode) if message_id: msg.properties['message_id'] = message_id try: chan.basic_publish(msg, exchange=amqp_exchange, routing_key=routing_key) except Exception as e: if e.errno == errno.EPIPE: connection_manager.get_channel(True) add_item(routing_key, body, message_id) else: raise
def send(self, event, kw): full_event = STAMPEDE_NS + event self._log.trace("send.start event=%s", full_event) data = self._encoder(event=event, **kw) self._channel.basic_publish(amqp.Message(body=data), exchange=self._exch, routing_key=full_event) self._log.trace("send.end event=%s", event)
def _add_item(routing_key, body, message_id=None, delivery_mode=DELIVERY_DURABLE, headers=None, exchange=amqp_exchange): """adds an item onto a queue. If the connection to amqp is lost it will try to reconnect and then call itself again.""" if not amqp_host: log.error("Ignoring amqp message %r to %r" % (body, routing_key)) return chan = connection_manager.get_channel() msg = amqp.Message(body, timestamp=datetime.now(), delivery_mode=delivery_mode) if message_id: msg.properties['message_id'] = message_id if headers: msg.properties["application_headers"] = headers event_name = 'amqp.%s' % routing_key try: chan.basic_publish(msg, exchange=exchange, routing_key=routing_key) except Exception as e: stats.event_count(event_name, 'enqueue_failed') if e.errno == errno.EPIPE: connection_manager.get_channel(True) add_item(routing_key, body, message_id) else: raise else: stats.event_count(event_name, 'enqueue')
def handle_scan(states, amqp_config, barcode): if states.get(barcode[:2], None): values = dict(BARCODE=barcode[2:], STATE=states.get(barcode[:2]), API_KEY=amqp_config['api_key']) print values data = xml % values print data conn = amqp.Connection(host=amqp_config['host'] + ":" + amqp_config['port'], userid=amqp_config['userid'], password=amqp_config['password'], virtual_host=amqp_config['virtual_host'], insist=False) chan = conn.channel() msg = amqp.Message( data, content_type='text/plain', application_headers={'msg_type': 'sample_state_update'}) msg.properties["delivery_mode"] = 2 chan.basic_publish(msg, exchange=amqp_config['exchange'], routing_key=amqp_config['routing_key']) chan.close() conn.close()
def publish(self, exchange_name, exchange_key, message, headers): try: msg = amqp.Message(message, content_type='text/plain', application_headers=headers) self.channel.basic_publish(msg, exchange_name, exchange_key) self.channel.tx_commit() return True except: if self.hc.loop: (stype, value, tb) = sys.exc_info() self.logger.error("Type: %s, Value: %s" % (stype, value)) self.logger.error("Sleeping 5 seconds ... and reconnecting") time.sleep(5) self.hc.reconnect() if self.hc.asleep: return False return self.publish(exchange_name, exchange_key, message, headers) else: (etype, evalue, tb) = sys.exc_info() self.logger.error("Type: %s, Value: %s" % (etype, evalue)) self.logger.error( "could not publish %s %s %s %s" % (exchange_name, exchange_key, message, headers)) return False
def main(): server = {'host': HOST, 'userid': USER, 'password': PAWD, 'ssl': False} x_name = 'ops.collector.ex' routing_key = "ops.mt.k" conn = amqp.Connection(server['host'], userid=server['userid'], password=server['password'], ssl=server['ssl'] ) ch = conn.channel() ch.access_request('/data', active=True, write=True) ch.exchange_declare(exchange=x_name, type='topic', durable=True, auto_delete=False) retry = True import time headers = {"timestamp_in_ms": int(time.time()), "platformid": "mooooooo-oooo-oooo-oooo-defaultplatf"} count = 0 while retry: msg_body = input('>') # msg_body = str(time.time()) count += 1 msg_body = str(count) + "," + str(time.time()) + "." msg = amqp.Message(msg_body, content_encoding='UTF-8', application_headers=headers) msg.properties['delivery_mode'] = 2 ch.basic_publish(msg, x_name, routing_key) # if msg_body == 'quit': # retry = False print(count) if count == 200: break ch.close() conn.close()
def basic_publish(ch, qname, min_size, max_size, persistent): body = 'a' * random.randint(min_size, max_size) msg = amqp.Message(body, application_headers={}) #if persistent: if True: msg.properties["delivery_mode"] = 2 ch.basic_publish(msg, '', routing_key=qname)
def send_test_request(self, src, arch, trigger, huge=False): '''Send out AMQP request for testing src/arch for trigger If huge is true, then the request will be put into the -huge instead of normal queue. ''' if self.options.dry_run: return params = {'triggers': [trigger]} if self.options.adt_ppas: params['ppas'] = self.options.adt_ppas qname = 'debci-ppa-%s-%s' % (self.options.series, arch) elif huge: qname = 'debci-huge-%s-%s' % (self.options.series, arch) else: qname = 'debci-%s-%s' % (self.options.series, arch) params['submit-time'] = datetime.strftime(datetime.utcnow(), '%Y-%m-%d %H:%M:%S%z') params = json.dumps(params) if self.amqp_channel: self.amqp_channel.basic_publish(amqp.Message(src + '\n' + params, delivery_mode=2), # persistent routing_key=qname) else: assert self.amqp_file with open(self.amqp_file, 'a') as f: f.write('%s:%s %s\n' % (qname, src, params))
def sync(self): """Sync the in-memory list of OOPS with the external OOPS source.""" if not self.channel: return # Send ourselves a message: when we receive this, we've processed all # oopses created before sync() was invoked. message = amqp.Message(self.AMQP_SENTINEL) # Match what oops publishing does message.properties["delivery_mode"] = 2 # Publish the message via a new channel (otherwise rabbit # shortcircuits it straight back to us, apparently). connection = connect() try: channel = connection.channel() try: channel.basic_publish(message, config.error_reports.error_exchange, config.error_reports.error_queue_key) finally: channel.close() finally: connection.close() receiver = oops_amqp.Receiver(self.oops_config, connect, self.queue_name) receiver.sentinel = self.AMQP_SENTINEL try: receiver.run_forever() finally: # Ensure we leave the queue ready to roll, or later calls to # sync() will fail. self.setUpQueue()
def send_job(self, job_id): try: conn = amqp.Connection( self.rabbitMQConf['hostPort'], self.rabbitMQConf['user'], self.rabbitMQConf['pass'], virtualHost=self.rabbitMQConf['virtualHost'], insist=False) chan = conn.channel() chan.access_request(self.rabbitMQConf['virtualHost']) new_message = amqp.Message(job_id) chan.basic_publish(new_message, exchange=self.queueConf['exchange']) chan.close() conn.close() # $msg = new AMQPMessage($userId, array('content_type' = > 'text/plain')); # $channel->basic_publish($msg, $this->queueConf['exchange']); # $channel->close(); # $conn->close(); return True except Exception as exc: print("send_Exception") print(exc)
def prepare_message(self, body, priority=None, content_type=None, content_encoding=None, headers=None, properties=None): """Encapsulate data into a AMQP message.""" return amqp.Message(body, priority=priority, content_type=content_type, content_encoding=content_encoding, application_headers=headers, **properties)
def send_message(chan, exname, counter=None, message=None): """ publish a message on the exchange """ if not message: message = "This is test message %d" % counter msg = amqp.Message(message) chan.basic_publish(msg, exchange=exname, routing_key=ROUTE_KEY) if options.verbose: print "Sent message: %s" % message
def offer_delete(self, offer_Id, campaign_id): ch = self._get_channel() msg = 'Offer:%s,Campaign:%s' % (offer_Id, campaign_id) msg = amqp.Message(msg) ch.basic_publish(msg, exchange='getmyad', routing_key='advertise.delete') ch.close()
def publish(self, kind, message_data): encoded = simplejson.dumps({'kind': kind, 'data': message_data}) message = amqp.Message(encoded) message.properties['delivery_mode'] = self.delivery_mode self.channel.basic_publish(message, exchange=self.exchange, routing_key=self.routing_key) return message
def _rpc_send_call(self, service, parts): self._rpc_id += 1 msg = amqp.Message(body=dumps(parts), reply_to=self._rpc_queue, message_id=str(self._rpc_id)) self._rpc_channel.basic_publish(msg, exchange="amq.direct", routing_key=service)
def campaign_update(self, campaign_id): ''' Отправляет уведомление об обновлении рекламной кампании ``campaign_id`` ''' ch = self._get_channel() msg = amqp.Message(campaign_id) ch.basic_publish(msg, exchange='getmyad', routing_key='campaign.update') ch.close()
def informer_update(self, informer_id): ''' Отправляет уведомление о том, что информер ``informer_id`` был изменён ''' ch = self._get_channel() msg = amqp.Message(informer_id) ch.basic_publish(msg, exchange='getmyad', routing_key='informer.update') ch.close()
def prepare_message(self, message_data, delivery_mode, priority=None, content_type=None, content_encoding=None): """Encapsulate data into a AMQP message.""" message = amqp.Message(message_data, priority=priority, content_type=content_type, content_encoding=content_encoding) message.properties["delivery_mode"] = delivery_mode return message
def informer_rating_update(self, id): ''' Отправляет уведомление об изменении в аккаунте ``login`` ''' ch = self._get_channel() msg = amqp.Message(id) ch.basic_publish(msg, exchange='getmyad', routing_key='informer.updateRating') ch.close()
def send2amqp(yml_temp_name, args): started = str(int(time.time())) g.mysql_db.execute("INSERT INTO jobs(started, template_name, args) \ VALUES(%s, %s, %s)", started, yml_temp_name, pickle.dumps(args)) body = (started, yml_temp_name, args) msg = amqp.Message(pickle.dumps(body)) msg.properties['delivery_mode'] = 2 #持久化消息 g.amqp_chan.basic_publish(msg, exchange="sorting_room", routing_key="json")
def emit(chan, ex, rkey, payload, content_type="application/octet-stream"): log.info("---> ex=%r rkey=%r c/t=%r: %r", ex, rkey, content_type, payload) msg = amqp.Message(payload.encode("utf-8")) msg.properties["content_type"] = content_type msg.properties["delivery_mode"] = 2 msg.properties["app_id"] = u"edilib" chan.basic_publish(exchange=ex, routing_key=rkey, msg=msg)