Ejemplo n.º 1
0
 def __init__( self, mqManager, mqURI, producerId ):
   self._connectionManager = mqManager
   self._mqURI = mqURI
   self._destination = getDestinationAddress( self._mqURI )
   self._mqService = getMQService( self._mqURI )
   self._id = producerId
   self.log = gLogger.getSubLogger( self.__class__.__name__ )
Ejemplo n.º 2
0
 def addNewMessenger( self, mqURI, messengerType ):
   """ Function updates the MQ connection by adding the messenger Id to the internal connection storage.
       Also the messengerId is chosen.
       messenger Id is set to the maximum existing value (or 0 no messengers are connected) + 1.
       messenger Id is calculated separately for consumers and producers
   Args:
     mqURI(str):
     messengerType(str): 'consumer' or 'producer'.
   Returns:
     S_OK: with the value of the messenger Id or S_ERROR if the messenger was not added,
           cause the same id already exists.
   """
   # 'consumer1' ->1
   # 'producer21' ->21
   msgIdToInt = lambda msgIds, msgType : [int(m.replace( msgType,'' )) for m in msgIds]
   # The messengerId is str e.g.  'consumer5' or 'producer3'
   generateMessengerId = lambda msgT: msgT + str( max( msgIdToInt( self.__getAllMessengersIdWithType( msgT ), msgT ) or [0] ) + 1 )
   self.lock.acquire( )
   try:
     conn = getMQService( mqURI )
     dest = getDestinationAddress( mqURI )
     mId = generateMessengerId( messengerType )
     if self.__addMessenger( conn, dest, mId ):
       return S_OK( mId )
     return S_ERROR( EMQCONN, "Failed to update the connection: the messenger %s  already exists"%  mId )
   finally:
     self.lock.release()
Ejemplo n.º 3
0
 def __init__(self, mqManager, mqURI, producerId):
     self._connectionManager = mqManager
     self._mqURI = mqURI
     self._destination = getDestinationAddress(self._mqURI)
     self._mqService = getMQService(self._mqURI)
     self._id = producerId
     self.log = gLogger.getSubLogger(self.__class__.__name__)
Ejemplo n.º 4
0
  def stopConnection( self, mqURI, messengerId ):
    """ Function 'stops' the connection for given messenger, which means
        it removes it from the messenger list. If this is the consumer, the
        unsubscribe() connector method is called. If it is the last messenger
        of this destination (queue or topic), then the destination is removed.
        If it is the last destination from this connection. The disconnect function
        is called and the connection is removed.
    Args:
      mqURI(str):
      messengerId(str): e.g. 'consumer1' or 'producer10'.
    Returns:
      S_OK: with the value of the messenger Id or S_ERROR if the messenger was not added,
            cause the same id already exists.
    """
    self.lock.acquire()
    try:
      conn = getMQService( mqURI )
      dest = getDestinationAddress( mqURI )
      connector = self.__getConnector( conn )

      if not self.__removeMessenger( conn, dest, messengerId ):
        return S_ERROR( EMQCONN, 'Failed to stop the connection!The messenger %s does not exist!' % messengerId )
      else:
        if 'consumer' in messengerId:
          result = self.unsubscribe( connector, destination = dest, messengerId = messengerId )
          if not result['OK']:
            return result
      if not self.__connectionExists( conn ):
        return self.disconnect( connector)
      return S_OK()
    finally:
      self.lock.release()
