java jfinal 定时任务_JFinal 中的cron4j定时插件

宇文俊风
2023-12-01

1、下载cron4j的jar包,并放入classpath

2、编写Cron4jPlugin.java

package com.welicai.app.common.plugin;

import it.sauronsoftware.cron4j.InvalidPatternException;

import it.sauronsoftware.cron4j.Scheduler;

import java.io.IOException;

import java.io.InputStream;

import java.util.Date;

import java.util.Enumeration;

import java.util.Properties;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.jfinal.plugin.IPlugin;

import com.welicai.app.test.Testjob;

public class Cron4jPlugin implements IPlugin {

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

private Scheduler scheduler = null;

private String config = "job.properties";

private Properties properties;

public Cron4jPlugin(String config) {

this.config = config;

}

public Cron4jPlugin() {

}

@SuppressWarnings({ "rawtypes"})

public boolean start() {

scheduler = new Scheduler();

loadProperties();

Enumeration enums = properties.keys();

while (enums.hasMoreElements()) {

String key = enums.nextElement() + "";

if (!key.endsWith("job")) {

continue;

}

String cronKey = key.substring(0, key.indexOf("job")) + "cron";

String enable = key.substring(0, key.indexOf("job")) + "enable";

if (isDisableJob(enable)) {

continue;

}

String jobClassName = properties.get(key) + "";

String jobCronExp = properties.getProperty(cronKey) + "";

Class clazz;

try {

clazz = Class.forName(jobClassName);

} catch (ClassNotFoundException e) {

throw new RuntimeException(e);

}

try {

scheduler.schedule(jobCronExp, (Runnable) clazz.newInstance());

} catch (InvalidPatternException e) {

throw new RuntimeException(e);

} catch (InstantiationException e) {

throw new RuntimeException(e);

} catch (IllegalAccessException e) {

throw new RuntimeException(e);

}

scheduler.start();

logger.info(jobCronExp + " has been scheduled to run at: " + new Date()

+ " and repeat based on expression: "

);

}

return true;

}

private boolean isDisableJob(String enable) {

return Boolean.valueOf(properties.get(enable) + "") == false;

}

private void loadProperties() {

properties = new Properties();

InputStream is = Cron4jPlugin.class.getClassLoader()

.getResourceAsStream(config);

try {

properties.load(is);

} catch (IOException e) {

throw new RuntimeException(e);

}

}

public boolean stop() {

scheduler.stop();

return true;

}

public static void main(String[] args) throws InterruptedException {

Cron4jPlugin plugin = new Cron4jPlugin();

plugin.start();

System.out.println("执行成功!!!");

Thread.sleep(5000);

// plugin.stop();

}

}

3、创建一个定时任务类(必须实现Runnable接口)

public class Testjob implements Runnable {

public void run() {

System.out.println("Current system time: " + new Date());

System.out.println("Another minute ticked away...");

}

}

4、在classpath下加入job.properties,然后配置上刚才创建的任务类(1分钟执行一次)

UpdateOrderStatusJob.job=com.welicai.app.test.Testjob

UpdateOrderStatusJob.cron=*/1 * * * *

UpdateOrderStatusJob.enable=true

5、把Cron4jPlugin加入jfinal的核心配置类configPlugin方法

/**

* 配置插件

*/

public void configPlugin( Plugins me ) {

// 配置C3p0数据库连接池插件

C3p0Plugin c3p0Plugin = new C3p0Plugin( getProperty("url"), getProperty("user"), getProperty("pwd") );

me.add( c3p0Plugin );

// 配置ActiveRecord插件

ActiveRecordPlugin arp = new ActiveRecordPlugin( c3p0Plugin );

arp.setShowSql( Boolean.valueOf( getProperty("showSql") ) );

me.add( arp );

// 配置Cron4jPlugin插件

me.add(new Cron4jPlugin());

}

6、启动项目试一下吧(注意:cron4j只能精确到分,不能到秒,但是应该够用了,时间语法同Linux的crontab)

 类似资料: