Ejemplo n.º 1
0
 def receive(self, timeout=DEFAULT_TIMEOUT, logDebugMessages=True):
     """
     Receive the next message from any of the queues we're listening on.
     :param timeout: maximum time in seconds to wait for a message.
     :return: received message, None if timeout occurred.
     """
     self._check_session()
     if logDebugMessages:
         logger.debug("[FromBus] Waiting %s seconds for next message", timeout)
     try:
         recv = self.session.next_receiver(timeout)
         msg = recv.fetch(0)
     except qpid.messaging.exceptions.Empty:
         if logDebugMessages:
             logger.debug("[FromBus] No message received within %s seconds", timeout)
         return None
     except qpid.messaging.MessagingError:
         raise_exception(MessageBusError,
                         "[FromBus] Failed to fetch message from: "
                         "%s" % self.address) 
     logger.debug("[FromBus] Message received on: %s subject: %s" % (self.address, msg.subject))
     if logDebugMessages:
         logger.debug("[FromBus] %s" % msg)
     try:
         amsg = MESSAGE_FACTORY.create(msg)
     except MessageFactoryError:
         self.reject(msg)
         raise_exception(MessageBusError, "[FromBus] Message rejected")
     # self.ack(msg)
     return amsg
Ejemplo n.º 2
0
 def close(self):
     if (self.opened==1):
        try:
          if self.connection.opened():
             self.connection.close(DEFAULT_TIMEOUT)
        except qpid.messaging.exceptions.Timeout:
           raise_exception(MessageBusError,
                         "[ToBus] Failed to disconnect from broker %s" %
                         self.broker)
        finally:
           self.session = None
     self.opened-=1
Ejemplo n.º 3
0
 def open(self):
     if (self.opened==0):
         try:
             logger.debug("[ToBus] Connecting to broker: %s", self.broker)
             self.connection.open()
             logger.debug("[ToBus] Connected to broker: %s", self.broker)
             self.session = self.connection.session()
             logger.debug("[ToBus] Created session: %s", self.session.name)
             self._add_queue(self.address, self.options)
         except qpid.messaging.MessagingError:
             self.__exit__(*sys.exc_info())
             raise_exception(MessageBusError, "[ToBus] Initialization failed")
         except MessageBusError:
             self.__exit__(*sys.exc_info())
             raise
     self.opened+=1
Ejemplo n.º 4
0
 def send(self, message, timeout=DEFAULT_TIMEOUT):
     """
     Send a message to the exchange (target) we're connected to.
     :param message: message to be sent
     :param timeout: maximum time in seconds to wait for send action
     :return:
     """
     sender = self._get_sender()
     qmsg = to_qpid_message(message)
     logger.debug("[ToBus] Sending message to: %s (%s)", self.address, qmsg)
     try:
         sender.send(qmsg, timeout=timeout)
     except qpid.messaging.MessagingError:
         raise_exception(MessageBusError,
                         "[ToBus] Failed to send message to: %s" %
                         sender.target)
     logger.debug("[ToBus] Message sent to: %s subject: %s" % (self.address, message.subject))
Ejemplo n.º 5
0
    def _add_queue(self, address, options):
        """
        Add a queue that you want to sends messages to.
        :param address: valid Qpid address
        :param options: dict containing valid Qpid address options
        :raise MessageBusError: if sender could not be created
        """
        self._check_session()

        optstr = address_options_to_str(options)

        what = "sender for source: %s (broker: %s, session: %s, options: %s)" % \
               (address, self.broker, self.session.name, optstr)

        try:
            self.session.sender("%s; %s" % (address, optstr))
        except qpid.messaging.MessagingError:
            raise_exception(MessageBusError,
                            "[ToBus] Failed to create %s" % (what,))
        logger.debug("[ToBus] Created %s", what)
Ejemplo n.º 6
0
 def __exit__(self, exc_type, exc_val, exc_tb):
     """
     The following actions will be performed:
     * close the connection to the broker
     * set `session` and `sender` to None
     :param exc_type: type of exception thrown in context
     :param exc_val: value of exception thrown in context
     :param exc_tb: traceback of exception thrown in context
     :raise MessageBusError: if disconnect from broker fails
     """
     try:
         if self.connection.opened():
             self.connection.close(DEFAULT_TIMEOUT)
     except qpid.messaging.exceptions.Timeout:
         raise_exception(MessageBusError,
                         "[ToBus] Failed to disconnect from broker %s" %
                         self.broker)
     finally:
         self.session = None
     logger.debug("[ToBus] Disconnected from broker: %s", self.broker)
Ejemplo n.º 7
0
 def close(self):
     """
     The following actions will be performed:
     * close the connection to the broker
     * set session to None
     :param exc_type: type of exception thrown in context
     :param exc_val: value of exception thrown in context
     :param exc_tb: traceback of exception thrown in context
     """
     if (self.opened==1):
       try:
         if self.connection.opened():
             self.connection.close(DEFAULT_TIMEOUT)
       except qpid.messaging.exceptions.Timeout:
         raise_exception(MessageBusError,
                         "[FromBus] Failed to disconnect from broker: %s" %
                         self.broker)
       finally:
         self.session = None
       logger.debug("[FromBus] Disconnected from broker: %s", self.broker)
     self.opened-=1
Ejemplo n.º 8
0
    def add_queue(self, address, options=None):
        """
        Add a queue that you want to receive messages from.
        :param address: valid Qpid address
        :param options: dict containing valid Qpid address options
        """
        self._check_session()
        options = options if options else self.options

        # Extract capacity (not supported in address string in Python, see COMMON_OPTS in qpid/messaging/driver.py)
        capacity = options.pop("capacity", DEFAULT_RECEIVER_CAPACITY)

        optstr = address_options_to_str(options)

        what = "receiver for source: %s (broker: %s, session: %s, options: %s)" % \
               (address, self.broker, self.session.name, optstr)

        try:
            self.session.receiver("%s; %s" % (address, optstr), capacity=capacity)
        except qpid.messaging.MessagingError:
            raise_exception(MessageBusError,
                            "[FromBus] Failed to create %s" % (what,))
        logger.debug("[FromBus] Created %s", what)
Ejemplo n.º 9
0
 def open(self):
     """
     The following actions will be performed when entering a context:
     * connect to the broker
     * create a session
     * add a receiver
     The connection to the broker will be closed if any of these failed.
     :raise MessageBusError: if any of the above actions failed.
     :return: self
     """
     if (self.opened==0):
       try:
         self.connection.open()
         logger.debug("[FromBus] Connected to broker: %s", self.broker)
         self.session = self.connection.session()
         logger.debug("[FromBus] Created session: %s", self.session.name)
         self.add_queue(self.address, self.options)
       except qpid.messaging.MessagingError:
         self.__exit__(*sys.exc_info())
         raise_exception(MessageBusError, "[FromBus] Initialization failed")
       except MessageBusError:
         self.__exit__(*sys.exc_info())
         raise
     self.opened+=1