Class ResourceRegistry
java.lang.Object
com.linkedin.alpini.base.registry.ResourceRegistry
- Direct Known Subclasses:
SyncResourceRegistry
A registry to manage
Shutdownable resources.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFactories must implement this interface.static interfaceResources which implementResourceRegistry.ShutdownFirstwill be shut down during the first phase.static interfaceResources which implementResourceRegistry.ShutdownLastwill be shut down during the last phase.static interfaceResources which implementResourceRegistry.Syncwill be shut down after preceding resources have finished shutting down and further resources will shut down after it has completed shutting down. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final com.linkedin.alpini.base.registry.ResourceRegistry.Statestatic final String -
Constructor Summary
ConstructorsConstructorDescriptionConstruct a ResourceRegistry class instance.ResourceRegistry(boolean garbageCollectable) Construct a ResourceRegistry class instance. -
Method Summary
Modifier and TypeMethodDescription<R extends ShutdownableResource,F extends ResourceRegistry.Factory<R>>
FReturn a factory which implements the requested factory class F.static longGet the delay for global shutdown.static voidStarts shutdown for all ResourceRegistry instances.static booleanisFactoryInterface(Class<?> clazz) Checks if the supplied class is a permissable factory class.final booleanReturns true if this resource has been shut down.final booleanReturns true if the resource has completed shutting down.protected Iterator<ShutdownableResource>iterator()<R extends ShutdownableResource>
Rregister(R resource) Register aShutdownableResourceresource with thisResourceRegistry.static <R extends ShutdownableResource,F extends ResourceRegistry.Factory<R>>
FregisterFactory(Class<F> clazz, F factory) Register the factory with theResourceRegistrysuch that future invocations offactory(Class)will return an appropiate proxy for the factory.<R extends ShutdownableResource>
voidremove(R resource) Removes the specifiedShutdownableResourcefrom thisResourceRegistry.static voidsetGlobalShutdownDelayMillis(long globalShutdownDelayMillis) Set a delay for global shutdown.voidshutdown()Starts the shutdown process.toString()voidvacuum()Vacuums any already-terminated resources.voidWaits for shutdown to completevoidwaitForShutdown(long timeoutInMs) Waits for shutdown to complete with a timeoutvoidWaits until another caller callsshutdown()on this resource registry.voidwaitToStartShutdown(long timeoutInMs) Waits until another caller callsshutdown()on this resource registry.
-
Field Details
-
SHUTDOWN_THREAD_PREFIX
-
_state
protected final com.linkedin.alpini.base.registry.ResourceRegistry.State _state
-
-
Constructor Details
-
ResourceRegistry
public ResourceRegistry()Construct a ResourceRegistry class instance. When instances of theseResourceRegistryare garbage collected when not shut down, they will automatically behave as-ifshutdown()was invoked and a warning is emitted to the logs. -
ResourceRegistry
public ResourceRegistry(boolean garbageCollectable) Construct a ResourceRegistry class instance. Instances constructed with this constructor will not emit any log when it is garbage collected.- Parameters:
garbageCollectable- If true, the ResourceRegistry will be shut down when no remaining references to the ResourceRegistry remain referenced.
-
-
Method Details
-
isFactoryInterface
Checks if the supplied class is a permissable factory class.- Parameters:
clazz- Class to be checked- Returns:
- true iff class is not null and is a
ResourceRegistry.Factoryinterface.
-
registerFactory
public static <R extends ShutdownableResource,F extends ResourceRegistry.Factory<R>> F registerFactory(Class<F> clazz, F factory) Register the factory with theResourceRegistrysuch that future invocations offactory(Class)will return an appropiate proxy for the factory.- Type Parameters:
R- Type of the resource which the factory will return.F- Type of the factory.- Parameters:
clazz- Interface being registered with the ResourceRegistry.factory- Factory being registered with the ResourceRegistry.- Returns:
- the factory.
-
factory
@Nonnull public <R extends ShutdownableResource,F extends ResourceRegistry.Factory<R>> F factory(@Nonnull Class<F> clazz) Return a factory which implements the requested factory class F.- Type Parameters:
R- Resource type which the factory will construct.F- Type of the factory.- Parameters:
clazz- Interface demanded.- Returns:
- a factory.
-
register
Register aShutdownableResourceresource with thisResourceRegistry.- Type Parameters:
R- Type of the resource.- Parameters:
resource- Resource to be registered.- Returns:
- the resource.
-
register
Register aShutdownableresource with thisResourceRegistry.- Type Parameters:
R- Type of the resource.- Parameters:
resource- Resource to be registered.- Returns:
- the resource.
-
register
Register aSyncShutdownableresource with thisResourceRegistry.- Type Parameters:
R- Type of the resource.- Parameters:
resource- Resource to be registered.- Returns:
- the resource.
-
remove
Removes the specifiedShutdownableResourcefrom thisResourceRegistry.- Type Parameters:
R- Type of the resource.- Parameters:
resource- Resource to be removed.
-
remove
Removes the specifiedShutdownablefrom thisResourceRegistry.- Type Parameters:
R- Type of the resource.- Parameters:
resource- Resource to be removed.
-
remove
Removes the specifiedSyncShutdownablefrom thisResourceRegistry.- Type Parameters:
R- Type of the resource.- Parameters:
resource- Resource to be removed.
-
vacuum
public void vacuum()Vacuums any already-terminated resources. -
shutdown
public void shutdown()Starts the shutdown process. It is recommended to perform the actual shutdown activity in a separate thread from the thread that calls shutdown -
waitToStartShutdown
Waits until another caller callsshutdown()on this resource registry.- Throws:
InterruptedException- when the wait is interrupted
-
waitToStartShutdown
Waits until another caller callsshutdown()on this resource registry.- Parameters:
timeoutInMs- Time to wait, in milliseconds.- Throws:
InterruptedException- when the wait is interruptedTimeoutException- when the operation times out
-
isShutdown
public final boolean isShutdown()Returns true if this resource has been shut down.- Returns:
- true if this resource has been shut down
-
isTerminated
public final boolean isTerminated()Returns true if the resource has completed shutting down. Note that isTerminated is never true unless shutdown was called first.- Returns:
- true if the resource has completed shutting down.
-
waitForShutdown
Waits for shutdown to complete- Throws:
InterruptedException- when the wait is interruptedIllegalStateException- when the method is invoked before shutdown has started
-
waitForShutdown
public void waitForShutdown(long timeoutInMs) throws InterruptedException, IllegalStateException, TimeoutException Waits for shutdown to complete with a timeout- Parameters:
timeoutInMs- number of milliseconds to wait before throwing TimeoutException- Throws:
InterruptedException- when the wait is interruptedIllegalStateException- when the method is invoked before shutdown has been startedTimeoutException- when the wait times out
-
iterator
-
toString
-
globalShutdown
Starts shutdown for all ResourceRegistry instances.- Throws:
InterruptedException- if the current thread is interrupted while waiting to acquire a lock
-
getGlobalShutdownDelayMillis
public static long getGlobalShutdownDelayMillis()Get the delay for global shutdown. globalShutdown will wait this many millis before shutting down. -
setGlobalShutdownDelayMillis
public static void setGlobalShutdownDelayMillis(long globalShutdownDelayMillis) Set a delay for global shutdown. globalShutdown will wait this many millis before shutting down.
-