xxl-job 是一个分布式的定时任务执行框架,有调度中心和执行器两部分组成
调度中心:负责定时任务的调用
执行器:服务定时任务的执行
本篇文章介绍如何基于jfinal 5.0创建一个执行器项目并注册的调度中心中
1.添加依赖
<!-- xxl-rpc-core --> <dependency> <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core</artifactId> <version>2.4.0-SNAPSHOT</version> </dependency>
2.添加配置文件
xxl-job-executor.properties
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02" xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin ### xxl-job, access token xxl.job.accessToken=default_token ### xxl-job executor appname xxl.job.executor.appname=xxl-job-executor-sample ### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null xxl.job.executor.address= ### xxl-job executor server-info xxl.job.executor.ip= xxl.job.executor.port=9998 ### xxl-job executor log-path xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler ### xxl-job executor log-retention-days xxl.job.executor.logretentiondays=30
3.添加配置类
package top.ppnt.chaofu.job.config; import java.io.IOException; import java.io.InputStreamReader; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.xxl.job.core.executor.XxlJobExecutor; import com.xxl.job.core.executor.impl.XxlJobSimpleExecutor; import top.ppnt.chaofu.job.jobhandler.SyncJiuGanToDbJobHandler; /** * @author xuxueli 2018-10-31 19:05:43 */ public class FrameLessXxlJobConfig { private static Logger logger = LoggerFactory.getLogger(FrameLessXxlJobConfig.class); private static FrameLessXxlJobConfig instance = new FrameLessXxlJobConfig(); public static FrameLessXxlJobConfig getInstance() { return instance; } private XxlJobSimpleExecutor xxlJobExecutor = null; /** * init */ public void initXxlJobExecutor() { // load executor prop Properties xxlJobProp = loadProperties("xxl-job-executor.properties"); // init executor xxlJobExecutor = new XxlJobSimpleExecutor(); xxlJobExecutor.setAdminAddresses(xxlJobProp.getProperty("xxl.job.admin.addresses")); xxlJobExecutor.setAccessToken(xxlJobProp.getProperty("xxl.job.accessToken")); xxlJobExecutor.setAppname(xxlJobProp.getProperty("xxl.job.executor.appname")); xxlJobExecutor.setAddress(xxlJobProp.getProperty("xxl.job.executor.address")); xxlJobExecutor.setIp(xxlJobProp.getProperty("xxl.job.executor.ip")); xxlJobExecutor.setPort(Integer.valueOf(xxlJobProp.getProperty("xxl.job.executor.port"))); xxlJobExecutor.setLogPath(xxlJobProp.getProperty("xxl.job.executor.logpath")); xxlJobExecutor.setLogRetentionDays(Integer.valueOf(xxlJobProp.getProperty("xxl.job.executor.logretentiondays"))); // registry job bean //xxlJobExecutor.setXxlJobBeanList(Arrays.asList(new SyncXxlJob())); XxlJobExecutor.registJobHandler("syncJiuGanToDbJobHandler", new SyncJiuGanToDbJobHandler()); // start executor try { xxlJobExecutor.start(); } catch (Exception e) { logger.error(e.getMessage(), e); } } /** * destroy */ public void destroyXxlJobExecutor() { if (xxlJobExecutor != null) { xxlJobExecutor.destroy(); } } public static Properties loadProperties(String propertyFileName) { InputStreamReader in = null; try { ClassLoader loder = Thread.currentThread().getContextClassLoader(); in = new InputStreamReader(loder.getResourceAsStream(propertyFileName), "UTF-8"); if (in != null) { Properties prop = new Properties(); prop.load(in); return prop; } } catch (IOException e) { logger.error("load {} error!", propertyFileName); } finally { if (in != null) { try { in.close(); } catch (IOException e) { logger.error("close {} error!", propertyFileName); } } } return null; } }
4.添加job
package top.ppnt.chaofu.job.jobhandler; import com.jfinal.aop.Aop; import com.xxl.job.core.handler.IJobHandler; import top.ppnt.chaofu.job.task.SyncJiuGanToDbTask; public class SyncJiuGanToDbJobHandler extends IJobHandler { @Override public void execute() throws Exception { SyncJiuGanToDbTask syncJiuGanToDbTask = Aop.get(SyncJiuGanToDbTask.class); syncJiuGanToDbTask.index(); } }
5.在jfinal的配置类配置启动和停止
@Override public void onStart() { FrameLessXxlJobConfig.getInstance().initXxlJobExecutor(); super.onStart(); } @Override public void onStop() { FrameLessXxlJobConfig.getInstance().destroyXxlJobExecutor(); super.onStop(); }
6.在调度中心中添加定时任务
将上面的配置后,执行器项目在启动后会注册到调度中心,但是定时任务仍然需要手动添加,添加方法参加链接
https://www.xuxueli.com/xxl-job/#%E6%AD%A5%E9%AA%A4%E4%BA%8C%EF%BC%9A%E8%B0%83%E5%BA%A6%E4%B8%AD%E5%BF%83%EF%BC%8C%E6%96%B0%E5%BB%BA%E8%B0%83%E5%BA%A6%E4%BB%BB%E5%8A%A1