Skip to content

Commit 5ad1a58

Browse files
authored
Merge pull request #419 from Thales-Netherlands/feature/FELIX-6778
FELIX-6778 Reduce number of re-creation(s) of SCR Component Registry thread
2 parents 85251f4 + 3b674ea commit 5ad1a58

File tree

7 files changed

+217
-229
lines changed

7 files changed

+217
-229
lines changed

scr/src/main/java/org/apache/felix/scr/impl/Activator.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public class Activator extends AbstractExtender
9797
private ComponentRegistry m_componentRegistry;
9898

9999
// thread acting upon configurations
100-
private ComponentActorThread m_componentActor;
100+
private ComponentActorExecutor m_componentActor;
101101

102102
private ServiceRegistration<ServiceComponentRuntime> m_runtime_reg;
103103

@@ -210,7 +210,8 @@ protected void doStart() throws Exception
210210

211211
// prepare component registry
212212
m_componentBundles = new HashMap<>();
213-
m_componentRegistry = new ComponentRegistry( this.m_configuration, this.logger );
213+
m_componentActor = new ComponentActorExecutor( this.logger );
214+
m_componentRegistry = new ComponentRegistry( this.m_configuration, this.logger, this.m_componentActor );
214215

215216
final ServiceComponentRuntimeImpl runtime = new ServiceComponentRuntimeImpl( m_globalContext, m_componentRegistry );
216217
m_runtime_reg = m_context.registerService( ServiceComponentRuntime.class,
@@ -222,12 +223,6 @@ protected void doStart() throws Exception
222223
logger.log(Level.INFO, " Version = {0}",
223224
null, m_bundle.getVersion().toString() );
224225

225-
// create and start the component actor
226-
m_componentActor = new ComponentActorThread( this.logger );
227-
Thread t = new Thread( m_componentActor, "SCR Component Actor" );
228-
t.setDaemon( true );
229-
t.start();
230-
231226
super.doStart();
232227

233228
m_componentCommands = new ComponentCommands(m_context, runtime, m_configuration);
@@ -427,14 +422,13 @@ public void doStop() throws Exception
427422
// dispose component registry
428423
if ( m_componentRegistry != null )
429424
{
430-
m_componentRegistry.shutdown();
431425
m_componentRegistry = null;
432426
}
433427

434428
// terminate the actor thread
435429
if ( m_componentActor != null )
436430
{
437-
m_componentActor.terminate();
431+
m_componentActor.shutdownNow();
438432
m_componentActor = null;
439433
}
440434
ClassUtils.setFrameworkWiring(null);

scr/src/main/java/org/apache/felix/scr/impl/BundleComponentActivator.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Map;
3131
import java.util.StringTokenizer;
3232
import java.util.concurrent.CountDownLatch;
33+
import java.util.concurrent.ScheduledExecutorService;
3334
import java.util.concurrent.TimeUnit;
3435
import java.util.concurrent.atomic.AtomicBoolean;
3536

@@ -81,7 +82,7 @@ public class BundleComponentActivator implements ComponentActivator
8182
private final List<ComponentHolder<?>> m_holders = new ArrayList<>();
8283

8384
// thread acting upon configurations
84-
private final ComponentActorThread m_componentActor;
85+
private final ScheduledExecutorService m_componentActor;
8586

8687
// true as long as the dispose method is not called
8788
private final AtomicBoolean m_active = new AtomicBoolean( true );
@@ -196,7 +197,7 @@ public void removeServiceListener(String serviceFilterString,
196197
*/
197198
public BundleComponentActivator(final ScrLogger scrLogger,
198199
final ComponentRegistry componentRegistry,
199-
final ComponentActorThread componentActor,
200+
final ScheduledExecutorService componentActor,
200201
final BundleContext context,
201202
final ScrConfiguration configuration,
202203
final List<ComponentMetadata> cachedComponentMetadata,
@@ -712,10 +713,10 @@ public void schedule(Runnable task)
712713
{
713714
if ( isActive() )
714715
{
715-
ComponentActorThread cat = m_componentActor;
716+
ScheduledExecutorService cat = m_componentActor;
716717
if ( cat != null )
717718
{
718-
cat.schedule( task );
719+
cat.submit( task );
719720
}
720721
else
721722
{
@@ -762,7 +763,7 @@ public <T> void leaveCreate(ServiceReference<T> serviceReference)
762763
@Override
763764
public <T> void missingServicePresent(ServiceReference<T> serviceReference)
764765
{
765-
m_componentRegistry.missingServicePresent( serviceReference, m_componentActor );
766+
m_componentRegistry.missingServicePresent( serviceReference );
766767
}
767768

768769
@Override
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.scr.impl;
20+
21+
22+
import java.util.concurrent.ScheduledThreadPoolExecutor;
23+
import java.util.concurrent.ThreadFactory;
24+
25+
import org.apache.felix.scr.impl.logger.InternalLogger.Level;
26+
import org.apache.felix.scr.impl.logger.ScrLogger;
27+
28+
29+
/**
30+
* The <code>ComponentActorExecutor</code> is the thread used to act upon registered
31+
* components of the service component runtime.
32+
* This is also used by the ComponentRegistry to schedule service.changecount updates.
33+
*/
34+
class ComponentActorExecutor extends ScheduledThreadPoolExecutor
35+
{
36+
37+
private static final ThreadFactory THREAD_FACTORY = new ThreadFactory()
38+
{
39+
@Override
40+
public Thread newThread(Runnable r)
41+
{
42+
Thread thread = new Thread(r, "SCR Component Actor");
43+
thread.setDaemon(true);
44+
return thread;
45+
}
46+
};
47+
48+
private final ScrLogger logger;
49+
50+
ComponentActorExecutor(final ScrLogger log )
51+
{
52+
super( 1, THREAD_FACTORY );
53+
logger = log;
54+
}
55+
56+
@Override
57+
protected void beforeExecute(Thread t, Runnable r)
58+
{
59+
logger.log(Level.DEBUG, "Running task: " + r, null);
60+
}
61+
62+
@Override
63+
protected void afterExecute(Runnable r, Throwable t)
64+
{
65+
if (t != null)
66+
{
67+
logger.log(Level.ERROR, "Unexpected problem executing task " + r, t);
68+
}
69+
}
70+
}

scr/src/main/java/org/apache/felix/scr/impl/ComponentActorThread.java

Lines changed: 0 additions & 179 deletions
This file was deleted.

0 commit comments

Comments
 (0)