如何设置超时为JAX-WS Web服务调用(How to Set Timeout for JAX-W

2019-07-17 11:02发布

我工作的一个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.");

现在我所做的是,我已要求从遗嘱执行人在我的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();
        }

Answer 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 。 请参阅此线程的详细信息。



Answer 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


Answer 3:

对我来说,设置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);

参考链接



Answer 4:

升级将JBossWS本地库,并使用StubExt.PROPERTY_CLIENT_TIMEOUT

升级将JBossWS本地的,按照这个链接 。

*将JBossWS-原生3.4.0是对于JBoss 5.1.0GA支持的最新版本。 你可以看到将JBossWS -支持的目标容器

这为我工作



Answer 5:

设置下列选项为我工作。 我现在用的是地铁JAXWS的实现。

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

端口类型是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";


文章来源: How to Set Timeout for JAX-WS WebService Call