忽略 Web 服务单向操作的 HTTP 响应

Ignoring HTTP response for web service one-way operation

我正在开发一个 jax-ws web 服务,它使用单向操作将消息异步推送给订阅的消费者。

不幸的是,对于每个通知,服务器都会等待 HTTP202 响应确认,这会阻塞线程几分之一秒。这会影响系统的性能,我正在寻找解决此问题的方法。

有什么方法可以执行网络服务单向调用并忽略 HTTP 响应状态?

好的,所以在花了很多时间之后我找到了两个解决方案:

1) 使用 Apache HTTPComponents,它为 AsyncHTTPClient 提供了很好的 API 允许我们从头开始构建 HTTP 响应。

2) 更多基于 Apache CXF 平台的面向 Web 服务的解决方案(包括 HTTPClient 实现)——首先我们需要设置全局总线 属性:

Bus bus = BusFactory.getDefaultBus();
bus.setProperty(AsyncHTTPConduit.USE_ASYNC, Boolean.TRUE);

然后,我们使用自定义拦截器将消息交换属性设置为异步:

final class SkipWaitInterceptor extends AbstractSoapInterceptor {

    SkipWaitInterceptor() {
        super(Phase.SETUP);
    }

    @Override
    public void handleMessage(final SoapMessage message) throws Fault {
        message.getExchange().setSynchronous(false);
    }

}

最后,我们在异步端点上注册拦截器

org.apache.cxf.endpoint.Client client =
                org.apache.cxf.frontend.ClientProxy.getClient(this.notificationConsumer);
org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint();
cxfEndpoint.getOutInterceptors().add(new SkipWaitInterceptor());

就这样,单向操作响应不再阻塞通信