コード例 #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__ )
コード例 #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()
コード例 #3
0
ファイル: MQProducer.py プロジェクト: sparsh35/DIRAC
 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__)
コード例 #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()
コード例 #5
0
 def startConnection( self, mqURI, params, messengerType ):
   """ Function adds or updates the MQ connection. If the connection
       does not exists, MQconnector is created and added.
   Args:
     mqURI(str):
     params(dict): parameters to initialize the MQConnector.
     messengerType(str): 'consumer' or 'producer'.
   Returns:
     S_OK/S_ERROR: with the value of the messenger Id in S_OK.
   """
   self.lock.acquire()
   try:
     conn = getMQService( mqURI )
     if self.__connectionExists( conn ):
       return self.addNewMessenger( mqURI = mqURI, messengerType = messengerType )
     else: #Connection does not exist so we create the connector and we add a new connection
       result =  self.addNewMessenger( mqURI = mqURI, messengerType = messengerType )
       if not result['OK']:
         return result
       mId = result['Value']
       result = self.createConnectorAndConnect( parameters = params )
       if not result['OK']:
         return result
       if self.__getConnector( conn ):
         return S_ERROR( EMQCONN, "The connector already exists!" )
       self.__setConnector( conn, result['Value'] )
       return S_OK( mId )
   finally:
     self.lock.release()
コード例 #6
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()
コード例 #7
0
 def startConnection(self, mqURI, params, messengerType):
     """ Function adds or updates the MQ connection. If the connection
     does not exists, MQconnector is created and added.
 Args:
   mqURI(str):
   params(dict): parameters to initialize the MQConnector.
   messengerType(str): 'consumer' or 'producer'.
 Returns:
   S_OK/S_ERROR: with the value of the messenger Id in S_OK.
 """
     self.lock.acquire()
     try:
         conn = getMQService(mqURI)
         if self.__connectionExists(conn):
             return self.addNewMessenger(mqURI=mqURI,
                                         messengerType=messengerType)
         else:  #Connection does not exist so we create the connector and we add a new connection
             result = self.addNewMessenger(mqURI=mqURI,
                                           messengerType=messengerType)
             if not result['OK']:
                 return result
             mId = result['Value']
             result = self.createConnectorAndConnect(parameters=params)
             if not result['OK']:
                 return result
             if self.__getConnector(conn):
                 return S_ERROR(EMQCONN, "The connector already exists!")
             self.__setConnector(conn, result['Value'])
             return S_OK(mId)
     finally:
         self.lock.release()
コード例 #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!')
コード例 #9
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()
コード例 #10
0
ファイル: MQConsumer.py プロジェクト: DIRACGrid/DIRAC
 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!' )