Top Description Inners Fields Constructors Methods
org.glassfish.hk2.internal

public Class ImmediateHelper

extends Object
implements DynamicConfigurationListener, Runnable, ValidationService, ErrorService, Validator, ImmediateController
Class Inheritance
All Implemented Interfaces
org.glassfish.hk2.api.ImmediateController, org.glassfish.hk2.api.Validator, org.glassfish.hk2.api.ErrorService, org.glassfish.hk2.api.ValidationService, java.lang.Runnable, org.glassfish.hk2.api.DynamicConfigurationListener
Annotations
@Singleton
@Visibility:LOCAL
Imports
java.util.Collections, .HashSet, .List, java.util.concurrent.Executor, .SynchronousQueue, .ThreadFactory, .ThreadPoolExecutor, .TimeUnit, java.util.concurrent.locks.Condition, .ReentrantLock, jakarta.inject.Inject, .Singleton, org.glassfish.hk2.api.ActiveDescriptor, .DescriptorVisibility, .DynamicConfigurationListener, .ErrorInformation, .ErrorService, .ErrorType, .Filter, .ImmediateController, .MultiException, .Operation, .ServiceLocator, .ValidationInformation, .ValidationService, .Validator, .Visibility, org.glassfish.hk2.utilities.ImmediateContext

The implementation of the immediate context. This should NOT be added automatically, and hence is not annotated with org.jvnet.hk2.annotations.Service

This implementation uses a lot of facilities of hk2, so lets explain each one.

The thing that makes Immediate services immediate is that they are started as soon as they are noticed. This is done by implementing the DynamicConfigurationListener, as it will get notified when a configuration has changed. However, since the creation of user services can take an arbitrarily long time it is better to do that work on a separate thread, which is why we implement Runnable. The run method is the method that will pull from the queue of work and instantiate (and destroy) the immediate services.

However, there is also a desire to be highly efficient. This means we should not be creating this thread if there is no work for the thread to do. For this to work we need to know which configuration changes have added or removed an Immediate service. To know this we have implemented the Validation service and Validator. The validation service records the thread id of a configuration operation that is adding or removing services. This thread id goes into a map. But we have to be sure that the map does not grow without bound. To do this we clear the map both when the configuration service has succeeded (in the DynamicConfigurationListener) and when the configuration service has failed (in the ErrorService). This is why we have implemented the ErrorService.

Author
jwells

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class

Field Summary

Modifier and TypeField and Description
private final Condition
private Executor
private ImmediateController.ImmediateServiceState
private long
private static final Executor
private boolean
private final ImmediateContext
private final ServiceLocator
private boolean
private final ReentrantLock
private static final ThreadFactory
private boolean
private final HashSet<Long>
private boolean

Constructor Summary

AccessConstructor and Description
private
ImmediateHelper(ServiceLocator serviceLocator, ImmediateContext immediateContext)

Method Summary

Modifier and TypeMethod and Description
public void
configurationChanged()

Implements org.glassfish.hk2.api.DynamicConfigurationListener.configurationChanged.

This method is called when the set of descriptors in this service locator has been changed.

private void
doWorkIfWeHaveSome()

Must have queueLock held

public Executor
getExecutor()

Implements org.glassfish.hk2.api.ImmediateController.getExecutor.

Returns the executor that is currently in use by the Immediate subsystem

private List<ActiveDescriptor<?>>
public ImmediateController.ImmediateServiceState
getImmediateState()

Implements org.glassfish.hk2.api.ImmediateController.getImmediateState.

Returns the state the system is currently running under

public Filter
getLookupFilter()

Implements org.glassfish.hk2.api.ValidationService.getLookupFilter.

This filter will be run at least once per descriptor at the point that the descriptor is being looked up, either with the ServiceLocator API or due to an @Inject resolution.

public long
getThreadInactivityTimeout()

Implements org.glassfish.hk2.api.ImmediateController.getThreadInactivityTimeout.

Returns the time in milliseconds a thread will wait for new Immediate services before dying

public Validator
getValidator()

Implements org.glassfish.hk2.api.ValidationService.getValidator.

Returns the Validator that will be run whenever a Descriptor that passed the filter is to be looked up with the API or injected into an injection point, or on any bind or unbind operation.

private boolean

Returns:

true if there is work to be done
hasWork
()

Must have queueLock held

public void
onFailure(ErrorInformation errorInformation)

Implements org.glassfish.hk2.api.ErrorService.onFailure.

This method is called when a failure occurs in the system.

public void
run()

Implements java.lang.Runnable.run.

This thread will wait twenty seconds for new work to come in, and then kill itself

public void
setExecutor(Executor executor)

Implements org.glassfish.hk2.api.ImmediateController.setExecutor.

Sets the executor to be used by the Immediate subsystem.

