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 Summary
Fields Modifier and Type Field Description protected Exceptionerrorprotected Map<Class,List<SourceExceptionHandler>>exceptionHandlersMapprotected longinterRequestTimeprotected longlastRequestprotected ReentrantLocklockprotected intmaxRetryprotected StringoperationIdprotected intretryprotected Stringwarning
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractRemoteMetadataSource()Constructs an empty MetadataSource class object and initializes the Exceptionhandlers
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description ExceptiongetError()Retrieve the last encountered exceptionprotected List<SourceExceptionHandler>getExceptionHandler(Exception exception)Retrieve a list of SourceExceptionHandler objects that have an instanceof the exception configured to them.intgetMaxRetry()Return the number of max retries that can be undertaken before separate functionality kicks inStringgetOperationId()Retrieve the operationIdintgetRetry()Return the number of retries that have currently been undertakenStringgetWarning()Return the warning message used for logging during exception catchingprotected voidhandleException(int retry, Exception exception, String operationId)Handles a given exception or throws on aMetadataSourceExceptionif no ExceptionHandler is setabstract voidinit()Attempts to init a sessionprotected voidinitExceptionHandlers()initialize the exceptionHandlersMap with an emptyLinkedHashMapprotected <T> Tretry(Callable<T> callable)Command pattern implementation.voidsetError(Exception error)Set the last encountered errorvoidsetInterRequestTime(long interRequestTime)voidsetMaxRetry(int maxRetry)Set the number of maximum retries before throwing on the exceptionvoidsetWarning(String warning)Set the warning message used for loggingprotected voidthrowSourceException(int retry, Exception exception, String operationId)Throw aMetadataSourceExceptionprotected voidthrowSourceExceptionHook()A specified point where methods can be specified or callbacks can be executed
-
-
-
Field Detail
-
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
-
-
Method Detail
-
initExceptionHandlers
protected void initExceptionHandlers()
initialize the exceptionHandlersMap with an emptyLinkedHashMap
-
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 MetadataSourceExceptionHandles a given exception or throws on aMetadataSourceExceptionif no ExceptionHandler is set- Parameters:
retry- The number of retries before the exception was thrown onexception- The exception to handleoperationId- 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 ofSourceExceptionHandleron- Returns:
- a list of
SourceExceptionHandlerobjects
-
throwSourceException
protected void throwSourceException(int retry, Exception exception, String operationId) throws MetadataSourceExceptionThrow aMetadataSourceException- Parameters:
retry- The number of retries before the exception was thrown onexception- The exception to throwoperationId- 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 ExceptionAttempts to init a session- Throws:
Exception- on generic exception
-
setInterRequestTime
public void setInterRequestTime(long interRequestTime)
-
-