opt:调度操作判断当前IP是否是调度Ip
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.nl.system.service.quartz.utils;
|
||||
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.modules.common.exception.BadRequestException;
|
||||
import org.nl.system.service.quartz.dao.SysQuartzJob;
|
||||
@@ -26,6 +27,14 @@ public class QuartzManage {
|
||||
private Scheduler scheduler;
|
||||
|
||||
public void addJob(SysQuartzJob quartzJob) {
|
||||
|
||||
|
||||
//本机IP
|
||||
String localIp = NetUtil.getLocalhostStr();
|
||||
System.out.println("本机ip:"+localIp);
|
||||
if (!judgmentIp(quartzJob.getJob_ip())) {
|
||||
throw new BadRequestException("本机ip与"+localIp+"调度ip"+quartzJob.getJob_ip()+"不同");
|
||||
}
|
||||
try {
|
||||
// 构建job信息
|
||||
JobDetail jobDetail = JobBuilder.newJob(ExecutionJob.class).
|
||||
@@ -62,8 +71,16 @@ public class QuartzManage {
|
||||
* @param quartzJob /
|
||||
*/
|
||||
public void updateJobCron(SysQuartzJob quartzJob) {
|
||||
|
||||
//本机IP
|
||||
String localIp = NetUtil.getLocalhostStr();
|
||||
System.out.println("本机ip:"+localIp);
|
||||
TriggerKey triggerKey = TriggerKey.triggerKey(JOB_NAME + quartzJob.getJob_id());
|
||||
|
||||
if (!judgmentIp(quartzJob.getJob_ip())) {
|
||||
throw new BadRequestException("本机ip与"+localIp+"调度ip"+quartzJob.getJob_ip()+"不同");
|
||||
}
|
||||
try {
|
||||
TriggerKey triggerKey = TriggerKey.triggerKey(JOB_NAME + quartzJob.getJob_id());
|
||||
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
|
||||
// 如果不存在则创建一个定时任务
|
||||
if (trigger == null) {
|
||||
@@ -93,8 +110,16 @@ public class QuartzManage {
|
||||
* @param quartzJob /
|
||||
*/
|
||||
public void resumeJob(SysQuartzJob quartzJob) {
|
||||
|
||||
//本机IP
|
||||
String localIp = NetUtil.getLocalhostStr();
|
||||
System.out.println("本机ip:"+localIp);
|
||||
TriggerKey triggerKey = TriggerKey.triggerKey(JOB_NAME + quartzJob.getJob_id());
|
||||
|
||||
if (!judgmentIp(quartzJob.getJob_ip())) {
|
||||
throw new BadRequestException("本机ip与"+localIp+"调度ip"+quartzJob.getJob_ip()+"不同");
|
||||
}
|
||||
try {
|
||||
TriggerKey triggerKey = TriggerKey.triggerKey(JOB_NAME + quartzJob.getJob_id());
|
||||
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
|
||||
// 如果不存在则创建一个定时任务
|
||||
if (trigger == null) {
|
||||
@@ -129,9 +154,21 @@ public class QuartzManage {
|
||||
* @param quartzJob /
|
||||
*/
|
||||
public void runJobNow(SysQuartzJob quartzJob) {
|
||||
|
||||
//本机IP
|
||||
String localIp = NetUtil.getLocalhostStr();
|
||||
System.out.println("本机ip:"+localIp);
|
||||
TriggerKey triggerKey = TriggerKey.triggerKey(JOB_NAME + quartzJob.getJob_id());
|
||||
|
||||
if (!judgmentIp(quartzJob.getJob_ip())) {
|
||||
throw new BadRequestException("本机ip与"+localIp+"调度ip"+quartzJob.getJob_ip()+"不同");
|
||||
}
|
||||
|
||||
try {
|
||||
TriggerKey triggerKey = TriggerKey.triggerKey(JOB_NAME + quartzJob.getJob_id());
|
||||
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
|
||||
log.info("定时任务:{}, 执行ip: {}, 定时任务开启", quartzJob.getJob_name(), localIp);
|
||||
System.out.println("定时任务: " + quartzJob.getJob_name() + ", 执行ip: " + localIp + ", 定时任务开启");
|
||||
|
||||
// 如果不存在则创建一个定时任务
|
||||
if (trigger == null) {
|
||||
addJob(quartzJob);
|
||||
@@ -140,6 +177,7 @@ public class QuartzManage {
|
||||
dataMap.put(SysQuartzJob.JOB_KEY, quartzJob);
|
||||
JobKey jobKey = JobKey.jobKey(JOB_NAME + quartzJob.getJob_id());
|
||||
scheduler.triggerJob(jobKey, dataMap);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("定时任务执行失败", e);
|
||||
throw new BadRequestException("定时任务执行失败");
|
||||
@@ -161,4 +199,9 @@ public class QuartzManage {
|
||||
throw new BadRequestException("删除定时任务失败");
|
||||
}
|
||||
}
|
||||
|
||||
public static Boolean judgmentIp(String ip) {
|
||||
return NetUtil.localAddressList(null).stream()
|
||||
.anyMatch(inet -> ip.equals(inet.getHostAddress()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user