spring 定时器的方法参数是否能动态修改
答案:2 悬赏:40 手机版
解决时间 2021-04-10 16:18
- 提问者网友:孤凫
- 2021-04-10 04:34
spring 定时器的方法参数是否能动态修改
最佳答案
- 五星知识达人网友:思契十里
- 2021-04-10 04:56
很多时候,spring的quartz定时任务并不能完全满足项目的需要,经常会遇到需要任务的执行时间和频率是可控的,而不是写死在 quartz配置xml上面。为此,思考了很多动态修改任务执行的方法,以下方法是通过获取scheduler,对trigger的时间表达式进行修改,并重启任务去实现任务的动态变化。
[java] view plain copy
public class QuartzHandler {
public static final String DEFAULT_GROUP_NAME = "DEFAULT";
private static QuartzHandler instance = new QuartzHandler();
private static Logger logger = Logger.getLogger(QuartzHandler.class);
private QuartzManager quartzManager;
private Scheduler scheduler;
private ApplicationContext context;
private QuartzHandler() {
}
public static QuartzHandler genInstance() {
return instance;
}
public void start() {
context = new ClassPathXmlApplicationContext(
"spring/applicationContext.xml");
scheduler = (StdScheduler) context.getBean("schedulerFactory");
quartzManager = (QuartzManager) context.getBean("quartzManager");
}
public boolean updateCronExpression(String triggerName, String groupName,
String cronExpression) {
try {
CronTrigger trigger = (CronTrigger) getTrigger(triggerName,
groupName);
if (trigger == null) {
return false;
}
if (StringUtils.equals(trigger.getCronExpression(), cronExpression)) {
logger.info("cronExpression is same with the running Schedule , no need to update.");
return true;
}
trigger.setCronExpression(cronExpression);
scheduler.rescheduleJob(trigger.getName(), trigger.getGroup(),
trigger);
logger.info("Update the cronExpression successfully!!");
return true;
} catch (ParseException e) {
logger.error("The new cronExpression - " + cronExpression
+ " not conform to the standard. " + e);
return false;
} catch (SchedulerException e) {
logger.error("Fail to reschedule. " + e);
return false;
}
}
public Trigger getTrigger(String triggerName, String groupName) {
Trigger trigger = null;
if (StringUtils.isBlank(groupName)) {
logger.warn("Schedule Job Group is empty!");
return null;
}
if (StringUtils.isBlank(triggerName)) {
logger.warn("Schedule trigger Name is empty!");
return null;
}
try {
trigger = scheduler.getTrigger(triggerName, groupName);
} catch (SchedulerException e) {
logger.warn("Fail to get the trigger (triggerName: " + triggerName
+ ", groupName : " + groupName + ")");
return null;
}
if (trigger == null) {
logger.warn("Can not found the trigger of triggerName: "
+ triggerName + ", groupName : " + groupName);
}
return trigger;
}
}
[java] view plain copy
public class QuartzHandler {
public static final String DEFAULT_GROUP_NAME = "DEFAULT";
private static QuartzHandler instance = new QuartzHandler();
private static Logger logger = Logger.getLogger(QuartzHandler.class);
private QuartzManager quartzManager;
private Scheduler scheduler;
private ApplicationContext context;
private QuartzHandler() {
}
public static QuartzHandler genInstance() {
return instance;
}
public void start() {
context = new ClassPathXmlApplicationContext(
"spring/applicationContext.xml");
scheduler = (StdScheduler) context.getBean("schedulerFactory");
quartzManager = (QuartzManager) context.getBean("quartzManager");
}
public boolean updateCronExpression(String triggerName, String groupName,
String cronExpression) {
try {
CronTrigger trigger = (CronTrigger) getTrigger(triggerName,
groupName);
if (trigger == null) {
return false;
}
if (StringUtils.equals(trigger.getCronExpression(), cronExpression)) {
logger.info("cronExpression is same with the running Schedule , no need to update.");
return true;
}
trigger.setCronExpression(cronExpression);
scheduler.rescheduleJob(trigger.getName(), trigger.getGroup(),
trigger);
logger.info("Update the cronExpression successfully!!");
return true;
} catch (ParseException e) {
logger.error("The new cronExpression - " + cronExpression
+ " not conform to the standard. " + e);
return false;
} catch (SchedulerException e) {
logger.error("Fail to reschedule. " + e);
return false;
}
}
public Trigger getTrigger(String triggerName, String groupName) {
Trigger trigger = null;
if (StringUtils.isBlank(groupName)) {
logger.warn("Schedule Job Group is empty!");
return null;
}
if (StringUtils.isBlank(triggerName)) {
logger.warn("Schedule trigger Name is empty!");
return null;
}
try {
trigger = scheduler.getTrigger(triggerName, groupName);
} catch (SchedulerException e) {
logger.warn("Fail to get the trigger (triggerName: " + triggerName
+ ", groupName : " + groupName + ")");
return null;
}
if (trigger == null) {
logger.warn("Can not found the trigger of triggerName: "
+ triggerName + ", groupName : " + groupName);
}
return trigger;
}
}
全部回答
- 1楼网友:一袍清酒付
- 2021-04-10 06:01
你可以自己写个类继承methodinvokingjobdetailfactorybean,类里面写个你的参数对象比如private string arg及一个方法(比如public void mymethod(string arg))接收你的参数并初始化arg,在配置的时候就配置为你自己的类和方法。然后按照你的需求去重新实现work方法(在里面调用getarg就达到你的需求)。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