JFinal timed task scheduling problem

Since the recent project is a non-Maven project to achieve scheduled task scheduling, I found that the jar of JFinal-ext encapsulates the call to the Quartz timer by searching on the Internet, but debugging according to the operation, it appears that the com.jfinal.log.Log class cannot be found. Error message, I later learned that ext has not been updated, but I am very grateful to @JFinal for his help, and gave me the solution by email. Here are the steps to solve the problem:

The following link is the jar package and Cron4jPlugin, ITask required in the project: http://pan.baidu.com/s/1eSLm9to

Proceed as follows:

  1. First, you need the jar package of the project, put cron4j-2.2.5.jar in the lib directory of the project

  2. Put the Cron4jPlugin and the interface ITask into the project, and I put it under the same package as the class that inherits from JFinalConfig

  3. The settings in the Cron4j configuration file are as follows, this file is named task.properties, and newCron=* * * * * in the file

    The expression executes every minute. code show as below:

  4.  
       
    1. cron4j=newCron, registerCron, foreignExchangeCron
    2. #newCron 
    3. newCron.cron=*****    
    4. newCron.class=cn.cyansoft.xine.task.NewTask
    5. newCron.enable=true
    6. #registerCron
    7. registerCron.cron=*****    
    8. registerCron.class=cn.cyansoft.xine.task.RegisterTask
    9. registerCron.enable=true
    10. #foreignExchange
    11. foreignExchangeCron.cron=*****    
    12. foreignExchangeCron.class=cn.cyansoft.xine.task.ForeignExchangeTask
    13. foreignExchangeCron.enable=true
  5. In the configPlugin inherited from the JFinalConfig class, load the configuration


  6.  
       
    1. publicvoid configPlugin(Plugins me){  
    2.  
    3. me.add(newCron4jPlugin(PropKit.use("task.properties"))); 
    4.  
    5. }
  7. Implement the ITask interface in the task class, and write timed tasks in run

    Let your Task implement ITask Jack, or implement the Runable interface.

    Suppose this MyTask is under the com.xxx package        

  8.  
       
    1. public class MyTask implements ITask {     
    2.    publicvoid run(){  
    3.       // put the executed debug task code here
    4.    }
    5.    
    6.    publicvoid stop(){  
    7.      // The code here will be called before the task is closed
    8.    }
    9. }
  9. 到以上为止能够实现定时调度任务,由于我的项目是Jfinal-weixin项目,定时给部分用户发送客服消息,出现如下错误

    http://www.jfinal.com/feedback/138,已反馈并已解决在此反馈中。

    将我解决的方案分享一下,希望能帮到像我一样遇到这样问题的伙伴们。

补充

一、关于定时器配置文件(task.properties)的参数解释:

 
 
  1. 以上配置中 cron4j 是入口,后面的 newCron 是任务名称,可以随便起名,这个例子中起的名字是 "newCron"
  2. 一旦起了这个名称,后面的配置就是以这个 "newCron" 打头,例如上面的 newCron.cronnewCron.class等等
  3. newCron.cron=.... 配置调试表达式,具体的含义在 Cron4jPlugin 源代码中有说明,切记:cron 表达式只有五个部分,
  4. taskName.class=... 配置任务的实现类,指向最前面的 MyTask 类,注意类的路径配置正确
  5. taskName.enable=... 配置该任务是否有效,如果为 false,则不会被处理,相当于一个开关变量,这个配置可以省略,省略时默认值为 true
  6. 再说明一点,假定 cron4j=abc 这么配置,那么后面的三个配置如下:
  7. abc.cron=0 * * * *
  8. abc.class=com.xxx.MyTask
  9. abc.enable=true
  10. 最后再被被一点,cron4j 可以同时配置多个任务,以逗号分隔,以下代码同时配置了两个任务
  11. cron4j=taskAaa, taskBbb
  12. taskAaa.cron=0 * * * *
  13. taskAaa.class=com.xxx.TaskOne
  14. taskAaa.enable=true
  15. taskBbb.cron=0 * * * *
  16. taskBbb.class=com.xxx.OtherTask
  17. taskBbb.enable=true

二、以下贴出 Cron4jPlugin 中的部分注释,说明了如何使用:

 
 
  1. * cron 表达式由五部分组成:分 * :从 0 59 * :从 0 23 * :从 1 31,字母 L 可以表示月的最后一天 * :从 1 12,可以别名:jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov" and "dec" * 周 :从 0 到 6,0 表示周日,6 表示周六,可以使用别名: "sun", "mon", "tue", "wed", "thu", "fri" and "sat" * * 数字 n:表示一个具体的时间点,例如 5 * * * * 表示 5 分这个时间点时执行 * 逗号 , :表示指定多个数值,例如 3,5 * * * * 表示 3 和 5 分这两个时间点执行 * 减号 -:表示范围,例如 1-3 * * * * 表示 1 分、2 分再到 3 分这三个时间点执行 * 星号 *:表示每一个时间点,例如 * * * * * 表示每分钟执行 * 除号 /:表示指定一个值的增加幅度。例如 n/m表示从 n 开始,每次增加 m 的时间点执行

三、若是maven项目用户:

 
 
  1. <dependency>
  2.  <groupId>it.sauronsoftware.cron4j</groupId>
  3.  <artifactId>cron4j</artifactId>
  4.  <version>2.2.5</version>
  5. </dependency>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325978405&siteId=291194637