Detecting when a handler cannot be started when a Jetty is attached

I embed Jetty in the same way as described here . When RequestLogHandler cannot open the specified log file, it throws an exception, which, unfortunately, falls under org.eclipse.jetty.server.Server and is swallowed (but it is registered first, at least). This means that there is no obvious way to find out if the log handler is running correctly.

Is there a way that I'm missing to detect when the handler was unable to start?

+4
source share
2 answers

This idea is based on the implementation of WebAppContext , where you can use WebAppContext.getUnavailableException () to determine if the context has been successfully initialized.

Just replace the standard server and context implementation with your own:

 public static class MyContext extends Context { private Exception _exception; @Override protected void doStart() throws Exception { try { super.doStart(); } catch (final Exception e) { _exception = e; } } @Override protected void doStop() throws Exception { try { super.doStop(); } finally { _exception = null; } } public Exception getException() { return _exception; } } public static class MyServer extends Server implements InitializingBean { public void afterPropertiesSet() throws Exception { start(); for (final Handler h : getHandlers()) { if (h instanceof MyContext) { final MyContext c = (MyContext) h; if (c.getException() != null) { throw new RuntimeException("failed to init context " + c.getDisplayName(), c.getException()); } } } } } 

In beans.xml, just replace org.mortbay.jetty.Server (and remove init-method="start" ) and org.mortbay.jetty.servlet.Context with your own implementations.

This code is for Jetty 6, though (as in the example you are attached to), like what I have. However, I have not tested it, but it is almost the same as we successfully use in combination with WebAppContext. To extend this to RequestLogHandler, you can either do the same for any handler you use, or create a decorator to port any handler. You can look at org.mortbay.jetty.handler.HandlerWrapper for this purpose.

+2
source

How to change berth code? You could add some simple println instructions in strategic places in the RequestLogHandler, which would tell you if the handler was running.

0
source

Source: https://habr.com/ru/post/1306670/


All Articles