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

时间:2012-12-20 07:17:49

标签: java web-services java-ee jboss jax-ws

我正在使用WebService客户端,我想为我的WebService调用设置一个Timeout。我尝试了不同的方法,但我仍然无法做到这一点。我正在使用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.");

现在我所做的是,我已经从执行者内部调用了我的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();
        }

6 个答案:

答案 0 :(得分:20)

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

BindingProviderProperties.REQUEST_TIMEOUT
BindingProviderProperties.CONNECT_TIMEOUT

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

或字符串for 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。有关详细信息,请参阅this thread

答案 1 :(得分:7)

就像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

答案 2 :(得分: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);

参考link

答案 3 :(得分:3)

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

要升级jbossws-native,请按照此link

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

这对我有用

答案 4 :(得分: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";

答案 5 :(得分:0)

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

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

,并将jar jbossws-client.jar复制到jboss-4.2.3.GA\server\default\lib\

相关问题