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()) if (isDumpAfterStart())
dumpStdErr(); dumpStdErr();
if (mex.size()>0)
{
try
{
doStop();
}
catch(Throwable e)
{
mex.add(e);
}
}
mex.ifExceptionThrow(); mex.ifExceptionThrow();
LOG.info(String.format("Started @%dms",Uptime.getUptime())); LOG.info(String.format("Started @%dms",Uptime.getUptime()));

View File

@ -691,7 +691,8 @@ public class GracefulStopTest
assertThat(e.getMessage(),is("Test start failure")); 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 static class NoopHandler extends AbstractHandler

View File

@ -96,12 +96,14 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
_doStarted = true; _doStarted = true;
// start our managed and auto beans // start our managed and auto beans
try
{
for (Bean b : _beans) for (Bean b : _beans)
{ {
if (b._bean instanceof LifeCycle) if (b._bean instanceof LifeCycle)
{ {
LifeCycle l = (LifeCycle)b._bean; LifeCycle l = (LifeCycle)b._bean;
switch(b._managed) switch (b._managed)
{ {
case MANAGED: case MANAGED:
if (!l.isRunning()) if (!l.isRunning())
@ -126,6 +128,31 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
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;
}
}
/** /**
* Starts the given lifecycle. * Starts the given lifecycle.