Search in sources :

Example 1 with OpenApiHttpSessionBean

use of com.z.gateway.protocol.OpenApiHttpSessionBean in project gateway-dubbox by zhuzhong.

the class OpenApiReqHandler method doExcuteBiz.

// step2
@Override
public boolean doExcuteBiz(Context context) {
    OpenApiContext openApiContext = (OpenApiContext) context;
    OpenApiHttpSessionBean httpSessionBean = (OpenApiHttpSessionBean) openApiContext.getOpenApiHttpSessionBean();
    OpenApiHttpRequestBean request = httpSessionBean.getRequest();
    long currentTime = System.currentTimeMillis();
    if (logger.isDebugEnabled()) {
        logger.info(String.format("begin run doExecuteBiz,currentTime=%d,httpSessonBean=%s", currentTime, httpSessionBean));
    }
    String routeBeanKey = request.getRouteBeanKey();
    OpenApiRouteBean routeBean = (OpenApiRouteBean) cacheService.get(routeBeanKey);
    // 返回值
    routeBean.setServiceRsp(doInvokeBackService(routeBean));
    if (logger.isDebugEnabled()) {
        logger.info(String.format("end run doExecuteBiz,currentTime=%d,elapase_time=%d milseconds,httpSessonBean=%s", System.currentTimeMillis(), (System.currentTimeMillis() - currentTime), httpSessionBean));
    }
    return false;
}
Also used : OpenApiRouteBean(com.z.gateway.core.OpenApiRouteBean) OpenApiHttpSessionBean(com.z.gateway.protocol.OpenApiHttpSessionBean) OpenApiHttpRequestBean(com.z.gateway.common.OpenApiHttpRequestBean) OpenApiContext(com.z.gateway.protocol.OpenApiContext)

Example 2 with OpenApiHttpSessionBean

use of com.z.gateway.protocol.OpenApiHttpSessionBean in project gateway-dubbox by zhuzhong.

the class AsynOpenApiAcceptHandlerImpl method acceptRequest.

@Override
public void acceptRequest(HttpServletRequest request, HttpServletResponse response) {
    final AsyncContext context = request.startAsync(request, response);
    taskExecutor.submit(new Runnable() {

        @Override
        public void run() {
            try {
                HttpServletRequest asynRequest = (HttpServletRequest) context.getRequest();
                OpenApiHttpRequestBean reqBean = (OpenApiHttpRequestBean) asynRequest.getAttribute(CommonCodeConstants.REQ_BEAN_KEY);
                String traceId = idService.genInnerRequestId();
                reqBean.setTraceId(traceId);
                // 重新设置bean
                asynRequest.setAttribute(CommonCodeConstants.REQ_BEAN_KEY, reqBean);
                if (logger.isInfoEnabled()) {
                    logger.info(String.format("requestId=%s request begin,reqeust=%s", traceId, JSON.toJSONString(reqBean)));
                }
                OpenApiHttpSessionBean sessionBean = new OpenApiHttpSessionBean(reqBean);
                String operationType = sessionBean.getRequest().getOperationType();
                OpenApiHandlerExecuteTemplate handlerExecuteTemplate = applicationContext.getBean(operationType, OpenApiHandlerExecuteTemplate.class);
                OpenApiContext openApiContext = new OpenApiContext();
                openApiContext.setOpenApiHttpSessionBean(sessionBean);
                try {
                    handlerExecuteTemplate.execute(openApiContext);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                // 写入响应
                OpenApiResponseUtils.writeRsp((HttpServletResponse) context.getResponse(), sessionBean.getRequest());
            } finally {
                context.complete();
            }
        }
    });
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) OpenApiHandlerExecuteTemplate(com.z.gateway.handler.OpenApiHandlerExecuteTemplate) OpenApiHttpSessionBean(com.z.gateway.protocol.OpenApiHttpSessionBean) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) OpenApiHttpRequestBean(com.z.gateway.common.OpenApiHttpRequestBean) BeansException(org.springframework.beans.BeansException) OpenApiContext(com.z.gateway.protocol.OpenApiContext)

Example 3 with OpenApiHttpSessionBean

use of com.z.gateway.protocol.OpenApiHttpSessionBean in project gateway-dubbox by zhuzhong.

