Search in sources :

Example 1 with OpenApiHandlerExecuteTemplate

use of com.z.gateway.handler.OpenApiHandlerExecuteTemplate 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 2 with OpenApiHandlerExecuteTemplate

use of com.z.gateway.handler.OpenApiHandlerExecuteTemplate in project gateway-dubbox by zhuzhong.

the class OpenApiAcceptHandlerImpl method addTask2Pool.

private void addTask2Pool(HttpServletResponse response, OpenApiHttpSessionBean sessionBean) {
    long currentTime = System.currentTimeMillis();
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("begin deal_sessionbean,current_time=%d,sessionbean=%s ", currentTime, sessionBean));
    }
    logger.info("added one task to thread pool");
    OpenApiHttpReqTask task = null;
    String operationType = sessionBean.getRequest().getOperationType();
    OpenApiHandlerExecuteTemplate handlerExecuteTemplate = applicationContext.getBean(operationType, OpenApiHandlerExecuteTemplate.class);
    task = new OpenApiHttpReqTask(sessionBean, handlerExecuteTemplate);
    /**
     * 走责任链,将相关的请求处理
     */
    OpenApiHttpSessionBean tmp = (OpenApiHttpSessionBean) poolHandler.addTask(task);
    // 写入响应
    OpenApiResponseUtils.writeRsp(response, tmp.getRequest());
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("end deal_sessionbean,current_time=%d,elapase_time=%d milseconds,sessionbean=%s", System.currentTimeMillis(), (System.currentTimeMillis() - currentTime), tmp));
    }
}
Also used : OpenApiHandlerExecuteTemplate(com.z.gateway.handler.OpenApiHandlerExecuteTemplate) OpenApiHttpSessionBean(com.z.gateway.protocol.OpenApiHttpSessionBean) OpenApiHttpReqTask(com.z.gateway.protocol.OpenApiHttpReqTask)

Aggregations

OpenApiHandlerExecuteTemplate (com.z.gateway.handler.OpenApiHandlerExecuteTemplate)2 OpenApiHttpSessionBean (com.z.gateway.protocol.OpenApiHttpSessionBean)2 OpenApiHttpRequestBean (com.z.gateway.common.OpenApiHttpRequestBean)1 OpenApiContext (com.z.gateway.protocol.OpenApiContext)1 OpenApiHttpReqTask (com.z.gateway.protocol.OpenApiHttpReqTask)1 AsyncContext (javax.servlet.AsyncContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 BeansException (org.springframework.beans.BeansException)1