HubRestClient.java 文件源码

java
阅读 17 收藏 0 点赞 0 评论 0

项目:base 作者:
private <T> RestResult<T> execute( String httpMethod, String url, Object body, Class<T> clazz, boolean encrypt )
{
    log.info( "{} {}", httpMethod, url );

    WebClient webClient = null;
    Response response = null;
    RestResult<T> restResult = new RestResult<>( HttpStatus.SC_INTERNAL_SERVER_ERROR );

    try
    {
        webClient = configManager.getTrustedWebClientWithAuth( url, configManager.getHubIp() );

        Object requestBody = encrypt ? encryptBody( body ) : body;

        response = webClient.invoke( httpMethod, requestBody );

        // retry on 503 http code >>>
        int attemptNo = 1;
        while ( response.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE && attemptNo < MAX_ATTEMPTS )
        {
            attemptNo++;
            response = webClient.invoke( httpMethod, requestBody );
            TaskUtil.sleep( 500 );
        }
        // <<< retry on 503 http code

        log.info( "response.status: {} - {}", response.getStatus(), response.getStatusInfo().getReasonPhrase() );

        restResult = handleResponse( response, clazz, encrypt );
    }
    catch ( Exception e )
    {
        if ( response != null )
        {
            restResult.setReasonPhrase( response.getStatusInfo().getReasonPhrase() );
        }

        Throwable rootCause = ExceptionUtil.getRootCauze( e );
        if ( rootCause instanceof ConnectException || rootCause instanceof UnknownHostException
                || rootCause instanceof BindException || rootCause instanceof NoRouteToHostException
                || rootCause instanceof PortUnreachableException || rootCause instanceof SocketTimeoutException )
        {
            restResult.setError( CONNECTION_EXCEPTION_MARKER );
        }
        else
        {
            restResult.setError( ERROR + e.getMessage() );
        }

        log.error( ERROR + e.getMessage() );
    }
    finally
    {
        close( webClient, response );
    }

    return restResult;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号