6. xxl-job 原理-- 调度中心注册

xxl-job: v2.0.2 原理 目录学习

  • 0. xxl-job原理
  • 1. xxl-job原理---定时任务架构
  • 2. xxl-job原理-- 调度中心
  • 3. xxl-job原理-- 执行器注册
  • 4. xxl-job原理-- 执行器注册问题
  • 5 xxl-job原理-- 执行器注册问题
  • 6. xxl-job 原理-- 调度中心注册
  • 7. xxl-job 原理-- 任务管理
  • 8. xxl-job 原理-- 任务执行或触发
  • 9. xxl-job原理-- jobthread的作用
  • 10. xxl-job原理---回调

执行器往调度中心注册,需要调用调度中心的注册代码,并返回对应的数据

调度中心提供注册

JobApiController

//  接口为xxl-job-admin/api
@RequestMapping(AdminBiz.MAPPING)
    @PermissionLimit(limit = false)
    public void api(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        XxlJobScheduler.invokeAdminService(request, response);
    }


XxlJobScheduler

public static void invokeAdminService(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//   项目启动时,初始化 servletServerHandler  和 xxlRpcProviderFactory
        servletServerHandler.handle(null, request, response);
    }

ServletServerHandler

/**
     * handle servlet request
     */
    public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

       ......
      // default remoting mapping

            // request parse
            XxlRpcRequest xxlRpcRequest = null;
            try {
  // 检测发送的内容
                xxlRpcRequest = parseRequest(request);
            } catch (Exception e) {
                writeResponse(response, ThrowableUtil.toString(e).getBytes());
                return;
            }

            // invoke
            XxlRpcResponse xxlRpcResponse = xxlRpcProviderFactory.invokeService(xxlRpcRequest);

            // response-serialize + response-write 序列化返回数据
            byte[] responseBytes = xxlRpcProviderFactory.getSerializer().serialize(xxlRpcResponse);
            writeResponse(response, responseBytes);
        }
    }

xxl-rpc-core:1.4.0
XxlRpcProviderFactory
通过反射,调用方法, 这里的serviceBean 为AdminBizImpl , methodName= registry

/**
     * invoke service
     *
     * @param xxlRpcRequest
     * @return
     */
    public XxlRpcResponse invokeService(XxlRpcRequest xxlRpcRequest) {

        //  make response
        XxlRpcResponse xxlRpcResponse = new XxlRpcResponse();
        xxlRpcResponse.setRequestId(xxlRpcRequest.getRequestId());

        // match service bean
        String serviceKey = makeServiceKey(xxlRpcRequest.getClassName(), xxlRpcRequest.getVersion());
        Object serviceBean = serviceData.get(serviceKey);

        // valid
        if (serviceBean == null) {
            xxlRpcResponse.setErrorMsg("The serviceKey["+ serviceKey +"] not found.");
            return xxlRpcResponse;
        }

        if (System.currentTimeMillis() - xxlRpcRequest.getCreateMillisTime() > 3*60*1000) {
            xxlRpcResponse.setErrorMsg("The timestamp difference between admin and executor exceeds the limit.");
            return xxlRpcResponse;
        }
        if (accessToken!=null && accessToken.trim().length()>0 && !accessToken.trim().equals(xxlRpcRequest.getAccessToken())) {
            xxlRpcResponse.setErrorMsg("The access token[" + xxlRpcRequest.getAccessToken() + "] is wrong.");
            return xxlRpcResponse;
        }

        try {
            // invoke
            Class serviceClass = serviceBean.getClass();
            String methodName = xxlRpcRequest.getMethodName();
            Class[] parameterTypes = xxlRpcRequest.getParameterTypes();
            Object[] parameters = xxlRpcRequest.getParameters();

            Method method = serviceClass.getMethod(methodName, parameterTypes);
            method.setAccessible(true);
            Object result = method.invoke(serviceBean, parameters);

            /*FastClass serviceFastClass = FastClass.create(serviceClass);
            FastMethod serviceFastMethod = serviceFastClass.getMethod(methodName, parameterTypes);
            Object result = serviceFastMethod.invoke(serviceBean, parameters);*/

            xxlRpcResponse.setResult(result);
        } catch (Throwable t) {
            // catch error
            logger.error("xxl-rpc provider invokeService error.", t);
            xxlRpcResponse.setErrorMsg(ThrowableUtil.toString(t));
        }

        return xxlRpcResponse;
    }

PS: 若你觉得可以、还行、过得去、甚至不太差的话,可以“关注”一下,就此谢过!

你可能感兴趣的:(6. xxl-job 原理-- 调度中心注册)