理由
いくつかの仕事は最近、春のブートの実装の形で提供ノートを使用して、会社に書いています。
> git-を提出>ジェンキンスパックの再起動-セルフテスト、および時間を測定したテストはcronの式を修正する必要性に言及するとき、これは非常に面倒です
ソリューション
情報へのアクセスは、文書が明確で公式のブート例スプリング、コールベースのリモートRPCを使用して容易であるため、タスクのスケジューリング、選択XXLジョブのための多くの優れたプラットフォームがあり、タスクスケジューリングプラットフォームを使用することにしました。
配備します
最新バージョンをダウンロードするためにここにドッキングウィンドウを使用してください2.0.2ミラーです
docker pull xuxueli/xxl-job-admin:2.0.2
次に、パラメータを変更するドッキングウィンドウミラーの注目を実行します
docker run -e PARAMS="--spring.datasource.url=jdbc:mysql://数据库地址:3306/xxl-job?Unicode=true&characterEncoding=UTF-8 --spring.datasource.password=数据库密码 --spring.mail.host=smtp.163.com --spring.mail.username=邮箱名 --spring.mail.password=邮箱密码 --xxl.job.login.password=登录密码" -p 8080:8080 -v /tmp:/data/applogs --name xxl-job-admin --privileged=true -d xuxueli/xxl-job-admin:2.0.2
なメールボックスなど、いくつかのパラメータを省略することができることに注意してください
プロジェクト内のコンフィギュレーション
公式の春のブート構成を使用して、ここではネットワーク構成例
@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")
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;
}
}
xxl.job.admin.addresses
アドレスは次のように展開されているhttp://192.168.6.166:8080/xxl-job-admin
xxl.job.executor.appname
簡単にアクチュエータを再生することができます名前xxl.job.executor.port
コールのRPCポート番号はxxl.job.executor.logpath
、ログ出力ディレクトリを実行することでxxl.job.executor.logretentiondays
ログを保存します
アクチュエータ
あなたは、元のプロジェクトにアクチュエータを書くためにタスクを使用することができます公式は、アクチュエータの多くを与えるが、あなた自身を記述する必要がアクチュエータを改革するために、当然のことながら、このようなアクチュエータは、維持するために非常に困難であろう。反射的ので、ここで
最初の完全なクラス名反射モードのアクチュエータを使用して
@Component
@JobHandler(value = "BeanByClassHandler")
public class BeanByClassHandler extends IJobHandler {
@Autowired
private ApplicationContext applicationContext;
//根据完整类名 通过反射执行指定方法
@Override
public ReturnT<String> execute(String param) throws Exception {
XxlJobLogger.log(param);
if (param == null || param.equals("")) {
return new ReturnT<>(ReturnT.FAIL_CODE, "参数不能为空!");
}
String[] split = param.split(",");
if (split == null || split.length < 2) {
return new ReturnT<>(ReturnT.FAIL_CODE, "参数格式错误,应为 完整类名,方法名");
}
Class taskBeanClass = null;
try {
taskBeanClass = Class.forName(split[0]);
} catch (Exception e) {
return new ReturnT<>(ReturnT.FAIL_CODE, "类" + split[0] + "不存在");
}
Method method = null;
try {
method = taskBeanClass.getMethod(split[1]);
} catch (Exception e) {
return new ReturnT<>(ReturnT.FAIL_CODE, "方法" + split[1] + "不存在");
}
Object o = applicationContext.getBean(taskBeanClass);
if (o == null) {
return new ReturnT<>(ReturnT.FAIL_CODE, "在Application中类不存在");
}
try {
method.invoke(o);
} catch (Exception e) {
return new ReturnT<>(ReturnT.FAIL_CODE, "方法执行失败");
}
return new ReturnT<>(ReturnT.SUCCESS_CODE, "执行成功");
}
}
このような長い完全なクラス名は、スプリングを使用して得ることができるインスタンスの反射を管理するのbeanName
@Component
@JobHandler(value = "BeanByNameHandler")
public class BeanByNameHandler extends IJobHandler {
@Autowired
private ApplicationContext applicationContext;
//根据spring管理的bean name获取指定类
@Override
public ReturnT<String> execute(String param) throws Exception {
XxlJobLogger.log(param);
if (param == null || param.equals("")) {
return new ReturnT<>(ReturnT.FAIL_CODE, "参数不能为空!");
}
String[] split = param.split(",");
if (split == null || split.length < 2) {
return new ReturnT<>(ReturnT.FAIL_CODE, "参数格式错误,应为bean名称,方法名");
}
Object o = applicationContext.getBean(split[0]);
if(o == null){
return new ReturnT<>(ReturnT.FAIL_CODE,"类在applicationContext中不存在");
}
Method method;
try {
method = o.getClass().getMethod(split[1]);
}catch (Exception e){
return new ReturnT<>(ReturnT.FAIL_CODE,"方法"+split[1]+"不存在");
}
try {
method.invoke(o);
}catch (Exception e){
return new ReturnT<>(ReturnT.FAIL_CODE,"调用方法失败");
}
return new ReturnT<>(ReturnT.SUCCESS_CODE,"调用job成功");
}
}
# 调用
在web界面新建任务 参数为 全类名,方法名即可。
# 缺点
xxl-job只支持mysql数据库,公司使用的Oracle 所以docker需要运行一个mysql数据库