public void
public void
setThreadInactivityTimeout(long timeInMillis)

Implements org.glassfish.hk2.api.ImmediateController.setThreadInactivityTimeout.

Sets the time in milliseconds a thread will wait for new Immediate services before dying

public boolean
validate(ValidationInformation info)

Implements org.glassfish.hk2.api.Validator.validate.

This method is called whenever it has been determined that a validating class is to be injected into an injection point, or when a descriptor is being looked up explicitly with the API, or a descriptor is being bound or unbound into the registry.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

conditionback to summary
private final Condition condition
currentExecutorback to summary
private Executor currentExecutor
currentStateback to summary
private ImmediateController.ImmediateServiceState currentState
decayTimeback to summary
private long decayTime
DEFAULT_EXECUTORback to summary
private static final Executor DEFAULT_EXECUTOR
firstTimeback to summary
private boolean firstTime
immediateContextback to summary
private final ImmediateContext immediateContext
locatorback to summary
private final ServiceLocator locator
outstandingJobback to summary
private boolean outstandingJob
queueLockback to summary
private final ReentrantLock queueLock
THREAD_FACTORYback to summary
private static final ThreadFactory THREAD_FACTORY
threadAvailableback to summary
private boolean threadAvailable
tidsWithWorkback to summary
private final HashSet<Long> tidsWithWork
waitingForWorkback to summary
private boolean waitingForWork

Constructor Detail

ImmediateHelperback to summary
private ImmediateHelper(ServiceLocator serviceLocator, ImmediateContext immediateContext)
Annotations
@Inject

Method Detail

configurationChangedback to summary
public void configurationChanged()

Implements org.glassfish.hk2.api.DynamicConfigurationListener.configurationChanged.

Doc from org.glassfish.hk2.api.DynamicConfigurationListener.configurationChanged.

This method is called when the set of descriptors in this service locator has been changed. Changes to parent service locators descriptors will not be reported. These services are called back on the thread doing the DynamicConfiguration#commit() so care should be taken to do any work quickly. Any exception thrown from this method will be ignored. A commit that failed will not be reported to this method

Annotations
@Override
doWorkIfWeHaveSomeback to summary
private void doWorkIfWeHaveSome()

Must have queueLock held

getExecutorback to summary
public Executor getExecutor()

Implements org.glassfish.hk2.api.ImmediateController.getExecutor.

Doc from org.glassfish.hk2.api.ImmediateController.getExecutor.

Returns the executor that is currently in use by the Immediate subsystem

Annotations
@Override
getImmediateServicesback to summary
private List<ActiveDescriptor<?>> getImmediateServices()
getImmediateStateback to summary
public ImmediateController.ImmediateServiceState getImmediateState()

Implements org.glassfish.hk2.api.ImmediateController.getImmediateState.

Doc from org.glassfish.hk2.api.ImmediateController.getImmediateState.

Returns the state the system is currently running under

Annotations
@Override
getLookupFilterback to summary
public Filter getLookupFilter()

Implements org.glassfish.hk2.api.ValidationService.getLookupFilter.

Doc from org.glassfish.hk2.api.ValidationService.getLookupFilter.

This filter will be run at least once per descriptor at the point that the descriptor is being looked up, either with the ServiceLocator API or due to an @Inject resolution. The decision made by this filter will be cached and used every time that Descriptor is subsequently looked up. No validation checks should be done in the returned filter, it is purely meant to limit the Descriptors that are passed into the validator.

Descriptors passed to this filter may or may not be reified. The filter should try as much as possible to do its work without reifying the descriptor.

The filter may be run more than once on a descriptor if some condition caused the cache of results per descriptor to become invalidated.

Annotations
@Override
getThreadInactivityTimeoutback to summary
public long getThreadInactivityTimeout()

Implements org.glassfish.hk2.api.ImmediateController.getThreadInactivityTimeout.

Doc from org.glassfish.hk2.api.ImmediateController.getThreadInactivityTimeout.

Returns the time in milliseconds a thread will wait for new Immediate services before dying

Annotations
@Override
getValidatorback to summary
public Validator getValidator()

Implements org.glassfish.hk2.api.ValidationService.getValidator.

Doc from org.glassfish.hk2.api.ValidationService.getValidator.

Returns the Validator that will be run whenever a Descriptor that passed the filter is to be looked up with the API or injected into an injection point, or on any bind or unbind operation. If this method returns false then the operation will not proceed.

Annotations
@Override
hasWorkback to summary
private boolean hasWork()

Must have queueLock held

Returns:boolean

true if there is work to be done

onFailureback to summary
public void onFailure(ErrorInformation errorInformation) throws MultiException

Implements org.glassfish.hk2.api.ErrorService.onFailure.

Doc from org.glassfish.hk2.api.ErrorService.onFailure.

