当前位置: 首页 > 工具软件 > Jfinal-Quartz > 使用案例 >

Quartz[时间任务]之Jfinal

闽康安
2023-12-01

1.导入jar包:

下载地址:http://grepcode.com/snapshot/repo1.maven.org/maven2/org.quartz-scheduler/quartz/1.7.3

2.在服务器启动的所加载的文件配置你的Quartz

Config.java的configPlugin()方法中

me.add(new QuartzPlugin());你的Quartz的类

3.在job.properties文件中配置任务的参数

crawler.job=com.coo.crawlerJob.java#######你的所需要执行的任务类:

crawler.cron=0 20 16 * * ?#########时间参数,表示时间16:20执行你的任务

crawler.enable=true

4.QuartzPlugin.java

package com.fosbd.plugins;


import com.fosbd.kit.ReflectKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.IPlugin;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

/**
 * modify by Jfinal-ext
 */

public class QuartzPlugin implements IPlugin{

    private static final String JOB = "job";

    private final Logger logger = Logger.getLogger(getClass());

    private SchedulerFactory sf;
    private Scheduler sched;
    private String config = "job.properties";
    private Properties properties;

    public QuartzPlugin(String config) {
        this.config = config;
    }

    public QuartzPlugin() {
    }


    @Override
    public boolean start() {
        sf = new StdSchedulerFactory();
        try {
            sched = sf.getScheduler();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

        loadProperties();
        Enumeration<Object> enums = properties.keys();

        while (enums.hasMoreElements()) {
            String key = enums.nextElement() + "";
            if (!key.endsWith(JOB) || !isEnableJob(enable(key))) {
                continue;
            }
            String jobClassName = properties.get(key) + "";
            String jobCronExp = properties.getProperty(cronKey(key)) + "";
            Class<Job> clazz = ReflectKit.on(jobClassName).get();
            JobDetail job = JobBuilder.newJob(clazz)
                    .withIdentity(jobClassName,jobClassName)
                    .build();
            CronTrigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity(jobClassName,jobClassName)
                    .withSchedule(CronScheduleBuilder.cronSchedule(jobCronExp))
                    .build();
            java.util.Date ft = null;
            try {
                ft = sched.scheduleJob(job, trigger);
                sched.start();
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
            logger.debug(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "
                    + trigger.getCronExpression());
        }
        return true;
    }

    @Override
    public boolean stop() {
        try {
            sched.shutdown();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return true;
    }

    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";
    }

    private boolean isEnableJob(String enableKey) {
        Object enable = properties.get(enableKey);
        if (enable != null && "false".equalsIgnoreCase((enable + "").trim())) {
            return false;
        }
        return true;
    }


    private void loadProperties() {
        properties = new Properties();
        InputStream is = QuartzPlugin.class.getClassLoader().getResourceAsStream(config);
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        logger.debug("------------load Propteries---------------");
        logger.debug(properties.toString());
        logger.debug("------------------------------------------");
    }
}

5.crawlerJob.java

package com.fosbd.job;

import com.fosbd.model.InfoEn;
import com.fosbd.util.JsoupUtil;
import com.jfinal.log.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.List;

/**
 * Created by pc on 2015/4/22.
 */
public class crawlerJob implements Job {
    private static Logger logger = Logger.getLogger(crawlerJob.class);
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

           //编写你要定时的任务代码
    }
}


 类似资料: