-
- All Superinterfaces:
AutoCloseable
public interface ResourceContext extends AutoCloseable
A set of ResourceMeters that accumulate resource usage by threads. A ResourceContext can be associated with zero or more threads. TheResourceContextFactory.getThreadContext()andbindThreadContext()methods respectively get and set the association between the current thread and a ResourceContext.New threads are implicitly bound to the ResourceContext of the thread invoking
new Thread. The Thread'sRunnableshould explicitlybindto the desired context as required.Each meter is identified by a ResourceType. Resource meters are created with the desired behavior and
addedto the context. The semantics of a particular meter are specified by eachResourceMetersubtype. Resource implementations locate the appropriate meter usinggetMeter(ResourceType). TheResourceRequestis obtained bygetResourceRequest(ResourceType).- Since:
- 8u40
- See Also:
ResourceContextFactory,ResourceMeter
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default voidaddResourceMeter(ResourceMeter meter)Add a ResourceMeter.default ResourceContextbindThreadContext()Binds the ResourceContext to the current thread.default Stream<Thread>boundThreads()Returns a Stream of threads bound to the ResourceContext.voidclose()Close the ResourceContext, unbinding from all threads and removing all meters.ResourceMetergetMeter(ResourceType type)Return the ResourceMeter for the ResourceType.StringgetName()Return the name of the ResourceContext.ResourceRequestgetResourceRequest(ResourceType type)Returns the ResourceRequest in the ResourceContext that handles requests to allocate or release for the ResourceType.Stream<ResourceMeter>meters()Returns a Stream of ResourceMeters in the ResourceContext.default booleanremoveResourceMeter(ResourceMeter meter)Remove the ResourceMeter from the ResourceContext.default voidrequestAccurateUpdate(ResourceAccuracy accuracy)Request an update of the metrics for the ResourceContext.static ResourceContextunbindThreadContext()Unbind the current thread from the ResourceContext.
-
-
-
Method Detail
-
close
void close()
Close the ResourceContext, unbinding from all threads and removing all meters. The value and allocated values of meters is retained and included in theTotalsResourceContext. Afterclosethe ResourceContext behaves as if it has no ResourceMeters, ResourceMeters cannot be added, and it cannot be bound to a thread. Closing a closed ResourceContext has no effect.- Specified by:
closein interfaceAutoCloseable
-
getName
String getName()
Return the name of the ResourceContext.- Returns:
- the name of the ResourceContext
-
bindThreadContext
default ResourceContext bindThreadContext()
Binds the ResourceContext to the current thread.- Implementation Note:
- the default behavior is to throw UnsupportedOperationException
- Returns:
- the ResourceContext the thread was bound to;
the
unassignedcontext is returned if the thread is otherwise not bound - Throws:
UnsupportedOperationException- if the ResourceContext does not support binding
-
unbindThreadContext
static ResourceContext unbindThreadContext()
Unbind the current thread from the ResourceContext. The thread is implicitly bound to theunassigned context.- Implementation Note:
- the default behavior is to throw UnsupportedOperationException
- Returns:
- the ResourceContext the thread was bound to;
the
unassignedcontext is returned if the thread is otherwise not bound - Throws:
UnsupportedOperationException- if the ResourceContext does not support unbinding
-
boundThreads
default Stream<Thread> boundThreads()
Returns a Stream of threads bound to the ResourceContext. The Threads bound to a ResourceContext is fluid and may change at any time. If thread pooling is in use a Thread may be bound to different ResourceContexts at different times and may change dynamically.- Implementation Note:
- the default behavior is to throw UnsupportedOperationException
- Returns:
- a Stream of the threads bound to the ResourceContext
- Throws:
UnsupportedOperationException- if the ResourceContext does not maintain a set of Threads
-
getResourceRequest
ResourceRequest getResourceRequest(ResourceType type)
Returns the ResourceRequest in the ResourceContext that handles requests to allocate or release for the ResourceType.- Parameters:
type- the ResourceType of the resource making the request- Returns:
- a ResourceRequest;
nullif the ResourceContext is not counting metrics for the resource or the meter does not implementResourceRequest
-
addResourceMeter
default void addResourceMeter(ResourceMeter meter)
Add a ResourceMeter. ResourceMeters must have unique ResourceTypes within a ResourceContext.- Implementation Note:
- the default behavior is to throw UnsupportedOperationException
- Parameters:
meter- a resource meter- Throws:
IllegalArgumentException- if the ResourceContext contains a ResourceMeter with the same ResourceTypeUnsupportedOperationException- if the ResourceContext does not support adding or removing ResourceMeters or if the ResourceType is not supported
-
removeResourceMeter
default boolean removeResourceMeter(ResourceMeter meter)
Remove the ResourceMeter from the ResourceContext. Note that this does not actually deactivate the meter, but merely excises it from the scope of the current context.- Parameters:
meter- the ResourceMeter to remove- Returns:
- true if an element was removed as a result of this call
- Throws:
UnsupportedOperationException- if the ResourceContext does not support adding or removing Meters
-
getMeter
ResourceMeter getMeter(ResourceType type)
Return the ResourceMeter for the ResourceType.- Parameters:
type- a ResourceType- Returns:
- the ResourceMeter for the
typeornullif none
-
meters
Stream<ResourceMeter> meters()
Returns a Stream of ResourceMeters in the ResourceContext.- Returns:
- stream of the ResourceMeters
-
requestAccurateUpdate
default void requestAccurateUpdate(ResourceAccuracy accuracy)
Request an update of the metrics for the ResourceContext. If the accuracy of the information provided for this ResourceContext is less than the requested accuracy, the implementation starts a computation that will delivery the requested accuracy. Computing more accurate measurements may take some time. For example, it may be necessary to scan the heap to determine more accurately how much of the heap is retained to deliver an updatedHEAP_RETAINEDamount. When the computation is completed, the updated values are reported through the ResourceMeters in the ResourceContext. Only values that change are reported and use the normal ResourceMeter granularity and notification mechanisms. TheresourceId.getAccuracymethod returns the accuracy.- Parameters:
accuracy- the requested accuracy
-
-