Issue #3440 Stop on Unavailable

+ ContainerLifeCycle stops started beans on failure.

Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2019-03-11 17:10:03 +11:00
parent 054d7f240e
commit a9707471a0
3 changed files with 52 additions and 36 deletions

View File

@ -405,18 +405,6 @@ public class Server extends HandlerWrapper implements Attributes
if (isDumpAfterStart())
dumpStdErr();
if (mex.size()>0)
{
try
{
doStop();
}
catch(Throwable e)
{
mex.add(e);
}
}
mex.ifExceptionThrow();
LOG.info(String.format("Started @%dms",Uptime.getUptime()));

View File

@ -691,7 +691,8 @@ public class GracefulStopTest
assertThat(e.getMessage(),is("Test start failure"));
}
server.getContainedBeans(LifeCycle.class).stream().forEach(lc -> assertTrue(!lc.isRunning()));
assertTrue(server.getContainedBeans(LifeCycle.class).stream().noneMatch(LifeCycle::isRunning));
assertTrue(server.getContainedBeans(LifeCycle.class).stream().anyMatch(LifeCycle::isFailed));
}
static class NoopHandler extends AbstractHandler

View File

@ -96,35 +96,62 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
_doStarted = true;
// start our managed and auto beans
for (Bean b : _beans)
try
{
if (b._bean instanceof LifeCycle)
for (Bean b : _beans)
{
LifeCycle l = (LifeCycle)b._bean;
switch(b._managed)
if (b._bean instanceof LifeCycle)
{
case MANAGED:
if (!l.isRunning())
start(l);
break;
case AUTO:
if (l.isRunning())
unmanage(b);
else
{
manage(b);
start(l);
}
break;
default:
break;
LifeCycle l = (LifeCycle)b._bean;
switch (b._managed)
{
case MANAGED:
if (!l.isRunning())
start(l);
break;
case AUTO:
if (l.isRunning())
unmanage(b);
else
{
manage(b);
start(l);
}
break;
default:
break;
}
}
}
}
super.doStart();
super.doStart();
}
catch (Throwable t)
{
// on failure, stop any managed components that have been started
for (Bean b : _beans)
{
if (b._bean instanceof LifeCycle && b._managed == Managed.MANAGED)
{
LifeCycle l = (LifeCycle)b._bean;
if (l.isRunning())
{
try
{
l.stop();
}
catch(Throwable t2)
{
if (t2!=t)
t.addSuppressed(t2);
}
}
}
}
throw t;
}
}
/**