JFinal框架_定时触发程序

Posted 风戏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JFinal框架_定时触发程序相关的知识,希望对你有一定的参考价值。

JFinal框架进行作业调度,使用JFinal-ext2与quartzf进行配置。

maven说明:

<dependency>
    <groupId>com.jfinal</groupId>
    <artifactId>jfinal</artifactId>
    <version>3.2</version>
</dependency>

<dependency>
    <groupId>com.jfinal</groupId>
    <artifactId>jfinal-ext2</artifactId>
    <version>2.0.2</version>
</dependency>

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.1.1</version>
</dependency>

Config.java配置(插件)

public void configPlugin(Plugins me) {
    QuartzPlugin quartzPlugin = new QuartzPlugin("job.properties");
    me.add(quartzPlugin);
}

job.properties配置(每天14:48:10 调度作业):

#JobA

a.job=com.fwpsl.util.Test1

a.cron=10 48 14 * * ?

a.enable=true

 Test1.java

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class Test1 implements Job {
   @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //调度作业内容
        System.out.println("任务1:"+CommonUtils.getDate());
    }
}

 



使用quartz插件进行作业调度
QuartzManager.java
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * Created by fwpsl on 2017/10/16.
 */
public class QuartzManager {
    public  static QuartzManager qm=new QuartzManager();
    private Logger log = LogManager.getLogger(QuartzManager.class);
    private Scheduler scheduler = null;

    private QuartzManager() {
        try {
            scheduler = new StdSchedulerFactory().getScheduler();
            log.info("初始化调度器 ");
        } catch (SchedulerException ex) {
            log.error("初始化调度器=> [失败]:" + ex.getLocalizedMessage());
        }
    }

    //添加任务
    public void addJob(String name, String group, Class<? extends Job> clazz, String cronExpression) {
        try {
            // 构造任务
            JobDetail job = newJob(clazz).withIdentity(name, group).build();
            // 构造任务触发器
            Trigger trg = newTrigger().withIdentity(name, group).withSchedule(cronSchedule(cronExpression)).build();
            // 将作业添加到调度器
            scheduler.scheduleJob(job, trg);
            log.info("创建作业=> [作业名称:" + name + " 作业组:" + group + "] ");
            System.out.println("创建作业=> [作业名称:" + name + " 作业组:" + group + "] ");
        } catch (SchedulerException e) {
            e.printStackTrace();
            log.error("创建作业=> [作业名称:" + name + " 作业组:" + group + "]=> [失败]");
        }
    }
    //移除任务
    public void removeJob(String name, String group) {
        try {
            TriggerKey tk = TriggerKey.triggerKey(name, group);
            scheduler.pauseTrigger(tk);// 停止触发器
            scheduler.unscheduleJob(tk);// 移除触发器
            JobKey jobKey = JobKey.jobKey(name, group);
            scheduler.deleteJob(jobKey);// 删除作业
            log.info("删除作业=> [作业名称:" + name + " 作业组:" + group + "] ");
            System.out.println("删除作业=> [作业名称:" + name + " 作业组:" + group + "] ");
        } catch (SchedulerException e) {
            e.printStackTrace();
            log.error("删除作业=> [作业名称:" + name + " 作业组:" + group + "]=> [失败]");
        }
    }

    public void start() {
        try {
            scheduler.start();
            log.info("启动调度器 ");
            System.out.println("启动调度器 ");
        } catch (SchedulerException e) {
            e.printStackTrace();
            log.error("启动调度器=> [失败]");
        }
    }

    public void shutdown() {
        try {
            scheduler.shutdown();
            log.info("停止调度器 ");
            System.out.println("停止调度器 ");
        } catch (SchedulerException e) {
            e.printStackTrace();
            log.error("停止调度器=> [失败]");
        }
    }
    //测试
    public static void main(String[] args) throws InterruptedException{
        QuartzManager qm = QuartzManager.qm;
        String name = "DynamicJobName";
        String group = "DynamicJobGroup";
        String cronExpression = "*/2 * * * * ?";
        String className = "com.fwpsl.util.Test1";
        Class<? extends Job> jobClazz = null;
        try {
            jobClazz = Class.forName(className).asSubclass(Job.class);
        } catch (Exception e) {
            System.out.println(className+"没有继承job,e=="+e);
        }
        if(jobClazz==null){
            System.exit(0);
        }
        qm.addJob(name, group, jobClazz, cronExpression);
        qm.start();
        Thread.sleep(9000);
        String name2 = "DynamicJobName2";
        String group2 = "DynamicJobGroup2";
        String cronExpression2 = "*/1 * * * * ?";
        qm.addJob(name2, group2, Test2.class, cronExpression2);
        Thread.sleep(5000);
        qm.removeJob(name, group);
        Thread.sleep(5000);
        qm.shutdown();
    }

}

Test1.java

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class Test1 implements Job {
   @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("任务1:"+CommonUtils.getDate());
    }
}

Test2.java

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class Test2 implements Job {
   @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("任务2:"+CommonUtils.getDate());
    }
}

 







以上是关于JFinal框架_定时触发程序的主要内容,如果未能解决你的问题,请参考以下文章

[JavaWeb]_[中级]_[Tomcat在关闭时报指向Jfinal框架的错误]

[JavaWeb]_[初级]_[对Jfinal框架的Service层进行单元测试]

[JavaWeb]_[初级]_[对Jfinal框架的Service层进行单元测试]

jfinal定时任务插件jfinal-quartz

JFinal极速开发框架使用笔记 _JFinalDemoGenerator实体类生成及映射自动化

[JavaWeb]_[初级]_[对Jfinal框架的Controller进行单元测试]