18

我正在开发一个 WebService 客户端,我想为我的 WebService 调用设置一个超时。我尝试了不同的方法,但我仍然无法实现这一点。我正在使用 JAX-WS 从 WSDL 生成代码。我使用 JBoss-eap-5.1 作为应用服务器和 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.");

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

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();
        }
4

6 回答 6

22

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

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_TIMEOUT使用org.jboss.ws.core.StubExt. 有关详细信息,请参阅此线程

于 2012-12-22T06:20:33.300 回答
8

就像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
于 2013-04-23T09:05:51.813 回答
5

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

((BindingProvider)port).getRequestContext().put("javax.xml.ws.client.connectionTimeout", timeout);
((BindingProvider)port).getRequestContext().put("javax.xml.ws.client.receiveTimeout", timeout);

参考链接

于 2014-04-16T04:40:58.117 回答
3

升级 jbossws-native 库并使用 StubExt.PROPERTY_CLIENT_TIMEOUT

要升级 jbossws-native,请点击此链接

*jbossws-native-3.4.0 是 Jboss 5.1.0GA 支持的最新版本。你可以看到JBossWS - Supported Target Containers

这对我有用

于 2012-12-25T12:11:29.363 回答
3

设置以下选项对我有用。我正在使用 Metro JAXWS 实现。

((BindingProvider)portType).getRequestContext().put(JAXWSProperties.CONNECT_TIMEOUT, 10000);
((BindingProvider) portType).getRequestContext().put(JAXWSProperties.REQUEST_TIMEOUT, 50000);

portType 是 Web 服务端点接口。

来自 com.sun.xml.internal.ws.developer.JAXWSProperties 的上述字段的值

public static final java.lang.String CONNECT_TIMEOUT = "com.sun.xml.internal.ws.connect.timeout";
public static final java.lang.String REQUEST_TIMEOUT = "com.sun.xml.internal.ws.request.timeout";
于 2014-09-25T21:01:48.437 回答
0

我有一个具有此环境的旧安装运行时:Jdk-1.5、Jboss-4.2.3.GA 和 WSClient 是由 JAX-WS 规范 2.0 创建的。

激活肥皂请求超时我使用以下代码 ((BindingProvider)port).getRequestContext().put(org.jboss.ws.core.StubExt.PROPERTY_CLIENT_TIMEOUT, String.valueOf(readTimeout));

jbossws-client.jar和复制的罐子jboss-4.2.3.GA\server\default\lib\

于 2019-07-11T13:15:44.677 回答