Search in sources :

Example 1 with OpenApiException

use of com.z.gateway.common.exception.OpenApiException in project gateway-dubbox by zhuzhong.

the class ThreadPoolHandlerImpl method addTask.

@Override
public Object addTask(AbstractTask task) {
    try {
        FutureTask<OpenApiHttpSessionBean> tsFutre = new FutureTask<OpenApiHttpSessionBean>(task);
        taskExecutor.execute(tsFutre);
        while (!tsFutre.isDone()) {
        /*
                 * try { // logger.debug("waitting for result");
                 * TimeUnit.MICROSECONDS.sleep(200); } catch
                 * (InterruptedException e) { logger.error(String.format(
                 * "exception happend on executing task with ",
                 * e.getMessage())); }
                 */
        }
        return tsFutre.get();
    } catch (TaskRejectedException e) {
        logger.error("the queue reached max deepth");
        throw new OpenApiException(OpenApiServiceErrorEnum.SYSTEM_QUEUE_DEEPTH);
    } catch (Throwable e) {
        Throwable throwable = e.getCause();
        if (throwable instanceof OpenApiException) {
            throw (OpenApiException) throwable;
        }
        logger.error(String.format("exception happend on executing task with %s", e.getMessage()));
        OpenApiException ex = new OpenApiException(OpenApiServiceErrorEnum.SYSTEM_BUSY, throwable);
        throw ex;
    }
}
Also used : TaskRejectedException(org.springframework.core.task.TaskRejectedException) FutureTask(java.util.concurrent.FutureTask) OpenApiException(com.z.gateway.common.exception.OpenApiException) OpenApiHttpSessionBean(com.z.gateway.protocol.OpenApiHttpSessionBean)

Example 2 with OpenApiException

use of com.z.gateway.common.exception.OpenApiException in project gateway-dubbox by zhuzhong.

the class OpenApiReqHandler method doInvokeBackService.

/**
 * 根据routeBean信息,通过httpclient调用后端信息,然后将返回值构建成string
 *
 * @param bean
 * @return
 */
private String doInvokeBackService(OpenApiRouteBean bean) {
    logger.info("step5...");
    // 后端服务返回值
    String serviceRspData = null;
    String operationType = bean.getOperationType();
    String requestMethod = bean.getRequestMethod();
    if (operationType.equals(CommonCodeConstants.API_SYSERVICE_KEY)) {
    } else if (CommonCodeConstants.API_GETDATA_KEY.equals(operationType)) {
    } else if (CommonCodeConstants.API_SERVICE_KEY.equals(operationType)) {
        logger.info(String.format("{serviceId:%s ,version:%s }", bean.getApiId(), bean.getVersion()));
        ApiInterface apiInfo = apiInterfaceService.queryApiInterfaceByApiId(bean.getApiId(), bean.getVersion());
        if (apiInfo == null) {
            return String.format("this apiId=%s,version=%s has off line,please use another one", bean.getApiId(), bean.getVersion());
        }
        apiInfo.setTargetUrl(bean.getTargetUrl());
        apiInfo.setRequestMethod(bean.getRequestMethod());
        if (CommonCodeConstants.REQUEST_METHOD.GET.name().equalsIgnoreCase(requestMethod)) {
            // get请求
            String url = apiInfo.getUrl();
            url = UrlUtil.dealUrl(url, bean.getThdApiUrlParams());
            /*
                 * if (StringUtils.isNotBlank(bean.getQueryString())) { url +=
                 * "?" + bean.getQueryString(); // 串好url 地址 }
                 */
            logger.info(String.format("{service url:%s} ", url));
            // bean.getReqHeader().get(CONTENT_TYPE_KEY);
            if (url.startsWith(CommonCodeConstants.HTTPS)) {
                if (bean.getServiceGetReqData() == null) {
                    serviceRspData = apiHttpClientService.doHttpsGet(url, bean.getTraceId());
                } else {
                    serviceRspData = apiHttpClientService.doHttpsGet(url, bean.getServiceGetReqData(), bean.getTraceId());
                }
            } else {
                if (bean.getServiceGetReqData() == null) {
                    serviceRspData = apiHttpClientService.doGet(url, bean.getTraceId());
                } else {
                    serviceRspData = apiHttpClientService.doGet(url, bean.getServiceGetReqData(), bean.getTraceId());
                }
            }
        } else if (CommonCodeConstants.REQUEST_METHOD.POST.name().equalsIgnoreCase(requestMethod)) {
            // post请求
            String url = apiInfo.getUrl();
            logger.info(String.format("{service url:%s} ", url));
            // 请求的json格式数据参数
            String reqData = bean.getServiceReqData();
            if (StringUtils.isNotBlank(reqData) && reqData.length() > this.maxReqDataLth) {
                reqData = reqData.substring(0, this.maxReqDataLth - 1);
            }
            logger.info(String.format("{serviceId:%s ,reqData:%s }", bean.getApiId(), reqData));
            String contentType = bean.getReqHeader().get(CONTENT_TYPE_KEY);
            if (url.startsWith(CommonCodeConstants.HTTPS)) {
                serviceRspData = apiHttpClientService.doHttpsPost(url, bean.getServiceReqData(), contentType, bean.getTraceId());
            } else {
                serviceRspData = apiHttpClientService.doPost(url, bean.getServiceReqData(), contentType, bean.getTraceId());
            }
            if ("timeout".equals(serviceRspData)) {
                logger.error("invoke service: response is null!");
                throw new OpenApiException(OauthErrorEnum.ERROR.getErrCode(), OauthErrorEnum.ERROR.getErrMsg());
            }
        }
    } else {
    }
    return serviceRspData;
}
Also used : OpenApiException(com.z.gateway.common.exception.OpenApiException) ApiInterface(com.z.gateway.common.entity.ApiInterface)

Aggregations

OpenApiException (com.z.gateway.common.exception.OpenApiException)2 ApiInterface (com.z.gateway.common.entity.ApiInterface)1 OpenApiHttpSessionBean (com.z.gateway.protocol.OpenApiHttpSessionBean)1 FutureTask (java.util.concurrent.FutureTask)1 TaskRejectedException (org.springframework.core.task.TaskRejectedException)1