前段时间项目上需要用到任务调度,对比了Quartz的不足与XXL-JOB优缺点,整体上:XXL-JOB比Quartz更轻量化,更易上手,还具有可视化界面以及在任务调度、日志管理、弹性扩容缩容、执行器HA(分布式)、邮件报警、运行报表、全异步、跨平台、国际化、容器化、线程池隔离等方面更加人性化。
一、源码下载地址:
下载xxl-job源码
xxl-job源码地址:https://github.com/xuxueli/xxl-job
只需要将数据库配置成自己的,然后生成jar包即可,XXL-JOB的管理器直接通过运行jar包就正常运行;例如:
启动后效果图:
运行效果如下:
主页面:
执行器:
调度日志:
用户名密码在用户(xxl_job_user)表中;
二、数据库表创建:
基本支撑的表包括:
有需要建表语句的请关注公众号后留言即可;
三、集成到项目:
需要本地工程修改的代码类以及配置文件:XxlJobConfig.java、SampleXxlJob.java、application.properties、logback.xml
核心代码如下:
XxlJobConfig.java(直接copy到工程即可)
package www.tonghao.com.xxl.job.executor.core.config; import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Create by YSOcean */ @Configuration public class XxlJobConfig { private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class); @Value("${xxl.job.admin.addresses}") private String adminAddresses; @Value("${xxl.job.executor.appname}") private String appName; @Value("${xxl.job.executor.ip}") private String ip; @Value("${xxl.job.executor.port}") private int port; @Value("${xxl.job.accessToken}") private String accessToken; @Value("${xxl.job.executor.logpath}") private String logPath; @Value("${xxl.job.executor.logretentiondays}") private int logRetentionDays; // @Bean(initMethod = "start", destroyMethod = "destroy") @Bean public XxlJobSpringExecutor xxlJobExecutor() { logger.info(">>>>>>>>>>> xxl-job config init."); XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor(); xxlJobSpringExecutor.setAdminAddresses(adminAddresses); // xxlJobSpringExecutor.setAddress(address); xxlJobSpringExecutor.setAppname(appName); xxlJobSpringExecutor.setIp(ip); xxlJobSpringExecutor.setPort(port); xxlJobSpringExecutor.setAccessToken(accessToken); xxlJobSpringExecutor.setLogPath(logPath); xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays); return xxlJobSpringExecutor; } } |
SampleXxlJob.java(调度执行类)
package www.tonghao.com.xxl.job.executor.service.jobhandler; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.log.XxlJobLogger; import org.springframework.stereotype.Component; import www.tonghao.common.utils.SpringUtil; import www.tonghao.wc.service.WcinOrdersService; import www.tonghao.wc.web.WcinOrderApiController; /** * 任务Handler示例(Bean模式) * * 开发步骤: * 1、继承"IJobHandler":“com.xxl.job.core.handler.IJobHandler”; * 2、注册到Spring容器:添加“@Component”注解,被Spring容器扫描为Bean实例; * 3、注册到执行器工厂:添加“@JobHandler(value="自定义jobhandler名称")”注解,注解value值对应的是调度中心新建任务的JobHandler属性的值。 * 4、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志; */ //@JobHandler(value="demoJobHandler") @Component public class SampleXxlJob extends IJobHandler{ @XxlJob("demoJobHandler") @Override public ReturnT execute(String s) throws Exception { XxlJobLogger.log("XXL-JOB, Hello World."); System.out.println("This is example!"); //更换即可--start WcinOrderApiController contr=new WcinOrderApiController(); String msg=contr.getOrder(); //更换即可--end return SUCCESS; } } |
logback.xml(直接copy到工程即可)
logback %d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n ${log.path} ${log.path}.%d{yyyy-MM-dd}.zip %date %level [%thread] %logger{36} [%file : %line] %msg%n |
application.properties(核心配置)
# no web #spring.main.web-environment=false # log config logging.config=classpath:logback.xml ### xxl-job admin address list, such as "http://address" or "http://address01,http://address02" xxl.job.admin.addresses=http://127.0.0.1:6081/xxl-job-admin #xxl.job.admin.addresses=http://11.0.38.229:8080/xxl-job-admin ### xxl-job, access token xxl.job.accessToken= ### xxl-job executor appname xxl.job.executor.appname=testgmm ### 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.ip=127.0.0.1 xxl.job.executor.port=9999 ### 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 |
至此,XXL-JOB集成到项目,完成;
使用操作的详细配置,后续会更新,请持续关注!!!