Exemplo n.º 1
0
 def put(self, msg, msg_id=None):
     """ Put a message. """
     delivery_mode = 1
     if msg.header.get("persistent", "false") == "true":
         delivery_mode = 2
     header = dict()
     for key, val in msg.header.items():
         if type(key) == six.text_type:
             key = key.encode("utf-8")
         if type(val) == six.text_type:
             val = val.encode("utf-8")
         header[key] = val
     msg.header = header
     properties = self._pika.BasicProperties(timestamp=time.time(),
                                             headers=msg.header,
                                             delivery_mode=delivery_mode)
     if "content-type" in msg.header:
         content_type = msg.header["content-type"]
         if content_type.startswith("text/") or \
                 "charset=" in content_type:
             if not msg.is_text():
                 raise AmqpcltError("unexpected text content-type "
                                    "for binary message: %s" % content_type)
         else:
             if msg.is_text():
                 raise AmqpcltError("unexpected binary content-type for "
                                    "text message: %s" % content_type)
         properties.content_type = content_type
     elif msg.is_text():
         properties.content_type = "text/unknown"
     # Send the message
     if "destination" not in msg.header:
         raise AmqpcltError("message doesn't have a destination: %s" % msg)
     destination = common.parse_sender_destination(
         msg.header["destination"])
     if "queue" in destination:
         queue_name = self._maybe_declare_queue(destination["queue"])
     exch_name = self._maybe_declare_exchange(
         destination.get("exchange", dict()))
     if exch_name and "queue" in destination:
         self._maybe_bind(queue_name, exch_name,
                          destination.get("routing_key", ""))
     if type(msg.body) == six.text_type:
         body = msg.body.encode("utf-8")
     else:
         body = msg.body
     self._channel.basic_publish(exchange=exch_name,
                                 routing_key=destination.get(
                                     "routing_key", ""),
                                 body=body,
                                 properties=properties)
     if msg_id is None:
         return list()
     else:
         return [
             msg_id,
         ]
Exemplo n.º 2
0
 def put(self, msg, msg_id=None):
     """ Put a message. """
     delivery_mode = 1
     if msg.header.get("persistent", "false") == "true":
         delivery_mode = 2
     header = dict()
     for key, val in msg.header.items():
         if type(key) == unicode:
             key = key.encode("utf-8")
         if type(val) == unicode:
             val = val.encode("utf-8")
         header[key] = val
     msg.header = header
     properties = self._pika.BasicProperties(
         timestamp=time.time(),
         headers=msg.header,
         delivery_mode=delivery_mode)
     if "content-type" in msg.header:
         content_type = msg.header["content-type"]
         if content_type.startswith("text/") or \
                 "charset=" in content_type:
             if not msg.is_text():
                 raise AmqpcltError("unexpected text content-type "
                                    "for binary message: %s" % content_type)
         else:
             if msg.is_text():
                 raise AmqpcltError("unexpected binary content-type for "
                                    "text message: %s" % content_type)
         properties.content_type = content_type
     elif msg.is_text():
         properties.content_type = "text/unknown"
     # Send the message
     if "destination" not in msg.header:
         raise AmqpcltError("message doesn't have a destination: %s" % msg)
     destination = common.parse_sender_destination(
         msg.header["destination"])
     if "queue" in destination:
         queue_name = self._maybe_declare_queue(destination["queue"])
     exch_name = self._maybe_declare_exchange(
         destination.get("exchange", dict()))
     if exch_name and "queue" in destination:
         self._maybe_bind(queue_name,
                          exch_name,
                          destination.get("routing_key", ""))
     if type(msg.body) == unicode:
         body = msg.body.encode("utf-8")
     else:
         body = msg.body
     self._channel.basic_publish(
         exchange=exch_name,
         routing_key=destination.get("routing_key", ""),
         body=body,
         properties=properties)
     if msg_id is None:
         return list()
     else:
         return [msg_id, ]
Exemplo n.º 3
0
 def put(self, msg, msg_id=None):
     """ Put a message. """
     delivery_mode = 1
     if msg.header.get("persistent", "false") == "true":
         delivery_mode = 2
     # Send the message
     if "destination" not in msg.header:
         raise AmqpcltError("message doesn't have a destination: %s" % msg)
     destination = common.parse_sender_destination(
         msg.header["destination"])
     if "queue" in destination:
         queue_name = self._maybe_declare_queue(destination["queue"])
     exch_name = self._maybe_declare_exchange(
         destination.get("exchange", dict()))
     if exch_name and "queue" in destination:
         self._maybe_bind(queue_name, exch_name,
                          destination.get("routing_key", ""))
     extra = dict()
     if "content-type" in msg.header:
         content_type = msg.header["content-type"]
         if content_type.startswith("text/") or \
                 "charset=" in content_type:
             if not msg.is_text():
                 raise AmqpcltError("unexpected text content-type "
                                    "for binary message: %s" % content_type)
         else:
             if msg.is_text():
                 raise AmqpcltError("unexpected binary content-type for "
                                    "text message: %s" % content_type)
         extra["content_type"] = content_type
     elif msg.is_text():
         extra["content_type"] = "text/unknown"
     self._producer.publish(exchange=exch_name,
                            routing_key=destination.get("routing_key", ""),
                            delivery_mode=delivery_mode,
                            serializer=None,
                            compression=None,
                            headers=msg.header,
                            body=msg.body,
                            **extra)
     if msg_id is None:
         return list()
     else:
         return [
             msg_id,
         ]
Exemplo n.º 4
0
 def put(self, msg, msg_id=None):
     """ Put a message. """
     delivery_mode = 1
     if msg.header.get("persistent", "false") == "true":
         delivery_mode = 2
     # Send the message
     if "destination" not in msg.header:
         raise AmqpcltError("message doesn't have a destination: %s" % msg)
     destination = common.parse_sender_destination(
         msg.header["destination"])
     if "queue" in destination:
         queue_name = self._maybe_declare_queue(destination["queue"])
     exch_name = self._maybe_declare_exchange(
         destination.get("exchange", dict()))
     if exch_name and "queue" in destination:
         self._maybe_bind(queue_name,
                          exch_name,
                          destination.get("routing_key", ""))
     extra = dict()
     if "content-type" in msg.header:
         content_type = msg.header["content-type"]
         if content_type.startswith("text/") or \
                 "charset=" in content_type:
             if not msg.is_text():
                 raise AmqpcltError("unexpected text content-type "
                                    "for binary message: %s" % content_type)
         else:
             if msg.is_text():
                 raise AmqpcltError("unexpected binary content-type for "
                                    "text message: %s" % content_type)
         extra["content_type"] = content_type
     elif msg.is_text():
         extra["content_type"] = "text/unknown"
     self._producer.publish(
         exchange=exch_name,
         routing_key=destination.get("routing_key", ""),
         delivery_mode=delivery_mode,
         serializer=None,
         compression=None,
         headers=msg.header,
         body=msg.body, **extra)
     if msg_id is None:
         return list()
     else:
         return [msg_id, ]