xxl-job一共定义了5种类型的执行器,这里放个截图
这里我们挑选springboot项目来进行分析。
XxlJobExecutorApplication
,这是个简单的启动类。public static void main(String[] args) {
SpringApplication.run(XxlJobExecutorApplication.class, args);
}
XxlJobConfig
,这里会读取resources/application.properties
的配置信息,然后执行xxlJobExecutor()
方法,贴下代码。@Bean(initMethod = "start", destroyMethod = "destroy")
public XxlJobSpringExecutor xxlJobExecutor() {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppName(appName);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
XxlJobSpringExecutor
的start()
方法,OK,让我们在来看看他做了什么。@Override
public void start() throws Exception {
// 注册所有的jobHandler
initJobHandlerRepository(applicationContext);
// 不是很重要,就是注册个工程
GlueFactory.refreshInstance(1);
// 调用父类的start方法
super.start();
}
XxlJobExecutor
的start()
方法执行,我们在来看看。public void start() throws Exception {
// 初始化日志路径
XxlJobFileAppender.initLogPath(logPath);
// 初始化调度中心的本地列表
initAdminBizList(adminAddresses, accessToken);
// 日志清除线程
JobLogFileCleanThread.getInstance().start(logRetentionDays);
// 回调线程开启
TriggerCallbackThread.getInstance().start();
port = port>0?port: NetUtil.findAvailablePort(9999);
ip = (ip!=null&&ip.trim().length()>0)?ip: IpUtil.getIp();
// 初始化服务器
initRpcProvider(ip, port, appName, accessToken);
}
initAdminBizList()
和initRpcProvider()
,那么同样看代码。private void initAdminBizList(String adminAddresses, String accessToken) throws Exception {
if (adminAddresses!=null && adminAddresses.trim().length()>0) {
for (String address: adminAddresses.trim().split(",")) {
if (address!=null && address.trim().length()>0) {
String addressUrl = address.concat(AdminBiz.MAPPING);
AdminBiz adminBiz = (AdminBiz) new XxlRpcReferenceBean(
NetEnum.JETTY,
Serializer.SerializeEnum.HESSIAN.getSerializer(),
CallType.SYNC,
LoadBalance.ROUND,
AdminBiz.class,
null,
10000,
addressUrl,
accessToken,
null,
null
).getObject();
if (adminBizList == null) {
adminBizList = new ArrayList<AdminBiz>();
}
adminBizList.add(adminBiz);
}
}
}
}
这里主要意思就是得到AdminBiz
代理并放入到adminBizList
集合中,获取代理的方法是getObject()
,然后接着看,接下来就是完成向任务调度中心发送请求进行服务注册操作,我们主要看如何发起请求的代码。
client.asyncSend(finalAddress, xxlRpcRequest);
这里会有三种实现,xxl-job
默认采用JettyClient
,继续追踪会发现其实就是封装了一个Request请求,然后向注册中心发送。这样注册流程就完成了。
initRpcProvider()
方法,那这个方法做了什么呢?想必看名字也大概知道了,没错,就是启动一个web服务,供之后调度中心的分发任务,看代码。private void initRpcProvider(String ip, int port, String appName, String accessToken) throws Exception {
// 初始化服务注册工厂
String address = IpUtil.getIpPort(ip, port);
Map<String, String> serviceRegistryParam = new HashMap<String, String>();
serviceRegistryParam.put("appName", appName);
serviceRegistryParam.put("address", address);
xxlRpcProviderFactory = new XxlRpcProviderFactory();
xxlRpcProviderFactory.initConfig(NetEnum.JETTY, Serializer.SerializeEnum.HESSIAN.getSerializer(), ip, port, accessToken, ExecutorServiceRegistry.class, serviceRegistryParam);
// 添加服务
xxlRpcProviderFactory.addService(ExecutorBiz.class.getName(), null, new ExecutorBizImpl());
// 启动一个web服务
xxlRpcProviderFactory.start();
}
start()
方法。public void start() throws Exception {
// start server
serviceAddress = IpUtil.getIpPort(this.ip, port);
server = netType.serverClass.newInstance();
server.setStartedCallback(new BaseCallback() { // serviceRegistry started
@Override
public void run() throws Exception {
// start registry
if (serviceRegistryClass != null) {
serviceRegistry = serviceRegistryClass.newInstance();
serviceRegistry.start(serviceRegistryParam);
if (serviceData.size() > 0) {
serviceRegistry.registry(serviceData.keySet(), serviceAddress);
}
}
}
});
server.setStopedCallback(new BaseCallback() { // serviceRegistry stoped
@Override
public void run() {
// stop registry
if (serviceRegistry != null) {
if (serviceData.size() > 0) {
serviceRegistry.remove(serviceData.keySet(), serviceAddress);
}
serviceRegistry.stop();
serviceRegistry = null;
}
}
});
//前面就是一些地址,回调的设置,这里就是启动了一个jetty服务
server.start(this);
}