the class OpenApiReqAdapter method doExcuteBiz.

@Override
public boolean doExcuteBiz(Context context) {
    OpenApiContext openApiContext = (OpenApiContext) context;
    OpenApiHttpSessionBean httpSessionBean = (OpenApiHttpSessionBean) openApiContext.getOpenApiHttpSessionBean();
    OpenApiHttpRequestBean request = httpSessionBean.getRequest();
    long currentTime = System.currentTimeMillis();
    if (logger.isDebugEnabled()) {
        logger.info(String.format("begin run doExecuteBiz,currentTime=%d,httpSessonBean=%s", currentTime, httpSessionBean));
    }
    // 参数校验
    validateParam(request);
    // 权限校验
    authRequestBean(request);
    // 初始化路由bean
    initRouteBean(httpSessionBean.getRequest());
    if (logger.isDebugEnabled()) {
        logger.info(String.format("end run doExecuteBiz,currentTime=%d,elapase_time=%d milseconds,httpSessonBean=%s", System.currentTimeMillis(), (System.currentTimeMillis() - currentTime), httpSessionBean));
    }
    if (StringUtils.isNotBlank(request.getPrintStr())) {
        return true;
    }
    return false;
}
Also used : OpenApiHttpSessionBean(com.z.gateway.protocol.OpenApiHttpSessionBean) OpenApiHttpRequestBean(com.z.gateway.common.OpenApiHttpRequestBean) OpenApiContext(com.z.gateway.protocol.OpenApiContext)

Example 4 with OpenApiHttpSessionBean

use of com.z.gateway.protocol.OpenApiHttpSessionBean in project gateway-dubbox by zhuzhong.

the class OpenApiRspHandler method doExcuteBiz.

/*
	 * @Override public boolean execute(Context context) {
	 * logger.info("step1----"); return doExcuteBiz(context); }
	 */
@Override
public boolean doExcuteBiz(Context context) {
    logger.info("step2----");
    OpenApiContext blCtx = (OpenApiContext) context;
    OpenApiHttpSessionBean httpSessionBean = (OpenApiHttpSessionBean) blCtx.getOpenApiHttpSessionBean();
    OpenApiHttpRequestBean request = httpSessionBean.getRequest();
    long currentTime = System.currentTimeMillis();
    if (logger.isDebugEnabled()) {
        logger.info(String.format("begin run doExecuteBiz,currentTime=%d,httpSessonBean=%s", currentTime, httpSessionBean));
    }
    String printStr = this.executePrint(request);
    request.setPrintStr(printStr);
    if (logger.isDebugEnabled()) {
        logger.info(String.format("end run doExecuteBiz,currentTime=%d,elapase_time=%d milseconds,httpSessonBean=%s", System.currentTimeMillis(), (System.currentTimeMillis() - currentTime), httpSessionBean));
    }
    return false;
}
Also used : OpenApiHttpSessionBean(com.z.gateway.protocol.OpenApiHttpSessionBean) OpenApiHttpRequestBean(com.z.gateway.common.OpenApiHttpRequestBean) OpenApiContext(com.z.gateway.protocol.OpenApiContext)

Example 5 with OpenApiHttpSessionBean

use of com.z.gateway.protocol.OpenApiHttpSessionBean 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)

Aggregations

OpenApiHttpSessionBean (com.z.gateway.protocol.OpenApiHttpSessionBean)7 OpenApiHttpRequestBean (com.z.gateway.common.OpenApiHttpRequestBean)5 OpenApiContext (com.z.gateway.protocol.OpenApiContext)4 OpenApiHandlerExecuteTemplate (com.z.gateway.handler.OpenApiHandlerExecuteTemplate)2 OpenApiException (com.z.gateway.common.exception.OpenApiException)1 OpenApiRouteBean (com.z.gateway.core.OpenApiRouteBean)1 OpenApiHttpReqTask (com.z.gateway.protocol.OpenApiHttpReqTask)1 FutureTask (java.util.concurrent.FutureTask)1 AsyncContext (javax.servlet.AsyncContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 BeansException (org.springframework.beans.BeansException)1 TaskRejectedException (org.springframework.core.task.TaskRejectedException)1