public interface ComponentExecutorFactory
ComponentExecutorFactory
service can be registered by any management agent bundle
in order to enable parallel activation of Components.
A ComponentExecutorFactory
is part of the new concurrency model that forms the basis
of Dependency Manager 4.0. Let's first give a brief overview of the default thread model used when
no ComponentExecutorFactory is used. Then we'll explain the rationale and the usage of a
ComponentExecutorFactory
service.
Serial Queue
which is internally maintained in each Component.
Serial Queue
are then executed in FIFO order, by the first
thread which has triggered the first event. This avoid to use some blocking locks in DM internals, and
also it simplifies the development of DM components, because all lifecycle callbacks
(init/start/stop/destroy) and dependency injections are scheduled through the Serial Queue
:
This means that your component is not concurrently called in lifecycle callbacks and in dependency injection
methods.
Serial Queue
:
When a job (J1) is scheduled in the queue while it is empty, then the current thread becomes the "master"
and will immediately execute the Serial Queue
tasks (synchronously). And if another thread
triggers another event concurrently while the "master" thread is executing the job J1, then a job (J2)
for this new event is just enqueued in the Serial Queue
, but the other thread returns
immediately to the caller, and the job J2 will then be executed by the "master" thread (after J1).
This mechanism allows to serially handle all Component events (service dependencies) in FIFO order without maintaining any locks.
ComponentExecutorFactory
Serial Queue
of the Component, and the jobs are getting executed serially
by a single "master" thread. So usually, bundles are started from a single thread, meaning that all Components
are then activated synchronously.
But when you register in the OSGi service registry a ComponentExecutorFactory
, that factory
will be used by DependencyManager to create an Executor of your choice for each Component, typically a shared
threadpool configured by yourself. And all the Component Serial Queues
will be executed using
the Executor returned by the getExecutorFor(Component)
method.
However, jobs scheduled in the Serial Queue
of a given Component are still executed one at a
time, in FIFO order and the Component remains single threaded, and independent Components
may then each be managed and activated concurrently with respect to each other.
If you want to ensure that all Components are initialized after the ComponentExecutorFactory is registered in the OSGI registry, you can use the "org.apache.felix.dependencymanager.parallel" OSGi system property which specifies the list of components which must wait for the ComponentExecutorFactory service. This property value can be set to a wildcard ("*"), or a list of components implementation class prefixes (comma separated). So, all components whose class name starts with the specified prefixes will be cached until the ComponentExecutorFactory service is registered (In this way, it is not necessary to use the StartLevel service if you want to ensure that all components are started concurrently).
Some class name prefixes can also be negated (using "!"), in order to exclude some components from the list of components using the ComponentExecutorFactory service.
Notice that if the ComponentExecutorFactory itself and all its dependent services are defined using the Dependency Manager API, then you have to list the package of such components with a "!" prefix, in order to indicate that those components must not wait for a ComponentExecutorFactory service (since they are part of the ComponentExecutorFactory implementation !).
org.apache.felix.dependencymanager.parallel=* means all components must be cached until a ComponentExecutorFactory comes up. org.apache.felix.dependencymanager.parallel=foo.bar, foo.zoo means only components whose implementation class names are starting with "foo.bar" or "foo.zoo" must be handled using an Executor returned by the ComponentExecutorFactory service. Other Components will be handled normally, as when there is no ComponentExecutorFactory available. org.apache.felix.dependencymanager.parallel=!foo.threadpool, * means all components must be delayed until the ComponentExecutorFactory comes up, except the components whose implementations class names are starting with "foo.threadpool" prefix).
Next, here is the Activator which declares the ComponentExecutorFactory:org.apache.felix.dependencymanager.parallel=!com.acme.management.threadpool, *
And here is the implementation for our ComponentExecutorFactory:package com.acme.management.threadpool; import org.apache.felix.dm.*; public class Activator extends DependencyActivatorBase { public void init(BundleContext context, DependencyManager mgr) throws Exception { mgr.add(createComponent() .setInterface(ComponentExecutorFactory.class.getName(), null) .setImplementation(ComponentExecutorFactoryImpl.class) .add(createConfigurationDependency() .setPid("com.acme.management.threadpool.ComponentExecutorFactoryImpl"))); } }
package com.acme.management.threadpool; import org.apache.felix.dm.*; public class ComponentExecutorFactoryImpl implements ComponentExecutorFactory { volatile Executor m_threadPool; void updated(Dictionary conf) { m_sharedThreadPool = Executors.newFixedThreadPool(Integer.parseInt("threadpool.size")); } @Override public Executor getExecutorFor(Component component) { return m_sharedThreadPool; // Use a shared threadpool for all Components } }
Modifier and Type | Method and Description |
---|---|
java.util.concurrent.Executor |
getExecutorFor(Component component)
Returns an Executor (typically a shared thread pool) used to manage a given DependencyManager Component.
|
java.util.concurrent.Executor getExecutorFor(Component component)
component
- the Component to be managed by the returned Executor