Apache CXF捕获SocketTimeoutException

时间:2018-10-22 10:27:00

标签: java exception exception-handling cxf

我有CXF service并为此设置了超时时间:

@Bean(name = "MyPortType")
public MyPortType myPortType() {
    JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
    jaxWsProxyFactoryBean.setServiceClass(MyPortType .class);
    jaxWsProxyFactoryBean.setAddress(myUrl);
    jaxWsProxyFactoryBean.getInInterceptors().add(new LoggingInInterceptor());
    jaxWsProxyFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
    jaxWsProxyFactoryBean.getOutInterceptors().add(new FaultOutInterceptor());
    jaxWsProxyFactoryBean.getOutInterceptors().add(new FaultOutInterceptor());
    MyPortType myPortType = (MyPortType ) jaxWsProxyFactoryBean.create();

    Client client = ClientProxy.getClient(signingPortType);
    configClient(client, 60000L, 30000L);

    return myPortType ;
}

private void configClient(Client client, long connectionTimeout, long receiveTimeout) {
    if (client != null) {
        HTTPConduit conduit = (HTTPConduit) client.getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setConnectionTimeout(connectionTimeout);
        policy.setReceiveTimeout(receiveTimeout);
        conduit.setClient(policy);
    }
}

我设置了连接超时并接收超时。不,我尝试在我的代码中使用此服务:

     try {
        //create request
        myPortType.sendRequest(myRequest);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

如何捕获TimeOutException?

如果我再试一次:

      } catch (Exception ex) {
        if (ex instanceof SocketTimeoutException) {
          log.error(ex .getMessage(), ex );
        }
        else if (ex instanceof ConnectException) {
            log.error(ex .getMessage(), ex );
        }

    }

Intellij IDE说:

Condition 'ex instanceof SocketTimeoutException' is always 'false' less... (Ctrl+F1) 

编辑

enter image description here

public byte[] sign(SigningRequestType request) throws SigningFaultMessage;

1 个答案:

答案 0 :(得分:0)

你可以写这个

catch(SocketTimeoutException ex) {
log.error(ex.getMessage(), ex ); //TODO specific thing if socket time out
} catch(ConnectException ex) {
log.error(ex.getMessage(), ex ); //TODO specific thing if connect expcetion happened
}catch (Exception ex) {
log.error(ex.getMessage(), ex );   //TODO specific thing if generic exception happened
}

如果代码抛出SocketTimeoutException,它将从第一个程序段捕获。