我为了修改QuartzPlugin里面加载配置文件的逻辑,改为直接读取日期以方便配合我的项目逻辑。新建一个NewQuartPlugin复制并修改QuartPlugin代码如下(添加了loadProperties方法,修改start里面的配置文件方法为loadProperties方法):
@Override public boolean start() { loadProperties(); // 加载配置文件键改为这个方法 startJobs(); return true; } 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) { Throwables.propagate(e); } } } private void loadProperties() { String jobClassName = "jfinal.common.Last30MinJob"; // 获取cron表达式 DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); DateTime dateTime = DateTime.parse(GlobalVariables.END_TIME_STRING, dateTimeFormatter).minusMinutes(30); String jobCronExp = TimeUtil.formaterCronExpression(dateTime); Class<Job> job = Reflect.on(jobClassName).get(); logger.info("job " + jobClassName + " has scheduled based on cron " + jobCronExp); try { jobs.put(job.newInstance(), jobCronExp); } catch (Exception e) { Throwables.propagate(e); } }
然后报错:
[ERROR]-[Thread: RMI TCP Connection(3)-127.0.0.1]-[com.jfinal.core.Config.startPlugins()]: Plugin start error: jfinal.common.NewQuartzPlugin. org.quartz.CronScheduleBuilder cannot be cast to [Ljava.lang.Object; java.lang.ClassCastException: org.quartz.CronScheduleBuilder cannot be cast to [Ljava.lang.Object; at jfinal.common.NewQuartzPlugin.startJobs(NewQuartzPlugin.java:93) at jfinal.common.NewQuartzPlugin.start(NewQuartzPlugin.java:61) at com.jfinal.core.Config.startPlugins(Config.java:87) at com.jfinal.core.Config.configJFinal(Config.java:48) at com.jfinal.core.JFinal.init(JFinal.java:65) at com.jfinal.core.JFinalFilter.init(JFinalFilter.java:49) at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260) at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4590) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5233) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734) at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1702) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
我按照http://blog.csdn.net/sushengmiyan/article/details/52703435中的方法修改startJobs中的逻辑后报错:
[ERROR]-[Thread: RMI TCP Connection(3)-127.0.0.1]-[com.jfinal.core.Config.startPlugins()]: Plugin start error: jfinal.common.NewQuartzPlugin. java.lang.reflect.InvocationTargetException com.jfinal.ext.kit.ReflectException: java.lang.reflect.InvocationTargetException at com.jfinal.ext.kit.Reflect.on(Reflect.java:650) at com.jfinal.ext.kit.Reflect.call(Reflect.java:383) at jfinal.common.NewQuartzPlugin.startJobs(NewQuartzPlugin.java:100) at jfinal.common.NewQuartzPlugin.start(NewQuartzPlugin.java:62) at com.jfinal.core.Config.startPlugins(Config.java:87) at com.jfinal.core.Config.configJFinal(Config.java:48)
不知该怎么办了,其实我的要求很简单,就是在某一个时间点执行一次任务就行了,但是用这个QuartzPlugin必须要用到job.properties配置文件,导致在测试的时候很麻烦,经常要改动两个文件!请问大神怎么弄?
当我正常使用的时候却没有报错:
QuartzPlugin quartzPlugin = new QuartzPlugin("job.properties"); me.add(quartzPlugin);
job.properties文件:
a.job=jfinal.common.Last30MinJob # a.cron=03 00 16 23 9 ? 2017 a.cron=03 00 14 * * ? 2017 a.enable=true
项目:JFinal