Class AbstractRemoteMetadataSource

java.lang.Object
org.dspace.importer.external.service.components.AbstractRemoteMetadataSource
Direct Known Subclasses:
AbstractImportMetadataSourceService

public abstract class AbstractRemoteMetadataSource extends Object
This class contains primitives to handle request timeouts and to retry requests. This is achieved by classifying exceptions as fatal or as non fatal/retryable. Evidently only subclasses can make the proper determination of what is retryable and what isn't. This is useful in case the service employs throttling and to deal with general network issues.
Author:
Roeland Dillen (roeland at atmire dot com), Antoine Snyers (antoine at atmire dot com)
  • Field Details

    • lastRequest

      protected long lastRequest
    • interRequestTime

      protected long interRequestTime
    • lock

      protected ReentrantLock lock
    • maxRetry

      protected int maxRetry
    • retry

      protected int retry
    • operationId

      protected String operationId
    • warning

      protected String warning
    • exceptionHandlersMap

      protected Map<Class,List<SourceExceptionHandler>> exceptionHandlersMap
    • error

      protected Exception error
  • Constructor Details

    • AbstractRemoteMetadataSource

      protected AbstractRemoteMetadataSource()
      Constructs an empty MetadataSource class object and initializes the Exceptionhandlers
  • Method Details

    • initExceptionHandlers

      protected void initExceptionHandlers()
      initialize the exceptionHandlersMap with an empty LinkedHashMap
    • getWarning

      public String getWarning()
      Return the warning message used for logging during exception catching
      Returns:
      a "warning" String
    • setWarning

      public void setWarning(String warning)
      Set the warning message used for logging
      Parameters:
      warning - warning message
    • getRetry

      public int getRetry()
      Return the number of retries that have currently been undertaken
      Returns:
      the number of retries
    • getMaxRetry

      public int getMaxRetry()
      Return the number of max retries that can be undertaken before separate functionality kicks in
      Returns:
      maximum number of retries
    • setMaxRetry

      public void setMaxRetry(int maxRetry)
      Set the number of maximum retries before throwing on the exception
      Parameters:
      maxRetry - maximum number of retries
    • getOperationId

      public String getOperationId()
      Retrieve the operationId
      Returns:
      A randomly generated UUID. generated during the retry method
    • getError

      public Exception getError()
      Retrieve the last encountered exception
      Returns:
      An Exception object, the last one encountered in the retry method
    • setError

      public void setError(Exception error)
      Set the last encountered error
      Parameters:
      error - exception to set
    • retry

      protected <T> T retry(Callable<T> callable) throws MetadataSourceException
      Command pattern implementation. the callable.call method will be retried until it either succeeds or reaches the try limit. Maybe this should have a backoff algorithm instead of waiting a fixed time.
      Type Parameters:
      T - return type. Generics for type safety.
      Parameters:
      callable - the callable to call. See the classes with the same name as the public methods of this class.
      Returns:
      The result of the call
      Throws:
      MetadataSourceException - if something unrecoverable happens (e.g . network failures)
    • handleException

      protected void handleException(int retry, Exception exception, String operationId) throws MetadataSourceException
      Handles a given exception or throws on a MetadataSourceException if no ExceptionHandler is set
      Parameters:
      retry - The number of retries before the exception was thrown on
      exception - The exception to handle
      operationId - The id of the operation that threw the exception
      Throws:
      MetadataSourceException - if no ExceptionHandler is configured for the given exception
    • getExceptionHandler

      protected List<SourceExceptionHandler> getExceptionHandler(Exception exception)
      Retrieve a list of SourceExceptionHandler objects that have an instanceof the exception configured to them.
      Parameters:
      exception - The exception to base the retrieval of SourceExceptionHandler on
      Returns:
      a list of SourceExceptionHandler objects
    • throwSourceException

      protected void throwSourceException(int retry, Exception exception, String operationId) throws MetadataSourceException
      Parameters:
      retry - The number of retries before the exception was thrown on
      exception - The exception to throw
      operationId - The id of the operation that threw the exception
      Throws:
      MetadataSourceException - if no ExceptionHandler is configured for the given exception
    • throwSourceExceptionHook

      protected void throwSourceExceptionHook()
      A specified point where methods can be specified or callbacks can be executed
    • init

      public abstract void init() throws Exception
      Attempts to init a session
      Throws:
      Exception - on generic exception
    • setInterRequestTime

      public void setInterRequestTime(long interRequestTime)