xxl-job: v2.0.2 原理 目錄學(xué)習(xí)
- 0. xxl-job原理
- 1. xxl-job原理---定時任務(wù)架構(gòu)
- 2. xxl-job原理-- 調(diào)度中心
- 3. xxl-job原理-- 執(zhí)行器注冊
- 4. xxl-job原理-- 執(zhí)行器注冊問題
- 5 xxl-job原理-- 執(zhí)行器注冊問題
- 6. xxl-job 原理-- 調(diào)度中心注冊
- 7. xxl-job 原理-- 任務(wù)管理
- 8. xxl-job 原理-- 任務(wù)執(zhí)行或觸發(fā)
- 9. xxl-job原理-- jobthread的作用
- 10. xxl-job原理---回調(diào)
執(zhí)行器往調(diào)度中心注冊拙已,需要調(diào)用調(diào)度中心的注冊代碼控硼,并返回對應(yīng)的數(shù)據(jù)
調(diào)度中心提供注冊
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 {
// 檢測發(fā)送的內(nèi)容
xxlRpcRequest = parseRequest(request);
} catch (Exception e) {
writeResponse(response, ThrowableUtil.toString(e).getBytes());
return;
}
// invoke
XxlRpcResponse xxlRpcResponse = xxlRpcProviderFactory.invokeService(xxlRpcRequest);
// response-serialize + response-write 序列化返回數(shù)據(jù)
byte[] responseBytes = xxlRpcProviderFactory.getSerializer().serialize(xxlRpcResponse);
writeResponse(response, responseBytes);
}
}
xxl-rpc-core:1.4.0
XxlRpcProviderFactory
通過反射陕习,調(diào)用方法, 這里的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: 若你覺得可以、還行氧卧、過得去囱皿、甚至不太差的話埃仪,可以“關(guān)注”一下,就此謝過!