首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何设置JAX-WS WebService调用的超时

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

Stack Overflow用户
提问于 2012-12-20 07:17:49
回答 6查看 83.8K关注 0票数 18

我正在处理一个WebService客户端,我想为我的WebService调用设置一个超时。我尝试过不同的方法,但我仍然无法做到这一点。我使用JAX从WSDL生成代码。我使用JBoss 5.1作为应用服务器和JDK1.6.0_27。我发现了这些不同的方法来设定超时,但是没有一个对我有用。

代码语言:javascript
运行
复制
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.");

现在,我所做的就是从执行器内部调用我的webservice方法。我知道这不是一个好办法,但它对我有用。伙计们,请帮助我以正确的方式做这件事。

代码语言:javascript
运行
复制
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();
        }
EN

回答 6

Stack Overflow用户

发布于 2012-12-22 06:20:33

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

代码语言:javascript
运行
复制
BindingProviderProperties.REQUEST_TIMEOUT
BindingProviderProperties.CONNECT_TIMEOUT

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

或者字符串对于JBoss

代码语言:javascript
运行
复制
javax.xml.ws.client.connectionTimeout
javax.xml.ws.client.receiveTimeout

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

代码语言:javascript
运行
复制
(BindingProvider)wsPort).getRequestContext().put(BindingProviderProperties.REQUEST_TIMEOUT, yourTimeoutInMillisec);

具体来说,对于JBoss,您可能希望使用来自org.jboss.ws.core.StubExt的属性StubExt.PROPERTY_CLIENT_TIMEOUT。详情请参见这条线

票数 22
EN

Stack Overflow用户

发布于 2013-04-23 09:05:51

就像kolossus说的,你应该用:

代码语言:javascript
运行
复制
com.sun.xml.internal.ws.client.BindingProviderProperties     

字符串值是:

代码语言:javascript
运行
复制
com.sun.xml.internal.ws.connect.timeout
com.sun.xml.internal.ws.request.timeout

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

代码语言:javascript
运行
复制
bindingProvider.getRequestContext().put(BindingProviderProperties.REQUEST_TIMEOUT,requestTimeoutMs);

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

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

代码语言:javascript
运行
复制
com.sun.xml.ws.client.BindingProviderProperties

对于REQUEST_TIMEOUT和CONNECT_TIMEOUT,您将有不同的字符串值:

代码语言:javascript
运行
复制
com.sun.xml.ws.request.timeout
com.sun.xml.ws.connect.timeout
票数 8
EN

Stack Overflow用户

发布于 2014-04-16 04:40:58

对我来说,设置javax.xml.ws.client.connectionTimeoutjavax.xml.ws.client.receiveTimeout解决了这个问题。

代码语言:javascript
运行
复制
((BindingProvider)port).getRequestContext().put("javax.xml.ws.client.connectionTimeout", timeout);
((BindingProvider)port).getRequestContext().put("javax.xml.ws.client.receiveTimeout", timeout);

参考链接

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13967069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档