发生异常时如何正常关闭端点? [英] How to gracefully close an Endpoint when an exception occurs?

查看:168
本文介绍了发生异常时如何正常关闭端点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小的JAX-WS Web服务,我在一个容器外面运行 Endpoint.publish()

I've written a small JAX-WS webservice that I'm running outside a container with Endpoint.publish():

Endpoint endpoint = Endpoint.create(new MyServiceImpl());
endpoint.publish("http://localhost:4425/myService");

如果我的任何Web服务方法抛出异常,则端点未正常关闭且地址仍然存在在Windows最终发布之前一直在使用。这会导致经典错误:

If any of my web service methods throws an exception, the endpoint is not gracefully closed and the address remains in use until Windows eventually releases it. This causes the classic error:


com.sun.xml.internal.ws.server.ServerRtException:服务器运行时错误:java.net。 BindException:地址已在使用中:bind

com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.net.BindException: Address already in use: bind

我可以使用try / catch包装所有的web服务方法,但这似乎有点重复。我还尝试通过 Thread.setDefaultUncaughtExceptionHandler()安装一个清理类,但是当我的Web服务方法抛出异常时,没有触发这个。

I could wrap all of my web service methods with try/catch, but this seems a little repetitive. I also tried installing a clean-up class via Thread.setDefaultUncaughtExceptionHandler(), but this wasn't triggered when my web service method threw an exception.

有没有一种更优雅的解决方法,而不是诉诸无数的try / catch块?

Is there a more elegant way of solving this than resorting to countless try/catch blocks?

根据Waldheinz的回答,我试图使用Jetty类来支持JDK默认值。代码编译,但执行后,它会在 publish 之后立即终止。使用JDK类时,主线程将保持活动状态,直到我手动终止该进程。有什么想法会有什么不妥?我想知道某个地方是否发生异常,但被吞没,所以我看不到它。

Based on Waldheinz's answer, I've attempted to use Jetty classes in favour of JDK defaults. The code compiles, but when executed it terminates immediately after publish. When using JDK classes, the main thread would remain alive until I manually terminated the process. Any ideas what's going amiss? I wonder if an exception is happening somewhere but being swallowed so I can't see it.

Endpoint endpoint = Endpoint.create(new MyServiceImpl());

Server s = new Server(new InetSocketAddress(HOST, PORT));
ServerConnector connector = new ServerConnector(s);
connector.setReuseAddress(true);
s.setConnectors(new Connector[] { connector });
s.setHandler(new ContextHandlerCollection());

JettyHttpServer server = new JettyHttpServer(s, false);
JettyHttpContext context = (JettyHttpContext) server.createContext(PATH);
endpoint.publish(context);


推荐答案

如果绑定失败,但旧实例不是再运行,设置 SO_REUSEADDR 可能有所帮助。

If the bind fails, but the old instance is not running any more, setting SO_REUSEADDR is likely to help.

这篇关于发生异常时如何正常关闭端点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