Class ResourceRegistry
- java.lang.Object
-
- com.linkedin.alpini.base.registry.ResourceRegistry
-
- Direct Known Subclasses:
SyncResourceRegistry
public class ResourceRegistry extends java.lang.ObjectA registry to manageShutdownableresources.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceResourceRegistry.Factory<R extends ShutdownableResource>Factories must implement this interface.static interfaceResourceRegistry.ShutdownFirstResources which implementResourceRegistry.ShutdownFirstwill be shut down during the first phase.static interfaceResourceRegistry.ShutdownLastResources which implementResourceRegistry.ShutdownLastwill be shut down during the last phase.static interfaceResourceRegistry.SyncResources 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
Fields Modifier and Type Field Description protected com.linkedin.alpini.base.registry.ResourceRegistry.State_statestatic java.lang.StringSHUTDOWN_THREAD_PREFIX
-
Constructor Summary
Constructors Constructor Description ResourceRegistry()Construct a ResourceRegistry class instance.ResourceRegistry(boolean garbageCollectable)Construct a ResourceRegistry class instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <R extends ShutdownableResource,F extends ResourceRegistry.Factory<R>>
Ffactory(java.lang.Class<F> clazz)Return a factory which implements the requested factory class F.static longgetGlobalShutdownDelayMillis()Get the delay for global shutdown.static voidglobalShutdown()Starts shutdown for all ResourceRegistry instances.static booleanisFactoryInterface(java.lang.Class<?> clazz)Checks if the supplied class is a permissable factory class.booleanisShutdown()Returns true if this resource has been shut down.booleanisTerminated()Returns true if the resource has completed shutting down.protected java.util.Iterator<ShutdownableResource>iterator()<R extends ShutdownableResource>
Rregister(R resource)Register aShutdownableResourceresource with thisResourceRegistry.static <R extends ShutdownableResource,F extends ResourceRegistry.Factory<R>>
FregisterFactory(java.lang.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.java.lang.StringtoString()voidvacuum()Vacuums any already-terminated resources.voidwaitForShutdown()Waits for shutdown to completevoidwaitForShutdown(long timeoutInMs)Waits for shutdown to complete with a timeoutvoidwaitToStartShutdown()Waits until another caller callsshutdown()on this resource registry.voidwaitToStartShutdown(long timeoutInMs)Waits until another caller callsshutdown()on this resource registry.
-
-
-
Constructor Detail
-
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 Detail
-
isFactoryInterface
public static boolean isFactoryInterface(java.lang.Class<?> clazz)
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(java.lang.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 java.lang.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
public <R extends ShutdownableResource> R register(R resource)
Register aShutdownableResourceresource with thisResourceRegistry.- Type Parameters:
R- Type of the resource.- Parameters:
resource- Resource to be registered.- Returns:
- the resource.
-
register
public <R extends Shutdownable> R register(R resource)
Register aShutdownableresource with thisResourceRegistry.- Type Parameters:
R- Type of the resource.- Parameters:
resource- Resource to be registered.- Returns:
- the resource.
-
register
public <R extends SyncShutdownable> R register(R resource)
Register aSyncShutdownableresource with thisResourceRegistry.- Type Parameters:
R- Type of the resource.- Parameters:
resource- Resource to be registered.- Returns:
- the resource.
-
remove
public <R extends ShutdownableResource> void remove(R resource)
Removes the specifiedShutdownableResourcefrom thisResourceRegistry.- Type Parameters:
R- Type of the resource.- Parameters:
resource- Resource to be removed.
-
remove
public <R extends Shutdownable> void remove(R resource)
Removes the specifiedShutdownablefrom thisResourceRegistry.- Type Parameters:
R- Type of the resource.- Parameters:
resource- Resource to be removed.
-
remove
public <R extends SyncShutdownable> void remove(R resource)
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
public void waitToStartShutdown() throws java.lang.InterruptedExceptionWaits until another caller callsshutdown()on this resource registry.- Throws:
java.lang.InterruptedException- when the wait is interrupted
-
waitToStartShutdown
public void waitToStartShutdown(long timeoutInMs) throws java.lang.InterruptedException, java.util.concurrent.TimeoutExceptionWaits until another caller callsshutdown()on this resource registry.- Parameters:
timeoutInMs- Time to wait, in milliseconds.- Throws:
java.lang.InterruptedException- when the wait is interruptedjava.util.concurrent.TimeoutException- 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
public void waitForShutdown() throws java.lang.InterruptedException, java.lang.IllegalStateExceptionWaits for shutdown to complete- Throws:
java.lang.InterruptedException- when the wait is interruptedjava.lang.IllegalStateException- when the method is invoked before shutdown has started
-
waitForShutdown
public void waitForShutdown(long timeoutInMs) throws java.lang.InterruptedException, java.lang.IllegalStateException, java.util.concurrent.TimeoutExceptionWaits for shutdown to complete with a timeout- Parameters:
timeoutInMs- number of milliseconds to wait before throwing TimeoutException- Throws:
java.lang.InterruptedException- when the wait is interruptedjava.lang.IllegalStateException- when the method is invoked before shutdown has been startedjava.util.concurrent.TimeoutException- when the wait times out
-
iterator
protected java.util.Iterator<ShutdownableResource> iterator()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- See Also:
Object.toString()
-
globalShutdown
public static void globalShutdown() throws java.lang.InterruptedExceptionStarts shutdown for all ResourceRegistry instances.- Throws:
java.lang.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.
-
-