Java Dynamic Management Kit 3.2 Programming Guide | ||||
---|---|---|---|---|
![]() | ![]() | Chapter 10. Base Services - Customizing the Framework | ![]() | ![]() |
The thread allocator service centralizes all thread creation for an agent's m-beans. All m-beans which require a new thread should use this service instead of calling one of the java.lang.Thread class constructors directly. All Java Dynamic Management components which require new threads, such as adaptors, also call this service to obtain the threads they need during execution.
The thread allocator service implements the com.sun.jaw.reference.agent.services.ThreadAllocatorSrvIf Java interface. This interface defines methods which are similar to the Thread class constructors, with the addition of an extra parameter which identifies the object requesting the thread. The convention which is used in the Java Dynamic Management Kit is that this parameter contains the registered object name of the object requesting the thread.
The default implementation provided with the Java Dynamic Management Kit is a simple thread allocator which keeps a list of the names of all active threads which it has allocated. In all other aspects, this implementation handles thread names and thread groups in exactly the same manner as when instantiated with Thread class constructors.
Usually, the default implementation of the thread allocator is started when the framework object is instantiated. However, there are two ways to specify that an alternate thread allocator be used in an agent, should you wish to do so. First, you must instantiate the alternate thread allocator object using a class which implements the com.sun.jaw.reference.agent.services.ThreadAllocatorSrvIf interface. Then follow either of these two steps:
When instantiating the framework, call the constructor which allows you to specify the alternate thread allocator object as one of its arguments (see Initializing the Framework)
Use one of the other constructors to instantiate the framework and then call its setThreadAllocatorSrv method with the alternate thread allocator object as its sole argument
M-beans which require a thread during their execution must call the thread allocator service. To obtain a new thread, the m-bean needs to perform the following steps:
Instantiate an object which implements the java.lang.Runnable and which contains the code to be executed in a separate thread
Call the getThreadAllocatorSrv method of the framework to obtain a reference to the thread allocator service
Call one of the obtainThread methods of the thread allocator service and pass the runnable object as one of its parameters; this will return the reference to a new thread object
Begin execution in the new thread by calling its start method
Example 10-4 shows how to allocate a new thread in an m-bean.
Example 10-4. Using the thread allocator service
// a runnable class: class myRunnable implements java.lang.Runnable { public void run() { // code to be executed in a separate thread ... } } // inside the m-bean: // cmf is a reference to the framework object // myName is the ObjectName used to register this m-bean myRunnable runMe = new myRunnable(); java.lang.Thread newThread; com.sun.jaw.reference.agent.services.ThreadAllocatorSrvIf allocator; allocator = cmf.getThreadAllocatorSrv(); newThread = allocator.obtainThread( myName, runMe ) if (newThread != null) newThread.start(); |
Normally, the thread allocator service is not a managed object, it is only accessible through the getThreadAllocatorSrv method of the framework. However, if you wish to access the properties of the thread allocator service as if it were an m-bean, you may register it in the framework as you would any other m-bean. By default, the thread allocator service is not registered with the framework, you must register it explicitly if you wish to access its properties.
For example, the default implementation of the thread allocator follows the m-bean design pattern and exposes its list of active threads. When this thread allocator is registered with the framework, you may then access these properties through the HTML adaptor, which can be useful for debugging.
Finally, the Java Dynamic Management Kit also provides the MO interface and its stub implementation for the default thread allocator. Using these in a client application, you may access the thread allocator's list of active threads remotely, through any of the adaptors.
![]() | ![]() | ![]() |
Relationship Service | ![]() | Security |