This method is called when a failure occurs in the system. This method may use any ServiceLocator api. For example, an implementation of this method might want to remove a descriptor from the registry if the error can be determined to be a permanent failure.

Annotations
@Override
Exceptions
MultiException:
if this method throws an exception that exception will be thrown back to the caller wrapped in another MultiException if the error is of type ErrorType#FAILURE_TO_REIFY. If the error is of type ErrorType#DYNAMIC_CONFIGURATION_FAILURE or ErrorType#SERVICE_CREATION_FAILURE or ErrorType#SERVICE_DESTRUCTION_FAILURE then any exception thrown from this method is ignored and the original exception is thrown back to the caller
runback to summary
public void run()

Implements java.lang.Runnable.run.

This thread will wait twenty seconds for new work to come in, and then kill itself

Annotations
@Override
setExecutorback to summary
public void setExecutor(Executor executor) throws IllegalStateException

Implements org.glassfish.hk2.api.ImmediateController.setExecutor.

Doc from org.glassfish.hk2.api.ImmediateController.setExecutor.

Sets the executor to be used by the Immediate subsystem. If set to null a default executor will be used. This may only be called when the Immediate service is suspended

Annotations
@Override
Exceptions
IllegalStateException:
if this is called when the Immediate service is not in suspended state
setImmediateStateback to summary
public void setImmediateState(ImmediateController.ImmediateServiceState state)

Implements org.glassfish.hk2.api.ImmediateController.setImmediateState.

Doc from org.glassfish.hk2.api.ImmediateController.setImmediateState.

Sets the state the system is currently running under

Annotations
@Override
setThreadInactivityTimeoutback to summary
public void setThreadInactivityTimeout(long timeInMillis) throws IllegalStateException

Implements org.glassfish.hk2.api.ImmediateController.setThreadInactivityTimeout.

Doc from org.glassfish.hk2.api.ImmediateController.setThreadInactivityTimeout.

Sets the time in milliseconds a thread will wait for new Immediate services before dying

Annotations
@Override
validateback to summary
public boolean validate(ValidationInformation info)

Implements org.glassfish.hk2.api.Validator.validate.

Doc from org.glassfish.hk2.api.Validator.validate.

This method is called whenever it has been determined that a validating class is to be injected into an injection point, or when a descriptor is being looked up explicitly with the API, or a descriptor is being bound or unbound into the registry.

The candidate descriptor being passed in may not have yet been reified. If possible, this method should do its work without reifying the descriptor. However, if it is necessary to reify the descriptor, it should be done with the ServiceLocator.reifyDescriptor method.

The operation will determine what operation is being performed. In the BIND or UNBIND cases the Injectee will be null. In the LOOKUP case the Injectee will be non-null if this is being done as part of an injection point. In the LOOKUP case the Injectee will be null if this is being looked up directly from the ServiceLocator API, in which case the caller of the lookup method will be on the call frame.

Annotations
@Override
org.glassfish.hk2.internal back to summary

private Class ImmediateHelper.ImmediateThread

extends Thread
Class Inheritance

Field Summary

Inherited from java.lang.Thread:
MAX_PRIORITYMIN_PRIORITYNORM_PRIORITY

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Thread:
activeCountcheckAccessclonecurrentThreaddumpStackenumerategetAllStackTracesgetContextClassLoadergetDefaultUncaughtExceptionHandlergetIdgetNamegetPrioritygetStackTracegetStategetThreadGroupgetUncaughtExceptionHandlerholdsLockinterruptinterruptedisAliveisDaemonisInterruptedisVirtualjoinjoinjoinjoinofPlatformofVirtualonSpinWaitrunsetContextClassLoadersetDaemonsetDefaultUncaughtExceptionHandlersetNamesetPrioritysetUncaughtExceptionHandlersleepsleepsleepstartstartVirtualThreadthreadIdtoStringyield

Constructor Detail

ImmediateThreadback to summary
private ImmediateThread(Runnable r)
org.glassfish.hk2.internal back to summary

private Class ImmediateHelper.ImmediateThreadFactory

extends Object
implements ThreadFactory
Class Inheritance
  • java.lang.Object
  • org.glassfish.hk2.internal.ImmediateHelper.ImmediateThreadFactory
All Implemented Interfaces
java.util.concurrent.ThreadFactory

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Thread
newThread(Runnable runnable)

Implements java.util.concurrent.ThreadFactory.newThread.

Constructs a new unstarted Thread to run the given runnable.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ImmediateThreadFactoryback to summary
private ImmediateThreadFactory()

Method Detail

newThreadback to summary
public Thread newThread(Runnable runnable)

Implements java.util.concurrent.ThreadFactory.newThread.

Doc from java.util.concurrent.ThreadFactory.newThread.

Constructs a new unstarted Thread to run the given runnable.

Annotations
@Override