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.
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private final Condition | |
| private Executor | |
| private ImmediateController. | |
| 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 | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | configurationChanged()
Implements org. This method is called when the set of descriptors in this service locator has been changed. |
| private void | |
| public Executor | getExecutor()
Implements org. Returns the executor that is currently in use by the Immediate subsystem |
| private List | |
| public ImmediateController. | getImmediateState()
Implements org. Returns the state the system is currently running under |
| public Filter | getLookupFilter()
Implements org. This filter will be run at least once per descriptor at the point that the descriptor
is being looked up, either with the |
| public long | getThreadInactivityTimeout()
Implements org. Returns the time in milliseconds a thread will wait for new Immediate services before dying |
| public Validator | getValidator()
Implements org. Returns the |
| private boolean | |
| public void | onFailure(ErrorInformation errorInformation)
Implements org. This method is called when a failure occurs in the system. |
| public void | run()
Implements java. This thread will wait twenty seconds for new work to come in, and then kill itself |
| public void | setExecutor(Executor executor)
Implements org. Sets the executor to be used by the Immediate subsystem. |
| public void | setImmediateState(ImmediateController.
Implements org. Sets the state the system is currently running under |
| public void | setThreadInactivityTimeout(long timeInMillis)
Implements org. Sets the time in milliseconds a thread will wait for new Immediate services before dying |
| public boolean | validate(ValidationInformation info)
Implements org. 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. |
| condition | back to summary |
|---|---|
| private final Condition condition | |
| currentExecutor | back to summary |
|---|---|
| private Executor currentExecutor | |
| currentState | back to summary |
|---|---|
| private ImmediateController. | |
| decayTime | back to summary |
|---|---|
| private long decayTime | |
| DEFAULT_EXECUTOR | back to summary |
|---|---|
| private static final Executor DEFAULT_EXECUTOR | |
| firstTime | back to summary |
|---|---|
| private boolean firstTime | |
| immediateContext | back to summary |
|---|---|
| private final ImmediateContext immediateContext | |
| locator | back to summary |
|---|---|
| private final ServiceLocator locator | |
| outstandingJob | back to summary |
|---|---|
| private boolean outstandingJob | |
| queueLock | back to summary |
|---|---|
| private final ReentrantLock queueLock | |
| THREAD_FACTORY | back to summary |
|---|---|
| private static final ThreadFactory THREAD_FACTORY | |
| threadAvailable | back to summary |
|---|---|
| private boolean threadAvailable | |
| tidsWithWork | back to summary |
|---|---|
| private final HashSet<Long> tidsWithWork | |
| waitingForWork | back to summary |
|---|---|
| private boolean waitingForWork | |
| ImmediateHelper | back to summary |
|---|---|
private ImmediateHelper(ServiceLocator serviceLocator, ImmediateContext immediateContext)
| |
| configurationChanged | back to summary |
|---|---|
| public void configurationChanged() Implements org. Doc from org. 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
| |
| doWorkIfWeHaveSome | back to summary |
|---|---|
| private void doWorkIfWeHaveSome() Must have queueLock held | |
| getExecutor | back to summary |
|---|---|
| public Executor getExecutor() Implements org. Doc from org. Returns the executor that is currently in use by the Immediate subsystem
| |
| getImmediateServices | back to summary |
|---|---|
| private List | |
| getImmediateState | back to summary |
|---|---|
| public ImmediateController. Implements org. Doc from org. Returns the state the system is currently running under
| |
| getLookupFilter | back to summary |
|---|---|
| public Filter getLookupFilter() Implements org. Doc from org. This filter will be run at least once per descriptor at the point that the descriptor
is being looked up, either with the 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.
| |
| getThreadInactivityTimeout | back to summary |
|---|---|
| public long getThreadInactivityTimeout() Implements org. Doc from org. Returns the time in milliseconds a thread will wait for new Immediate services before dying
| |
| getValidator | back to summary |
|---|---|
| public Validator getValidator() Implements org. Doc from org. Returns the
| |
| hasWork | back to summary |
|---|---|
| private boolean hasWork() Must have queueLock held
| |
| onFailure | back to summary |
|---|---|
| public void onFailure(ErrorInformation errorInformation) throws MultiException Implements org. Doc from org. This method is called when a failure occurs in the system. This method may
use any
| |
| run | back to summary |
|---|---|
| public void run() Implements java. This thread will wait twenty seconds for new work to come in, and then kill itself
| |
| setExecutor | back to summary |
|---|---|
| public void setExecutor(Executor executor) throws IllegalStateException Implements org. Doc from org. 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
| |
| setImmediateState | back to summary |
|---|---|
| public void setImmediateState(ImmediateController. Implements org. Doc from org. Sets the state the system is currently running under
| |
| setThreadInactivityTimeout | back to summary |
|---|---|
| public void setThreadInactivityTimeout(long timeInMillis) throws IllegalStateException Implements org. Doc from org. Sets the time in milliseconds a thread will wait for new Immediate services before dying
| |
| validate | back to summary |
|---|---|
| public boolean validate(ValidationInformation info) Implements org. Doc from org. 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
| |
| Access | Constructor and Description |
|---|---|
| private |
| ImmediateThread | back to summary |
|---|---|
| private ImmediateThread(Runnable r) | |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public Thread | newThread(Runnable runnable)
Implements java. Constructs a new unstarted |
| ImmediateThreadFactory | back to summary |
|---|---|
| private ImmediateThreadFactory() | |
| newThread | back to summary |
|---|---|
| public Thread newThread(Runnable runnable) Implements java. Doc from java. Constructs a new unstarted
| |