1. xxl-job 概述
官方網址:鏈接
XXL-JOB是一個輕量級分佈式任務調度平臺,其核心設計目標是開發迅速、學習簡單、輕量級、易擴展。現已開放源代碼並接入多家公司線上產品線,開箱即用。
2. 代碼示例
下載 demo 。
放置配置:
修改配置,主要修改 數據庫配置。
### 1、調度中心項目的端口號以及訪問路徑
server.port=8080
server.context-path=/xxl-job-admin
### 2、配置靜態文件的前綴
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static/
### 3、配置模板文件
spring.freemarker.templateLoaderPath=classpath:/templates/
spring.freemarker.suffix=.ftl
spring.freemarker.charset=UTF-8
spring.freemarker.request-context-attribute=request
spring.freemarker.settings.number_format=0.##########
### 4、配置mybatis的mapper文件地址
mybatis.mapper-locations=classpath:/mybatis-mapper/*Mapper.xml
### 5、配置數據庫的地址
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl-job?Unicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=syrdbt
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.max-active=30
spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.validation-query=SELECT 1
spring.datasource.tomcat.validation-interval=30000
### 6、配置報警郵箱
spring.mail.host=smtp.qq.com
spring.mail.port=25
[email protected]
spring.mail.password=xxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
### 7、管理界面的登錄用戶名密碼
xxl.job.login.username=admin
xxl.job.login.password=123456
### 8、調度中心通訊TOKEN,非空時啓用
xxl.job.accessToken=
### 9、調度中心國際化設置,默認爲中文版本,值設置爲“en”時切換爲英文版本
xxl.job.i18n=
新建 jobHandler ,HelloJobHandler:
job Handler 的源碼如下所示:
package com.xxl.job.executor.service.jobhandler;
import org.springframework.stereotype.Component;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import java.util.Date;
/**
* 任務Handler示例(Bean模式)
* <p>
* 開發步驟:
* 1、繼承"IJobHandler":“com.xxl.job.core.handler.IJobHandler”;
* 2、註冊到Spring容器:添加“@Component”註解,被Spring容器掃描爲Bean實例;
* 3、註冊到執行器工廠:添加“@JobHandler(value="自定義jobhandler名稱")”註解,註解value值對應的是調度中心新建任務的JobHandler屬性的值。
* 4、執行日誌:需要通過 "XxlJobLogger.log" 打印執行日誌;
*/
@JobHandler(value = "helloJobHandler")
@Component
public class HelloJobHandler extends IJobHandler {
@Override
public ReturnT<String> execute(String param) {
String now = new Date().toString();
System.out.println(now + "XXL-JOB, Hello World.");
return SUCCESS;
}
}
登陸:http://localhost:8080/xxl-job-admin/jobinfo
賬號:admin, 密碼:123456 。