minor cleanups

This commit is contained in:
Greg Wilkins 2017-03-17 12:18:03 +11:00
parent c1a159b7a9
commit d2d6bc3e65
5 changed files with 74 additions and 41 deletions

View File

@ -58,17 +58,9 @@ public class HTTP2Connection extends AbstractConnection
this.parser = parser;
this.session = session;
this.bufferSize = bufferSize;
this.strategy = new EatWhatYouKill(producer, executor,InvocationType.BLOCKING, InvocationType.BLOCKING, 0);
this.strategy = new EatWhatYouKill(producer, executor, 0);
// TODO clean this up.
try
{
((LifeCycle)this.strategy).start();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
LifeCycle.start(strategy);
}
@Override
@ -113,16 +105,8 @@ public class HTTP2Connection extends AbstractConnection
if (LOG.isDebugEnabled())
LOG.debug("HTTP2 Close {} ", this);
super.onClose();
// TODO clean this up.
try
{
((LifeCycle)this.strategy).stop();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
LifeCycle.stop(strategy);
}
@Override

View File

@ -122,4 +122,47 @@ public interface LifeCycle
public void lifeCycleStopping(LifeCycle event);
public void lifeCycleStopped(LifeCycle event);
}
/**
* Utility to start an object if it is a LifeCycle and to convert
* any exception thrown to a {@link RuntimeException}
* @param object The instance to start.
* @throws RuntimeException if the call to start throws an exception.
*/
public static void start(Object object)
{
if (object instanceof LifeCycle)
{
try
{
((LifeCycle)object).start();
}
catch(Exception e)
{
throw new RuntimeException(e);
}
}
}
/**
* Utility to stop an object if it is a LifeCycle and to convert
* any exception thrown to a {@link RuntimeException}
* @param object The instance to stop.
* @throws RuntimeException if the call to stop throws an exception.
*/
public static void stop(Object object)
{
if (object instanceof LifeCycle)
{
try
{
((LifeCycle)object).stop();
}
catch(Exception e)
{
throw new RuntimeException(e);
}
}
}
}

View File

@ -195,38 +195,38 @@ public interface Invocable
private static final Logger LOG = Log.getLogger(InvocableExecutor.class);
private final Executor _executor;
private final InvocationType _preferredExecutionType;
private final InvocationType _preferredInvocationType;
private final InvocationType _preferredInvocationForExecute;
private final InvocationType _preferredInvocationForInvoke;
public InvocableExecutor(Executor executor,InvocationType preferred)
{
this(executor,preferred,preferred);
}
public InvocableExecutor(Executor executor,InvocationType preferredForExecution,InvocationType preferredForIvocation)
public InvocableExecutor(Executor executor,InvocationType preferredInvocationForExecute,InvocationType preferredInvocationForIvoke)
{
_executor=executor;
_preferredExecutionType=preferredForExecution;
_preferredInvocationType=preferredForIvocation;
_preferredInvocationForExecute=preferredInvocationForExecute;
_preferredInvocationForInvoke=preferredInvocationForIvoke;
}
public Invocable.InvocationType getPreferredInvocationType()
{
return _preferredInvocationType;
return _preferredInvocationForInvoke;
}
public void invoke(Runnable task)
{
if (LOG.isDebugEnabled())
LOG.debug("{} invoke {}", this, task);
Invocable.invokePreferred(task,_preferredInvocationType);
Invocable.invokePreferred(task,_preferredInvocationForInvoke);
if (LOG.isDebugEnabled())
LOG.debug("{} invoked {}", this, task);
}
public void execute(Runnable task)
{
tryExecute(task,_preferredExecutionType);
tryExecute(task,_preferredInvocationForExecute);
}
public void execute(Runnable task, InvocationType preferred)
@ -236,7 +236,7 @@ public interface Invocable
public boolean tryExecute(Runnable task)
{
return tryExecute(task,_preferredExecutionType);
return tryExecute(task,_preferredInvocationForExecute);
}
public boolean tryExecute(Runnable task, InvocationType preferred)

View File

@ -66,16 +66,21 @@ public class EatWhatYouKill extends AbstractLifeCycle implements ExecutionStrate
this(producer,executor,InvocationType.NON_BLOCKING,InvocationType.BLOCKING);
}
public EatWhatYouKill(Producer producer, Executor executor, InvocationType preferredExecution, InvocationType preferredInvocation)
public EatWhatYouKill(Producer producer, Executor executor, int maxProducersPending )
{
this(producer,executor,preferredExecution,preferredInvocation,1);
this(producer,executor,InvocationType.NON_BLOCKING,InvocationType.BLOCKING);
}
public EatWhatYouKill(Producer producer, Executor executor, InvocationType preferredExecution, InvocationType preferredInvocation, int maxProducersPending )
public EatWhatYouKill(Producer producer, Executor executor, InvocationType preferredInvocationPEC, InvocationType preferredInvocationEPC)
{
this(producer,executor,preferredInvocationPEC,preferredInvocationEPC,1);
}
public EatWhatYouKill(Producer producer, Executor executor, InvocationType preferredInvocationPEC, InvocationType preferredInvocationEPC, int maxProducersPending )
{
_producer = producer;
_pendingProducersMax = maxProducersPending;
_executor = new InvocableExecutor(executor,preferredExecution,preferredInvocation);
_executor = new InvocableExecutor(executor,preferredInvocationPEC,preferredInvocationEPC);
}
@Override
@ -167,12 +172,15 @@ public class EatWhatYouKill extends AbstractLifeCycle implements ExecutionStrate
// spurious wakeup!
_pendingProducers--;
}
else if (_state == State.IDLE)
else
{
_pendingProducersSignalled--;
_state = State.PRODUCING;
return true;
}
if (_state == State.IDLE)
{
_state = State.PRODUCING;
return true;
}
}
}
catch (InterruptedException e)
{

View File

@ -67,8 +67,7 @@ public class ExecutionStrategyTest
void newExecutionStrategy(Producer producer, Executor executor) throws Exception
{
_strategy = _strategyClass.getConstructor(Producer.class,Executor.class).newInstance(producer,executor);
if (_strategy instanceof LifeCycle)
((LifeCycle)_strategy).start();
LifeCycle.start(_strategy);
}
@Before
@ -80,8 +79,7 @@ public class ExecutionStrategyTest
@After
public void after() throws Exception
{
if (_strategy instanceof LifeCycle)
((LifeCycle)_strategy).stop();
LifeCycle.stop(_strategy);
threads.stop();
}