hoge66的专栏

-----------------------看到专栏,偷着笑!
随笔 - 158, 文章 - 26, 评论 - 5, 阅读 - 18万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

Jfinal中定时器的初步探索(一)

Posted on   hehoge  阅读(1003)  评论(0编辑  收藏  举报

1、添加包引用:/jfinal_demo/WebContent/WEB-INF/lib/quartz-all-1.6.1.jar

注意版本号,这个版本是现在项目中使用的,已经有更高版本了,但这版比较稳定。

2、添加Job的实现类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package cn.jfinal.job;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
 
public class RedPacketValidate implements Job{
    private static final Logger log = Logger.getLogger(RedPacketValidate.class);
     
 
    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        // TODO Auto-generated method stub
        validate();
         
    }
 
    private void validate() {
        // TODO Auto-generated method stub
        SimpleDateFormat FORMAT = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
        String msgString="当前时间:"+FORMAT.format(new Date());
        log.info("红包定时器开始扫描……"+msgString);
         System.out.println(msgString);
    }
 
}

  

三、添加调度类:

有了干活的,得通知他什么时候干活,怎么干。

SimpleScheduler  简单调度,以后会有复杂调度类,敬请期待。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cn.jfinal.job;
 
import java.util.Date;
 
import org.apache.log4j.Logger;
import org.junit.Test;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;
 
 
public class SimpleScheduler {
     
    private static final Logger log = Logger.getLogger(SimpleScheduler.class);  
     
    @Test
    public  void testScheduler()
    {      
         SimpleScheduler simple = new SimpleScheduler();      
         try
         {      
             // Create a Scheduler and schedule the Job      
             Scheduler scheduler = simple.createScheduler();      
             simple.scheduleJob(scheduler);      
     
             // Start the Scheduler running      
             scheduler.start();      
     
             log.info( "Scheduler started at " + new Date());      
     
        } catch (SchedulerException ex) {      
            log.error(ex);      
        }  
         
    }
     
    public Scheduler createScheduler() throws SchedulerException
    {   //创建调度器      
        return StdSchedulerFactory.getDefaultScheduler();
    }
     
     //Create and Schedule a RedPacketValidate with the Scheduler      
 
    public void scheduleJob(Scheduler scheduler) throws SchedulerException
    {      
 
         // Create a JobDetail for the Job      
         JobDetail jobDetail = new JobDetail("RedPacketValidate",Scheduler.DEFAULT_GROUP,RedPacketValidate.class);
 
         // Configure the directory to scan      
         //jobDetail.getJobDataMap().put("SCAN_DIR","D:\\Tomcat\\conf"); //set the JobDataMap that is associated with the Job.           
 
         // Create a trigger that fires every 10 seconds, forever      
         Trigger trigger = TriggerUtils.makeSecondlyTrigger(10);//每10秒触发一次      
 
         trigger.setName("scanTrigger");      
 
         // Start the trigger firing from now      
         trigger.setStartTime(new Date());//设置第一次触发时间           
 
         // Associate the trigger with the job in the scheduler      
         scheduler.scheduleJob(jobDetail, trigger);      
    }      
 
 
}

  

四、在web中使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
SimpleScheduler simple = new SimpleScheduler();
        try {
            // Create a Scheduler and schedule the Job
            Scheduler scheduler = simple.createScheduler();
            simple.scheduleJob(scheduler);
 
            // Start the Scheduler running
            scheduler.start();
 
            log.info("Scheduler started at " + new Date());
 
        } catch (SchedulerException ex) {
            log.error(ex);
        }

  

五、注意引用job用到的jar包:

点击右上角即可分享
微信分享提示