前言:學(xué)習(xí)xxl-job需要有g(shù)it,springboot的基礎(chǔ),學(xué)起來就很簡(jiǎn)單
xxl-job是一個(gè)分布式的任務(wù)調(diào)度平臺(tái),其核心設(shè)計(jì)目標(biāo)是:學(xué)習(xí)簡(jiǎn)單、開發(fā)迅速、輕量級(jí)、易擴(kuò)展,現(xiàn)在已經(jīng)開放源代碼并接入多家公司的線上產(chǎn)品線,開箱即用。xxl是xxl-job的開發(fā)者大眾點(diǎn)評(píng)的許雪里名稱的拼音開頭。
?源碼下載地址(放在下面第二個(gè)),我自己寫的demo也開源放在下面第一個(gè),
?springboot-xxl-job: 學(xué)習(xí)xxl-job定時(shí)任務(wù) (gitee.com)
xxl-job: 一個(gè)分布式任務(wù)調(diào)度平臺(tái),其核心設(shè)計(jì)目標(biāo)是開發(fā)迅速、學(xué)習(xí)簡(jiǎn)單、輕量級(jí)、易擴(kuò)展?,F(xiàn)已開放源代碼并接入多家公司線上產(chǎn)品線,開箱即用。 (gitee.com)
?然后把sql文件運(yùn)行一下(也就是所需要的數(shù)據(jù)庫(kù)),效果如下
?直接啟動(dòng)(原神!啟動(dòng).jpg),啟動(dòng)后輸入? ?http://localhost:8090/xxl-job-admin
效果如下,賬號(hào)admin密碼123456
登錄后效果如下
?
下面我們就來基于自己的操作xxl-job,新建一個(gè)springboot項(xiàng)目
?添加所需要的依賴
<!-- 使用xxl-job--> <dependency> <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core</artifactId> <version>2.3.1</version> </dependency>
新建job,config兩個(gè)文件夾
?XxljobConfig如下,SimpleXxlJob如下
@Configuration
public class XxlJobConfig {
? ? @Value("${xxl.job.admin.addresses}")
? ? private String adminAddresses;
? ? @Value("${xxl.job.accessToken}")
? ? private String accessToken;
? ? @Value("${xxl.job.executor.appname}")
? ? private String appname;
? ? @Value("${xxl.job.executor.address}")
? ? private String address;
? ? @Value("${xxl.job.executor.ip}")
? ? private String ip;
? ? @Value("${xxl.job.executor.port}")
? ? private int port;
? ? @Value("${xxl.job.executor.logpath}")
? ? private String logPath;
? ? @Value("${xxl.job.executor.logretentiondays}")
? ? private int logRetentionDays;
? ? @Bean
? ? public XxlJobSpringExecutor xxlJobExecutor() {
? ? ? ? XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
? ? ? ? xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
? ? ? ? xxlJobSpringExecutor.setAppname(appname);
? ? ? ? xxlJobSpringExecutor.setAddress(address);
? ? ? ? xxlJobSpringExecutor.setIp(ip);
? ? ? ? xxlJobSpringExecutor.setPort(port);
? ? ? ? xxlJobSpringExecutor.setAccessToken(accessToken);
? ? ? ? xxlJobSpringExecutor.setLogPath(logPath);
? ? ? ? xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
? ? ? ? return xxlJobSpringExecutor;
? ? }
}
?properties配置文件如下,端口記得改成自己的
### 調(diào)度中心部署根地址 [選填]:如調(diào)度中心集群部署存在多個(gè)地址則用逗號(hào)分隔。執(zhí)行器將會(huì)使用該地址進(jìn)行"執(zhí)行器心跳注冊(cè)"和"任務(wù)結(jié)果回調(diào)";為空則關(guān)閉自動(dòng)注冊(cè); xxl.job.admin.addresses=http://localhost:8090/xxl-job-admin ### 執(zhí)行器通訊TOKEN [選填]:非空時(shí)啟用; xxl.job.accessToken=default_token ### 執(zhí)行器AppName [選填]:執(zhí)行器心跳注冊(cè)分組依據(jù);為空則關(guān)閉自動(dòng)注冊(cè) xxl.job.executor.appname=xxl-job-executor-sample ### 執(zhí)行器注冊(cè) [選填]:優(yōu)先使用該配置作為注冊(cè)地址,為空時(shí)使用內(nèi)嵌服務(wù) ”IP:PORT“ 作為注冊(cè)地址。從而更靈活的支持容器類型執(zhí)行器動(dòng)態(tài)IP和動(dòng)態(tài)映射端口問題。 xxl.job.executor.address= ### 執(zhí)行器IP [選填]:默認(rèn)為空表示自動(dòng)獲取IP,多網(wǎng)卡時(shí)可手動(dòng)設(shè)置指定IP,該IP不會(huì)綁定Host僅作為通訊實(shí)用;地址信息用于 "執(zhí)行器注冊(cè)" 和 "調(diào)度中心請(qǐng)求并觸發(fā)任務(wù)"; xxl.job.executor.ip=127.0.0.1 ### 執(zhí)行器端口號(hào) [選填]:小于等于0則自動(dòng)獲??;默認(rèn)端口為9999,單機(jī)部署多個(gè)執(zhí)行器時(shí),注意要配置不同執(zhí)行器端口; xxl.job.executor.port=9999 ### 執(zhí)行器運(yùn)行日志文件存儲(chǔ)磁盤路徑 [選填] :需要對(duì)該路徑擁有讀寫權(quán)限;為空則使用默認(rèn)路徑; xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler ### 執(zhí)行器日志文件保存天數(shù) [選填] : 過期日志自動(dòng)清理, 限制值大于等于3時(shí)生效; 否則, 如-1, 關(guān)閉自動(dòng)清理功能; xxl.job.executor.logretentiondays=30
SimpleXxlJob類如下
@Component public class SimpleXxlJob { @XxlJob("demoJobHandler") public void demoJobHandler() throws Exception { System.out.println("執(zhí)行定時(shí)任務(wù),執(zhí)行時(shí)間:"+new Date()); } }
?我們直接啟動(dòng)我們的springboot項(xiàng)目,如果看到我們注冊(cè)上去的demoJobHandler,就表示成功了
下面我們來添加一個(gè)任務(wù)
點(diǎn)擊任務(wù)管理的新增,填寫我們所需要的東西
?
?設(shè)置好了如下,然后點(diǎn)擊保存
就會(huì)在idea里面看到,看到有日志就表示成功了
到這里就操作成功了,恭喜各位小伙伴
下面我們GLUE模式來操作
任務(wù)以源碼方式維護(hù)在調(diào)度中心,支持通過Web IDE在線更新,實(shí)時(shí)編譯和生效,因此不需要指定JobHandler。( “GLUE模式(Java)” 運(yùn)行模式的任務(wù)實(shí)際上是一段繼承自IJobHandler的Java類代碼,它在執(zhí)行器項(xiàng)目中運(yùn)行,可使用[@Resource][@Autowire]注入執(zhí)行器里中的其他服務(wù).
我們?cè)谌蝿?wù)管理新建一個(gè)GlUE模式的,
寫一個(gè)service文件夾,編寫HelloService類
@Service public class HelloService { public void methodA(){ System.out.println("執(zhí)行MethodA的方法"); } public void methodB(){ System.out.println("執(zhí)行MethodB的方法"); } }
在剛剛創(chuàng)建的任務(wù)管理,選擇GLUE IDE,?
?
效果如下,我們需要注冊(cè)我們的HelloService。
包也要記得引入
package com.xxl.job.service.handler;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.IJobHandler;
import cn.wolfcode.xxljobdemo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;public class DemoGlueJobHandler extends IJobHandler {
? ?@Autowired
? ? private HelloService helloService;
??
??
?? ?@Override
?? ?public void execute() throws Exception {
?? ??? ?XxlJobHelper.log("XXL-JOB, Hello World.");
?? ?}}
?
?
package?com.xxl.job.service.handler;
import?com.cskt.service.HelloService;
import?com.xxl.job.core.handler.IJobHandler;
import?org.springframework.beans.factory.annotation.Autowired;
public?class?DemoGlueJobHandler?extends?IJobHandler?{
????@Autowired
????private?HelloService?helloService;
????@Override
????public?void?execute()?throws?Exception?{
????????helloService.methodA();
????}
}
然后點(diǎn)擊保存
接下來在我們的任務(wù)管理執(zhí)行一次,看看輸出效果,輸出就表示成功文章來源:http://www.zghlxwxcb.cn/news/detail-684211.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-684211.html
到了這里,關(guān)于xxl-job學(xué)習(xí)(一篇文章解決)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!