Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.dm; |
| 20 | |
| 21 | import java.util.concurrent.Executor; |
| 22 | |
| 23 | /** |
| 24 | * A <code>ComponentExecutorFactory</code> service can be registered by any management agent bundle |
| 25 | * in order to enable parallel activation of Components.<p> |
| 26 | * |
| 27 | * A <code>ComponentExecutorFactory</code> is part of the new concurrency model that forms the basis |
| 28 | * of Dependency Manager 4.0. Let's first give a brief overview of the default thread model used when |
| 29 | * no ComponentExecutorFactory is used. Then we'll explain the rationale and the usage of a |
| 30 | * <code>ComponentExecutorFactory</code> service. |
| 31 | * <p> |
| 32 | * |
| 33 | * <h3>Default Thread Model</h3> |
| 34 | * |
| 35 | * By default, Dependency Manager uses a <b>lock-free/single thread</b> model: |
| 36 | * <p><ul> |
| 37 | * |
| 38 | * <li> When an external event that influence the state of a Component is taking place (for example, |
| 39 | * when a service dependency on which the Component is depending on is registered in the registry by |
| 40 | * a given thread), then DependencyManager does not perform any locking for the handling of the event. |
| 41 | * Instead of that, a job that will handle the event is inserted in an internal lock-free |
| 42 | * <b><code>Serial Queue</code></b> which is internally maintained in each Component. |
| 43 | * |
| 44 | * <li> all jobs scheduled in the <code>Serial Queue</code> are then executed in FIFO order, by the first |
| 45 | * thread which has triggered the first event. This avoid to use some blocking locks in DM internals, and |
| 46 | * also it simplifies the development of DM components, because all lifecycle callbacks |
| 47 | * (init/start/stop/destroy) and dependency injections are scheduled through the <code>Serial Queue</code>: |
| 48 | * This means that your component is not concurrently called in lifecycle callbacks and in dependency injection |
| 49 | * methods. |
| 50 | * |
| 51 | * <li> Now let's describe which thread is executing the jobs scheduled in a Component <code>Serial Queue</code>: |
| 52 | * When a job (J1) is scheduled in the queue while it is empty, then the current thread becomes the "master" |
| 53 | * and will immediately execute the </code>Serial Queue</code> tasks (synchronously). And if another thread |
| 54 | * triggers another event concurrently while the "master" thread is executing the job J1, then a job (J2) |
| 55 | * for this new event is just enqueued in the <code>Serial Queue</code>, but the other thread returns |
| 56 | * immediately to the caller, and the job J2 will then be executed by the "master" thread (after J1). |
| 57 | * </ul> |
| 58 | * |
| 59 | * <p> |
| 60 | * This mechanism allows to serially handle all Component events (service dependencies) in FIFO order |
| 61 | * without maintaining any locks. |
| 62 | * |
| 63 | * <h3>Enabling parallelism with a <code>ComponentExecutorFactory</code></h3> |
| 64 | * |
| 65 | * As described above, all the external events that influence the state of a given component are handed by |
| 66 | * jobs scheduled in the <code>Serial Queue</code> of the Component, and the jobs are getting executed serially |
| 67 | * by a single "master" thread. So usually, bundles are started from a single thread, meaning that all Components |
| 68 | * are then activated synchronously. |
| 69 | * <p> |
| 70 | * |
| 71 | * But when you register in the OSGi service registry a <code>ComponentExecutorFactory</code>, that factory |
| 72 | * will be used by DependencyManager to create an Executor of your choice for each Component, typically a shared |
| 73 | * threadpool configured by yourself. And all the Component <code>Serial Queues</code> will be executed using |
| 74 | * the Executor returned by the {@link #getExecutorFor(Component)} method. |
| 75 | * However, jobs scheduled in the <code>Serial Queue</code> of a given Component are still executed one at a |
| 76 | * time, in FIFO order and the Component remains single threaded, and <b>independent Components |
| 77 | * may then each be managed and activated concurrently with respect to each other</b>. |
| 78 | * <p> |
| 79 | * If you want to ensure that all Components are initialized <b>after</b> the ComponentExecutorFactory is |
| 80 | * registered in the OSGI registry, you can use the "org.apache.felix.dependencymanager.parallel" OSGi |
| 81 | * system property which specifies the list of components which must wait for the ComponentExecutorFactory |
| 82 | * service. This property value can be set to a wildcard ("*"), or a list of components implementation class |
| 83 | * prefixes (comma separated). So, all components whose class name starts with the specified prefixes will be cached |
| 84 | * until the ComponentExecutorFactory service is registered (In this way, it is not necessary to use |
| 85 | * the StartLevel service if you want to ensure that all components are started concurrently). |
| 86 | * <p> |
| 87 | * |
| 88 | * Some class name prefixes can also be negated (using "!"), in order to exclude some components from the |
| 89 | * list of components using the ComponentExecutorFactory service. |
| 90 | * <p> |
| 91 | * |
| 92 | * Notice that if the ComponentExecutorFactory itself and all its dependent services are defined using |
| 93 | * the Dependency Manager API, then you have to list the package of such components with a "!" |
| 94 | * prefix, in order to indicate that those components must not wait for a ComponentExecutorFactory service |
| 95 | * (since they are part of the ComponentExecutorFactory implementation !). |
| 96 | * <p> |
| 97 | * |
| 98 | * <h3>Examples for the usage of the "org.apache.felix.dependencymanager.parallel" property:</h3> |
| 99 | * |
| 100 | * <blockquote><pre> |
| 101 | * org.apache.felix.dependencymanager.parallel=* |
| 102 | * -> means all components must be cached until a ComponentExecutorFactory comes up. |
| 103 | * |
| 104 | * org.apache.felix.dependencymanager.parallel=foo.bar, foo.zoo |
| 105 | * -> means only components whose implementation class names are starting with "foo.bar" or "foo.zoo" |
| 106 | * must be handled using an Executor returned by the ComponentExecutorFactory service. Other Components |
| 107 | * will be handled normally, as when there is no ComponentExecutorFactory available. |
| 108 | * |
| 109 | * org.apache.felix.dependencymanager.parallel=!foo.threadpool, * |
| 110 | * -> means all components must be delayed until the ComponentExecutorFactory comes up, except the |
| 111 | * components whose implementations class names are starting with "foo.threadpool" prefix). |
| 112 | * </pre></blockquote> |
| 113 | * |
| 114 | * <h3>Examples of a ComponentExecutorFactory that provides a shared threadpool:</h3> |
| 115 | * |
| 116 | * First, we define the OSGi bundle context system property to enable parallelism for all DM Components |
| 117 | * excepts the one which declares the ComponentExecutorFactory: |
| 118 | * |
| 119 | * <blockquote> <pre> |
| 120 | * org.apache.felix.dependencymanager.parallel=!com.acme.management.threadpool, * |
| 121 | * </pre></blockquote> |
| 122 | * |
| 123 | * Next, here is the Activator which declares the ComponentExecutorFactory: |
| 124 | * |
| 125 | * <blockquote> <pre> |
| 126 | * package com.acme.management.threadpool; |
| 127 | * import org.apache.felix.dm.*; |
| 128 | * |
| 129 | * public class Activator extends DependencyActivatorBase { |
| 130 | * public void init(BundleContext context, DependencyManager mgr) throws Exception { |
| 131 | * mgr.add(createComponent() |
| 132 | * .setInterface(ComponentExecutorFactory.class.getName(), null) |
| 133 | * .setImplementation(ComponentExecutorFactoryImpl.class) |
| 134 | * .add(createConfigurationDependency() |
| 135 | * .setPid("com.acme.management.threadpool.ComponentExecutorFactoryImpl"))); |
| 136 | * } |
| 137 | * } |
| 138 | * </pre></blockquote> |
| 139 | * |
| 140 | * And here is the implementation for our ComponentExecutorFactory: |
| 141 | * |
| 142 | * <blockquote> <pre> |
| 143 | * package com.acme.management.threadpool; |
| 144 | * import org.apache.felix.dm.*; |
| 145 | * |
| 146 | * public class ComponentExecutorFactoryImpl implements ComponentExecutorFactory { |
| 147 | * volatile Executor m_threadPool; |
| 148 | * |
| 149 | * void updated(Dictionary conf) { |
| 150 | * m_sharedThreadPool = Executors.newFixedThreadPool(Integer.parseInt("threadpool.size")); |
| 151 | * } |
| 152 | * |
| 153 | * @Override |
| 154 | * public Executor getExecutorFor(Component component) { |
| 155 | * return m_sharedThreadPool; // Use a shared threadpool for all Components |
| 156 | * } |
| 157 | * } |
| 158 | * </pre></blockquote> |
| 159 | * |
| 160 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 161 | * @since 4.0.0 |
| 162 | */ |
| 163 | public interface ComponentExecutorFactory { |
| 164 | /** |
| 165 | * Returns an Executor (typically a shared thread pool) used to manage a given DependencyManager Component. |
| 166 | * |
| 167 | * @param component the Component to be managed by the returned Executor |
| 168 | * @return an Executor used to manage the given component, or null if the component must not be managed using any executor. |
| 169 | */ |
| 170 | Executor getExecutorFor(Component component); |
| 171 | } |