Ejemplo n.º 5
0
 def addNewMessenger(self, mqURI, messengerType):
     """ Function updates the MQ connection by adding the messenger Id to the internal connection storage.
     Also the messengerId is chosen.
     messenger Id is set to the maximum existing value (or 0 no messengers are connected) + 1.
     messenger Id is calculated separately for consumers and producers
 Args:
   mqURI(str):
   messengerType(str): 'consumer' or 'producer'.
 Returns:
   S_OK: with the value of the messenger Id or S_ERROR if the messenger was not added,
         cause the same id already exists.
 """
     # 'consumer1' ->1
     # 'producer21' ->21
     msgIdToInt = lambda msgIds, msgType: [
         int(m.replace(msgType, '')) for m in msgIds
     ]
     # The messengerId is str e.g.  'consumer5' or 'producer3'
     generateMessengerId = lambda msgT: msgT + str(
         max(
             msgIdToInt(self.__getAllMessengersIdWithType(msgT), msgT) or
             [0]) + 1)
     self.lock.acquire()
     try:
         conn = getMQService(mqURI)
         dest = getDestinationAddress(mqURI)
         mId = generateMessengerId(messengerType)
         if self.__addMessenger(conn, dest, mId):
             return S_OK(mId)
         return S_ERROR(
             EMQCONN,
             "Failed to update the connection: the messenger %s  already exists"
             % mId)
     finally:
         self.lock.release()
Ejemplo n.º 6
0
 def __init__(self,
              mqManager,
              mqURI,
              consumerId,
              callback=generateDefaultCallback()):
     self._connectionManager = mqManager
     self._mqURI = mqURI
     self._destination = getDestinationAddress(self._mqURI)
     self._id = consumerId
     self._callback = callback
     self.log = gLogger.getSubLogger(self.__class__.__name__)
     #subscribing to connection
     result = self._connectionManager.getConnector(getMQService(
         self._mqURI))
     if result['OK']:
         connector = result['Value']
         if connector:
             result = connector.subscribe(
                 parameters={
                     'messengerId': self._id,
                     'callback': callback,
                     'destination': self._destination
                 })
             if not result['OK']:
                 self.log.error('Failed to subscribe the consumer:' +
                                self._id)
         else:
             self.log.error(
                 'Failed to initialize MQConsumer! No MQConnector!')
     else:
         self.log.error('Failed to get MQConnector!')
Ejemplo n.º 7
0
  def stopConnection(self, mqURI, messengerId):
    """ Function 'stops' the connection for given messenger, which means
        it removes it from the messenger list. If this is the consumer, the
        unsubscribe() connector method is called. If it is the last messenger
        of this destination (queue or topic), then the destination is removed.
        If it is the last destination from this connection. The disconnect function
        is called and the connection is removed.

    Args:
      mqURI(str):
      messengerId(str): e.g. 'consumer1' or 'producer10'.
    Returns:
      S_OK: with the value of the messenger Id or S_ERROR if the messenger was not added,
            cause the same id already exists.
    """
    self.lock.acquire()
    try:
      conn = getMQService(mqURI)
      dest = getDestinationAddress(mqURI)
      connector = self.__getConnector(conn)

      if not self.__removeMessenger(conn, dest, messengerId):
        return S_ERROR(EMQCONN, 'Failed to stop the connection!The messenger %s does not exist!' % messengerId)
      else:
        if 'consumer' in messengerId:
          result = self.unsubscribe(connector, destination=dest, messengerId=messengerId)
          if not result['OK']:
            return result
      if not self.__connectionExists(conn):
        return self.disconnect(connector)
      return S_OK()
    finally:
      self.lock.release()
Ejemplo n.º 8
0
 def __init__( self, mqManager, mqURI, consumerId, callback = generateDefaultCallback() ):
   self._connectionManager = mqManager
   self._mqURI = mqURI
   self._destination = getDestinationAddress( self._mqURI )
   self._id = consumerId
   self._callback = callback
   self.log = gLogger.getSubLogger( self.__class__.__name__ )
   #subscribing to connection
   result =  self._connectionManager.getConnector( getMQService( self._mqURI ) )
   if result['OK']:
     connector = result['Value']
     if connector:
       result = connector.subscribe( parameters = {'messengerId':self._id, 'callback':callback, 'destination':self._destination} )
       if not result['OK']:
         self.log.error( 'Failed to subscribe the consumer:' + self._id )
     else:
       self.log.error( 'Failed to initialize MQConsumer! No MQConnector!' )
   else:
     self.log.error( 'Failed to get MQConnector!' )