Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions scr/src/main/java/org/apache/felix/scr/impl/Activator.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public class Activator extends AbstractExtender
private ComponentRegistry m_componentRegistry;

// thread acting upon configurations
private ComponentActorExecutor m_componentActor;
private ComponentActorThread m_componentActor;

private ServiceRegistration<ServiceComponentRuntime> m_runtime_reg;

Expand Down Expand Up @@ -210,8 +210,7 @@ protected void doStart() throws Exception

// prepare component registry
m_componentBundles = new HashMap<>();
m_componentActor = new ComponentActorExecutor( this.logger );
m_componentRegistry = new ComponentRegistry( this.m_configuration, this.logger, this.m_componentActor );
m_componentRegistry = new ComponentRegistry( this.m_configuration, this.logger );

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

// create and start the component actor
m_componentActor = new ComponentActorThread( this.logger );
Thread t = new Thread( m_componentActor, "SCR Component Actor" );
t.setDaemon( true );
t.start();

super.doStart();

m_componentCommands = new ComponentCommands(m_context, m_globalContext, runtime, m_configuration);
Expand Down Expand Up @@ -425,13 +430,14 @@ public void doStop() throws Exception
// dispose component registry
if ( m_componentRegistry != null )
{
m_componentRegistry.shutdown();
m_componentRegistry = null;
}

// terminate the actor thread
if ( m_componentActor != null )
{
m_componentActor.shutdownNow();
m_componentActor.terminate();
m_componentActor = null;
}
ClassUtils.setFrameworkWiring(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.Map;
import java.util.StringTokenizer;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

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

// thread acting upon configurations
private final ScheduledExecutorService m_componentActor;
private final ComponentActorThread m_componentActor;

// true as long as the dispose method is not called
private final AtomicBoolean m_active = new AtomicBoolean( true );
Expand Down Expand Up @@ -197,7 +196,7 @@ public void removeServiceListener(String serviceFilterString,
*/
public BundleComponentActivator(final ScrLogger scrLogger,
final ComponentRegistry componentRegistry,
final ScheduledExecutorService componentActor,
final ComponentActorThread componentActor,
final BundleContext context,
final ScrConfiguration configuration,
final List<ComponentMetadata> cachedComponentMetadata,
Expand Down Expand Up @@ -713,10 +712,10 @@ public void schedule(Runnable task)
{
if ( isActive() )
{
ScheduledExecutorService cat = m_componentActor;
ComponentActorThread cat = m_componentActor;
if ( cat != null )
{
cat.submit( task );
cat.schedule( task );
}
else
{
Expand Down Expand Up @@ -763,7 +762,7 @@ public <T> void leaveCreate(ServiceReference<T> serviceReference)
@Override
public <T> void missingServicePresent(ServiceReference<T> serviceReference)
{
m_componentRegistry.missingServicePresent( serviceReference );
m_componentRegistry.missingServicePresent( serviceReference, m_componentActor );
}

@Override
Expand Down

This file was deleted.

179 changes: 179 additions & 0 deletions scr/src/main/java/org/apache/felix/scr/impl/ComponentActorThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.felix.scr.impl;


import java.util.LinkedList;

import org.apache.felix.scr.impl.logger.InternalLogger.Level;
import org.apache.felix.scr.impl.logger.ScrLogger;


/**
* The <code>ComponentActorThread</code> is the thread used to act upon registered
* components of the service component runtime.
*/
class ComponentActorThread implements Runnable
{

// sentinel task to terminate this thread
private static final Runnable TERMINATION_TASK = new Runnable()
{
@Override
public void run()
{
}


@Override
public String toString()
{
return "Component Actor Terminator";
}
};

// the queue of Runnable instances to be run
private final LinkedList<Runnable> tasks = new LinkedList<>();

private final ScrLogger logger;


ComponentActorThread( final ScrLogger log )
{
logger = log;
}


// waits on Runnable instances coming into the queue. As instances come
// in, this method calls the Runnable.run method, logs any exception
// happening and keeps on waiting for the next Runnable. If the Runnable
// taken from the queue is this thread instance itself, the thread
// terminates.
@Override
public void run()
{
logger.log(Level.DEBUG, "Starting ComponentActorThread", null);

for ( ;; )
{
final Runnable task;
synchronized ( tasks )
{
while ( tasks.isEmpty() )
{
boolean interrupted = Thread.interrupted();
try
{
tasks.wait();
}
catch ( InterruptedException ie )
{
interrupted = true;
// don't care
}
finally
{
if (interrupted)
{ // restore interrupt status
Thread.currentThread().interrupt();
}
}
}

task = tasks.removeFirst();
}

try
{
// return if the task is this thread itself
if ( task == TERMINATION_TASK )
{
logger.log(Level.DEBUG, "Shutting down ComponentActorThread",
null);
return;
}

// otherwise execute the task, log any issues
logger.log(Level.DEBUG, "Running task: " + task, null);
task.run();
}
catch ( Throwable t )
{
logger.log(Level.ERROR, "Unexpected problem executing task " + task,
t);
}
finally
{
synchronized ( tasks )
{
tasks.notifyAll();
}
}
}
}


// cause this thread to terminate by adding this thread to the end
// of the queue
void terminate()
{
schedule( TERMINATION_TASK );
synchronized ( tasks )
{
while ( !tasks.isEmpty() )
{
boolean interrupted = Thread.interrupted();
try
{
tasks.wait();
}
catch ( InterruptedException e )
{
interrupted = true;
logger.log(Level.ERROR,
"Interrupted exception waiting for queue to empty", e);
}
finally
{
if (interrupted)
{ // restore interrupt status
Thread.currentThread().interrupt();
}
}
}
}
}


// queue the given runnable to be run as soon as possible
void schedule( Runnable task )
{
synchronized ( tasks )
{
// append to the task queue
tasks.add( task );

logger.log(Level.DEBUG, "Adding task [{0}] as #{1} in the queue", null,
task, tasks.size());

// notify the waiting thread
tasks.notifyAll();
}
}
}
Loading