From 65d44b20fcdfc440b76d43a4cf37394cd49efa37 Mon Sep 17 00:00:00 2001 From: Bo-Chun Louis Chen <louistw@uab.edu> Date: Thu, 13 Feb 2020 01:01:55 -0600 Subject: [PATCH] Add class UserRegMQ with simple publish method --- usrreg_mq.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 usrreg_mq.py diff --git a/usrreg_mq.py b/usrreg_mq.py new file mode 100644 index 0000000..ab54a00 --- /dev/null +++ b/usrreg_mq.py @@ -0,0 +1,53 @@ +import rabbitmq_config as rcfg + +class UserRegMQ(object): + + USER = 'guest' + PASSWORD = 'guest' + HOST = 'localhost' + PORT = 5672 + VHOST = '/' + EXCHANGE = '' + EXCHANGE_TYPE = 'direct' + QUEUE = '' + + def __init__(self, config=None): + if config: + if 'exchange' in config: + self.EXCHANGE = config['exchange'] + if 'queue' in config: + self.QUEUE = config['queue'] + + hostname = socket.gethostname().split(".", 1)[0] + + self.HOST = rcfg.Server if hostname != rcfg.Server else "localhost" + self.USER = rcfg.User + self.PASSWORD = rcfg.Password + self.VHOST = rcfg.VHost + self.PORT = rcfg.Port + self._parameters = pika.ConnectionParameters( + self.HOST, + self.PORT, + self.VHOST, + pika.PlainCredentials(self.USER, self.PASSWORD)) + + def connect(self): + self._connection = pika.BlockingConnection(self._parameters) + self._channel = self._connection.channel() + self._channel.exchange_declare( + exchange=self.EXCHANGE, + exchange_type=self.EXCHANGE_TYPE + durable=True) + + def close(self): + self._connection.close() + + def publish_msg(self, obj): + if self._channel is None or not self._channel.is_open: + return + + # Establish connection to RabbitMQ server + self.connect() + self._channel.basic_publish(exchange=self.EXCHANGE, routing_key=obj['routing_key'], body=json.dumps(obj['msg'])) + self.disconnect() + -- GitLab