1.引入nutz-integration-quartz插件
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz-integration-quartz</artifactId>
<version>${nutz-version}</version>
</dependency>
2.在resources目录下加入quartz.properties
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = NutzScheduler
org.quartz.scheduler.instanceId = AUTO
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 25
org.quartz.threadPool.threadPriority = 5
#============================================================================
# Configure JobStore
#============================================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
#============================================================================
# Configure Datasources
#============================================================================
org.quartz.dataSource.myDS.driver: com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL: jdbc:mysql://localhost:3306/kams
org.quartz.dataSource.myDS.user = root
org.quartz.dataSource.myDS.password = ***
org.quartz.dataSource.myDS.maxConnections = 5
org.quartz.dataSource.myDS.validationQuery=select 0 from dual
3.初始化quartz数据库表格
下载最新版本quartz并解压,运行里面的mysql建表sql即可。
1.png
4.在MainModule中IocBy注解中加入*quart参数
@IocBy(type=ComboIocProvider.class, args={"*js", "ioc/",
"*anno", "com.kaisa.kams",
"*tx",
"*async",
"*quartz",
"*com.kaisa.kams.components.flow.SnakerIocLoader",
})
5.编写Job
public class RepeatJob implements Job {
private static final Logger LOGGER = LoggerFactory.getLogger(RepeatJob.class);
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Just do it");
}
}
6.添加定时任务
一.获取QuartzManager
QuartzManager manager = ioc.get(QuartzManager.class);
二.添加定时任务
//每分钟第五秒执行
manager.cron("5 * * * * ? ",RepeatJob.class);
7.单元测试:
public class TestJob extends BaseTest{
QuartzManager manager;
Ioc ioc;
public void setUp() throws Exception {
ioc = super.getIoc();
manager = ioc.get(QuartzManager.class);
}
public void testRepeatJob() {
manager.cron("5 * * * * ? ",RepeatJob.class);
}
public static void main(String args[]) throws Exception {
TestJob tJob = new TestJob();
tJob.setUp();
tJob.testRepeatJob();
}
}
8.其他:
quartz官网:http://www.quartz-scheduler.org
quartz2.2.1 API doc:http://www.quartz-scheduler.org/api/2.2.1/index.html
cron表达式在线编辑器:http://cron.qqe2.com
网友评论