Chapter 32 Using the Thread Manager
The Thread Manager is a built-in Jaguar component. You can create a proxy and execute methods the same way that you would call any other component. Each thread executes a run method in a Jaguar component that you specify.
The Thread Manager must be called from a Jaguar component. You may want to create a control component and client application that allows an administrator to start and stop threads.
Before running components in the Thread Manager, make sure you understand how the component must be prepared, how threads are run in thread groups, and the effect of a thread group's run interval.
Each thread runs a Jaguar component instance. To be run by the Thread Manager, the component must have a run method with this IDL signature:
void run ( );
The Thread Manager calls the run method one or more times, depending on how you configure the run interval (described below).
The Thread Manager is itself a Jaguar component, and runs your component using intercomponent calls. All component properties, including transaction attributes, are in effect when your component is run by the Thread Manager. The Thread Manager executes with the system identity, as does your component's run method.
Threads are associated with a thread group. To start, stop, suspend, or manage the run interval of threads, you must specify the group name. These operations affect all threads in the specified group. The group name is simply a string. Group names have a scope limited to one Jaguar server; that is, you cannot have two like-named groups in the same server. If two applications use the same group name, their Thread Manager calls affect threads in both applications. You can run different components in one thread group.
Naming conventions for thread groups To avoid collisions between thread groups used by different applications, use the reverse-domain naming convention for group names, as used in Java package names. For example, "com.foo.mythreadgroup".
Each thread group has a run interval, which determines how often the Thread Manager calls the run method. The run interval can be:
To allow threads to be stopped or suspended, you must configure a positive or 0-length run interval and code each component's run method to perform a repetitive task, then return. The run interval has no effect if your run method never returns.
You can use a run interval to schedule periodic tasks, such as refreshing a cached copy of a database query result. You can also tune how much CPU time your component consumes if it performs CPU-intensive tasks such as lengthy calculations; such tuning also requires that you adjust the amount of work done in each invocation of the run method.
You can also use the Message Service to schedule periodic background processing. For example, you can configure a run interval of -1 (so Thread Manager calls run once only) and schedule another component to start threads at the desired interval. See "Subscribing to scheduled messages" for more information.
Other than restricted access, the Thread Manager can be instantiated as you would instantiate any other component.
To instantiate the Thread Manager, your component must execute with with the system identity. These are the recommended ways to satisfy this constraint:
Use the standard technique for your component model to instantiate the Thread Manager proxy.
CORBA (C++ and Java), ActiveX, and PowerBuilder components must declare a stub for the CtsComponents::ThreadManager IDL interface, then instantiate the component named CtsComponents/ThreadManager.
EJB components must use the home interface com.sybase.ejb.cts.ThreadManagerHome to create a stub for the remote interface com.sybase.ejb.cts.ThreadManager. Look up the name CtsComponents/ThreadManager to obtain the home interface.
To suspend the threads in a group, call the ThreadManager::suspend method, specifying the group name. Each thread is suspended when it next returns from its run method.
To resume execution, call the ThreadManager:resume method.
You can only stop threads that return from their run method. The Thread Manager stops each thread the next time it returns from its run method.
You can stop threads in two ways:
Copyright © 2000 Sybase, Inc. All rights reserved. |