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.parser = parser;
this.session = session; this.session = session;
this.bufferSize = bufferSize; 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. LifeCycle.start(strategy);
try
{
((LifeCycle)this.strategy).start();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
} }
@Override @Override
@ -114,15 +106,7 @@ public class HTTP2Connection extends AbstractConnection
LOG.debug("HTTP2 Close {} ", this); LOG.debug("HTTP2 Close {} ", this);
super.onClose(); super.onClose();
// TODO clean this up. LifeCycle.stop(strategy);
try
{
((LifeCycle)this.strategy).stop();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
} }
@Override @Override

View File

@ -122,4 +122,47 @@ public interface LifeCycle
public void lifeCycleStopping(LifeCycle event); public void lifeCycleStopping(LifeCycle event);
public void lifeCycleStopped(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 static final Logger LOG = Log.getLogger(InvocableExecutor.class);
private final Executor _executor; private final Executor _executor;
private final InvocationType _preferredExecutionType; private final InvocationType _preferredInvocationForExecute;
private final InvocationType _preferredInvocationType; private final InvocationType _preferredInvocationForInvoke;
public InvocableExecutor(Executor executor,InvocationType preferred) public InvocableExecutor(Executor executor,InvocationType preferred)
{ {
this(executor,preferred,preferred); this(executor,preferred,preferred);
} }
public InvocableExecutor(Executor executor,InvocationType preferredForExecution,InvocationType preferredForIvocation) public InvocableExecutor(Executor executor,InvocationType preferredInvocationForExecute,InvocationType preferredInvocationForIvoke)
{ {
_executor=executor; _executor=executor;
_preferredExecutionType=preferredForExecution; _preferredInvocationForExecute=preferredInvocationForExecute;
_preferredInvocationType=preferredForIvocation; _preferredInvocationForInvoke=preferredInvocationForIvoke;
} }
public Invocable.InvocationType getPreferredInvocationType() public Invocable.InvocationType getPreferredInvocationType()
{ {
return _preferredInvocationType; return _preferredInvocationForInvoke;
} }
public void invoke(Runnable task) public void invoke(Runnable task)
{ {
if (LOG.isDebugEnabled()) if (LOG.isDebugEnabled())
LOG.debug("{} invoke {}", this, task); LOG.debug("{} invoke {}", this, task);
Invocable.invokePreferred(task,_preferredInvocationType); Invocable.invokePreferred(task,_preferredInvocationForInvoke);
if (LOG.isDebugEnabled()) if (LOG.isDebugEnabled())
LOG.debug("{} invoked {}", this, task); LOG.debug("{} invoked {}", this, task);
} }
public void execute(Runnable task) public void execute(Runnable task)
{ {
tryExecute(task,_preferredExecutionType); tryExecute(task,_preferredInvocationForExecute);
} }
public void execute(Runnable task, InvocationType preferred) public void execute(Runnable task, InvocationType preferred)
@ -236,7 +236,7 @@ public interface Invocable
public boolean tryExecute(Runnable task) public boolean tryExecute(Runnable task)
{ {
return tryExecute(task,_preferredExecutionType); return tryExecute(task,_preferredInvocationForExecute);
} }
public boolean tryExecute(Runnable task, InvocationType preferred) 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); 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; _producer = producer;
_pendingProducersMax = maxProducersPending; _pendingProducersMax = maxProducersPending;
_executor = new InvocableExecutor(executor,preferredExecution,preferredInvocation); _executor = new InvocableExecutor(executor,preferredInvocationPEC,preferredInvocationEPC);
} }
@Override @Override
@ -167,13 +172,16 @@ public class EatWhatYouKill extends AbstractLifeCycle implements ExecutionStrate
// spurious wakeup! // spurious wakeup!
_pendingProducers--; _pendingProducers--;
} }
else if (_state == State.IDLE) else
{ {
_pendingProducersSignalled--; _pendingProducersSignalled--;
if (_state == State.IDLE)
{
_state = State.PRODUCING; _state = State.PRODUCING;
return true; return true;
} }
} }
}
catch (InterruptedException e) catch (InterruptedException e)
{ {
LOG.debug(e); LOG.debug(e);

View File

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