如何为 JAX-WS WebService 调用设置超时

2022-09-02 19:59:31

我正在处理 Web 服务客户端,并且想要为我的 Web 服务调用设置超时。我已经尝试了不同的方法,但仍然无法实现这一目标。我正在使用 JAX-WS 从 WSDL 生成代码。我使用JBoss-eap-5.1作为App Server,JDK1.6.0_27。我发现这些用于设置超时的差异方法,但它们都不适合我。

URL mbr_service_url = new URL(null,GlobalVars.MemberService_WSDL, new URLStreamHandler() {

            @Override
            protected URLConnection openConnection(URL url) throws IOException {
                URL clone_url = new URL(url.toString());
                HttpURLConnection clone_urlconnection = (HttpURLConnection) clone_url.openConnection();
                // TimeOut settings
                clone_urlconnection.setConnectTimeout(10000);
                clone_urlconnection.setReadTimeout(10000);
                return (clone_urlconnection);
            }
        });
        MemberService service = new MemberService(mbr_service_url);
        MemberPortType soap = service.getMemberPort();
        ObjectFactory factory = new ObjectFactory();
        MemberEligibilityWithEnrollmentSourceRequest request = factory.createMemberEligibilityWithEnrollmentSourceRequest();

        request.setMemberId(GlobalVars.MemberId);
        request.setEligibilityDate(value);

        ((BindingProvider) soap).getRequestContext().put(com.sun.xml.ws.client.BindingProviderProperties.REQUEST_TIMEOUT, 10000);
        ((BindingProvider) soap).getRequestContext().put(com.sun.xml.ws.client.BindingProviderProperties.CONNECT_TIMEOUT, 10000);
        ((BindingProvider) soap).getRequestContext().put(com.sun.xml.internal.ws.client.BindingProviderProperties.REQUEST_TIMEOUT, 10000);
        ((BindingProvider) soap).getRequestContext().put(com.sun.xml.internal.ws.client.BindingProviderProperties.CONNECT_TIMEOUT, 10000);
        ((BindingProvider) soap).getRequestContext().put(com.sun.xml.ws.developer.JAXWSProperties.REQUEST_TIMEOUT, 10000);
        ((BindingProvider) soap).getRequestContext().put(com.sun.xml.ws.developer.JAXWSProperties.CONNECT_TIMEOUT, 10000);
        ((BindingProvider) soap).getRequestContext().put(com.sun.xml.internal.ws.developer.JAXWSProperties.REQUEST_TIMEOUT, 10000);
        ((BindingProvider) soap).getRequestContext().put(com.sun.xml.internal.ws.developer.JAXWSProperties.CONNECT_TIMEOUT, 10000);
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        MemberEligibilityWithEnrollmentSourceResponse response = soap.getMemberEligibilityWithEnrollmentSource(request);
        logger.log("Call to member service finished.");

现在,我所做的是,我已经从执行器内部调用了我的Web服务方法。我知道这不是一个好方法,但它对我有用。伙计们,请帮助我以适当的方式做到这一点。

logger.log("Parameters set for createorUpdateContact call.\nGoing in Executor Service.");
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    response = soap.getMemberEligibilityWithEnrollmentSource(request);
                } catch (MemberServiceException ex) {
                    logger.log("Exception in call to WebService", ex.fillInStackTrace());
                }
            }
        });
        executorService.shutdown();
        try {
            executorService.awaitTermination(GlobalVars.WSCallTimeOut, TimeUnit.SECONDS);
        } catch (InterruptedException ex) {
            logger.log("Thread Interrupted!", ex);
            executorService.shutdownNow();
        }

答案 1

您可以尝试这些设置(它们配对成对使用)

BindingProviderProperties.REQUEST_TIMEOUT
BindingProviderProperties.CONNECT_TIMEOUT

BindingProviderProperties应来自com.sun.xml.internal.WS.client

或者 JBoss 的字符串:

javax.xml.ws.client.connectionTimeout
javax.xml.ws.client.receiveTimeout

要放置的所有属性(以毫秒为单位)。getRequestContext()

(BindingProvider)wsPort).getRequestContext().put(BindingProviderProperties.REQUEST_TIMEOUT, yourTimeoutInMillisec);

特别是对于 JBoss,您可能希望使用 中的属性。有关详细信息,请参阅此线程StubExt.PROPERTY_CLIENT_TIMEOUTorg.jboss.ws.core.StubExt


答案 2

就像kolossus说你应该使用:

com.sun.xml.internal.ws.client.BindingProviderProperties     

字符串值为:

com.sun.xml.internal.ws.connect.timeout
com.sun.xml.internal.ws.request.timeout

虽然不应该使用内部包,但这是使用默认 JDK6 的唯一方法。因此,在这种情况下,应通过以下方式完成接收和连接超时设置:

bindingProvider.getRequestContext().put(BindingProviderProperties.REQUEST_TIMEOUT,requestTimeoutMs);

bindingProvider.getRequestContext().put(BindingProviderProperties.CONNECT_TIMEOUT,connectTimeoutMs);

但请注意,如果您使用的是其他 JAXWS 参考实现,即 JAXWS-RT 2.1.4 BindingProviderProperties,则常量值是不同的:

com.sun.xml.ws.client.BindingProviderProperties

您将为REQUEST_TIMEOUT和CONNECT_TIMEOUT提供不同的字符串值:

com.sun.xml.ws.request.timeout
com.sun.xml.ws.connect.timeout

推荐