项目引入了秒级别的定时任务后,发现在此任务中出现的任何运行时异常,控制台都不会打印出来,打断点可以跳入异常源码中,但控制台没打印
把同样的代码,放到接口中调用正常打印异常信息
暂时还没有比较好的排查思路,有没有之前遇到过此类问题朋友
下面是配置代码(应该是很早之前在论坛copy的):
maven
<dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal-ext</artifactId> <version>3.1.2</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency>
QuartzPlugin.java
import com.google.common.collect.Maps; import com.jfinal.ext.kit.Reflect; import com.jfinal.ext.kit.ResourceKit; import com.jfinal.kit.StrKit; import com.jfinal.plugin.IPlugin; import java.util.*; import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; public class QuartzPlugin implements IPlugin { public static final String VERSION_1 = "1"; private static final String JOB = "job"; private Map<Job, String> jobs = Maps.newLinkedHashMap(); private String version; private SchedulerFactory sf; private Scheduler scheduler; private String jobConfig; private String confConfig; private Map<String, String> jobProp; public QuartzPlugin(String jobConfig, String confConfig) { this.jobConfig = jobConfig; this.confConfig = confConfig; } public QuartzPlugin(String jobConfig) { this.jobConfig = jobConfig; } public QuartzPlugin() { } public QuartzPlugin add(String jobCronExp, Job job) { jobs.put(job, jobCronExp); return this; } @Override public boolean start() { loadJobsFromProperties(); startJobs(); return true; } private void startJobs() { try { if (StrKit.notBlank(confConfig)) { sf = new StdSchedulerFactory(confConfig); } else { sf = new StdSchedulerFactory(); } scheduler = sf.getScheduler(); } catch (SchedulerException e) { e.printStackTrace(); } Set<Map.Entry<Job, String>> set = jobs.entrySet(); for (Map.Entry<Job, String> entry : set) { Job job = entry.getKey(); String jobClassName = job.getClass().getName(); String jobCronExp = entry.getValue(); JobDetail jobDetail; CronTrigger trigger; //JobDetail and CornTrigger are classes in 1.x version,but are interfaces in 2.X version. if (VERSION_1.equals(version)) { jobDetail = Reflect.on("org.quartz.JobDetail").create(jobClassName, jobClassName, job.getClass()).get(); trigger = Reflect.on("org.quartz.CronTrigger").create(jobClassName, jobClassName, jobCronExp).get(); } else { jobDetail = Reflect.on("org.quartz.JobBuilder").call("newJob", job.getClass()).call("withIdentity", jobClassName, jobClassName) .call("build").get(); Object temp = Reflect.on("org.quartz.TriggerBuilder").call("newTrigger").get(); temp = Reflect.on(temp).call("withIdentity", jobClassName, jobClassName).get(); CronScheduleBuilder csb = Reflect.on("org.quartz.CronScheduleBuilder").call("cronSchedule", jobCronExp).get(); temp = Reflect.on(temp).call("withSchedule", csb).get(); trigger = Reflect.on(temp).call("build").get(); } Date ft = Reflect.on(scheduler).call("scheduleJob", jobDetail, trigger).get(); } try { scheduler.start(); } catch (SchedulerException e) { e.printStackTrace(); } } private void loadJobsFromProperties() { if (StrKit.isBlank(jobConfig)) { return; } jobProp = ResourceKit.readProperties(jobConfig); Set<Map.Entry<String, String>> entries = jobProp.entrySet(); for (Map.Entry<String, String> entry : entries) { String key = entry.getKey(); if (!key.endsWith(JOB) || !isEnableJob(enable(key))) { continue; } String jobClassName = jobProp.get(key) + ""; String jobCronExp = jobProp.get(cronKey(key)) + ""; Class<Job> job = Reflect.on(jobClassName).get(); try { jobs.put(job.newInstance(), jobCronExp); } catch (Exception e) { e.printStackTrace(); } } } private String enable(String key) { return key.substring(0, key.lastIndexOf(JOB)) + "enable"; } private String cronKey(String key) { return key.substring(0, key.lastIndexOf(JOB)) + "cron"; } public QuartzPlugin version(String version) { this.version = version; return this; } private boolean isEnableJob(String enableKey) { Object enable = jobProp.get(enableKey); if (enable != null && "false".equalsIgnoreCase((enable + "").trim())) { return false; } return true; } @Override public boolean stop() { try { scheduler.shutdown(); } catch (SchedulerException e) { e.printStackTrace(); } return true; } public QuartzPlugin confConfig(String confConfig) { this.confConfig = confConfig; return this; } public QuartzPlugin jobConfig(String jobConfig) { this.jobConfig = jobConfig; return this; } }
Task.java
public class Task implements Job{ @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { } }
https://jfinal.com/share/15
https://jfinal.com/share/37
https://jfinal.com/feedback/3527
其实直接在 jfinal 中引入 quartz 即可,不一定要使用 plugin 整合
在 JFinalConfig 继承类的配置中初始化一下它