Quartz 最简单使用,没有用到配置文件,不适用于项目
2009-12-18 11:19
639 查看
ScanDirectoryJob 类,实现Job接口,在execute()中做想调用的事情
package com.haso.quartz;
import java.io.File;
import java.io.FileFilter;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.haso.utils.Utils;
public class ScanDirectoryJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException
{
//Every job has its own job detail
JobDetail jobDetail = context.getJobDetail();
// The name is defined in the job definition
String jobName = jobDetail.getName();//任务名称
// Log the time the job started
System.out.println(jobName + " fired at " + Utils.dateToStr(new Date(), "yyyy-MM-dd hh:mm:ss"));
// The directory to scan is stored in the job map
JobDataMap dataMap = jobDetail.getJobDataMap();//任务所配置的数据映射表
String dirName = dataMap.getString("SCAN_DIR");//获取要扫描的目录
// Validate the required input
if (dirName == null)
{//所需要的扫描目录没有提供
throw new JobExecutionException( "Directory not configured" );
}
// Make sure the directory exists
File dir = new File(dirName);
if (!dir.exists()){//提供的是错误目录
throw new JobExecutionException( "Invalid Dir "+ dirName);
}
// Use FileFilter to get only XML files
FileFilter filter = new FileExtensionFileFilter(".xml");
//只统计xml文件
File[] files = dir.listFiles(filter);
if (files == null || files.length <= 0)
{//目录下没有xml文件
System.out.println("No XML files found in " + dir);
// Return since there were no files
return;
}
// The number of XML files
int size = files.length;
// Iterate through the files found
for (int i = 0; i < size; i++)
{
File file = files[i];
// Log something interesting about each file.
File aFile = file.getAbsoluteFile();
long fileSize = file.length();
String msg = aFile + " - Size: " + fileSize;
System.out.println(msg);
}
}
}
上类中使用到的FileExtensionFileFilter类
package com.haso.quartz;
import java.io.File;
import java.io.FileFilter;
public class FileExtensionFileFilter implements FileFilter {
private String extension;//文件后缀
public FileExtensionFileFilter(String extension)
{
this.extension = extension;
}
public boolean accept(File file)
{//只接受指定后缀的文件
// Lowercase the filename for easier comparison
String lCaseFilename = file.getName().toLowerCase();//小写化
return (file.isFile() && (lCaseFilename.indexOf(extension) > 0 )) ? true : false ;
}
}
在main()中执行
package com.haso.quartz;
import java.util.Date;
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;
import com.haso.utils.Utils;
public class SimpleScheduler {
public static void main(String[] args)
{
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();
System.out.println("Scheduler started at " + Utils.dateToStr(new Date(), "yyyy-MM-dd hh:mm:ss"));
} catch (SchedulerException ex) {
ex.printStackTrace();
}
}
public Scheduler createScheduler() throws SchedulerException
{//创建调度器
return StdSchedulerFactory.getDefaultScheduler();
}
//Create and Schedule a ScanDirectoryJob with the Scheduler
private void scheduleJob(Scheduler scheduler) throws SchedulerException
{
// Create a JobDetail for the Job
JobDetail jobDetail = new JobDetail("ScanDirectory1111",Scheduler.DEFAULT_GROUP,ScanDirectoryJob.class);
// Configure the directory to scan
jobDetail.getJobDataMap().put("SCAN_DIR","e://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秒触发一次
String cronTime = "0 26 13 * * ? ";
//CronTrigger trigger = new CronTrigger("MyJob111",Scheduler.DEFAULT_GROUP,cronTime);//每天13点26分触发一次
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);
}
}
package com.haso.quartz;
import java.io.File;
import java.io.FileFilter;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.haso.utils.Utils;
public class ScanDirectoryJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException
{
//Every job has its own job detail
JobDetail jobDetail = context.getJobDetail();
// The name is defined in the job definition
String jobName = jobDetail.getName();//任务名称
// Log the time the job started
System.out.println(jobName + " fired at " + Utils.dateToStr(new Date(), "yyyy-MM-dd hh:mm:ss"));
// The directory to scan is stored in the job map
JobDataMap dataMap = jobDetail.getJobDataMap();//任务所配置的数据映射表
String dirName = dataMap.getString("SCAN_DIR");//获取要扫描的目录
// Validate the required input
if (dirName == null)
{//所需要的扫描目录没有提供
throw new JobExecutionException( "Directory not configured" );
}
// Make sure the directory exists
File dir = new File(dirName);
if (!dir.exists()){//提供的是错误目录
throw new JobExecutionException( "Invalid Dir "+ dirName);
}
// Use FileFilter to get only XML files
FileFilter filter = new FileExtensionFileFilter(".xml");
//只统计xml文件
File[] files = dir.listFiles(filter);
if (files == null || files.length <= 0)
{//目录下没有xml文件
System.out.println("No XML files found in " + dir);
// Return since there were no files
return;
}
// The number of XML files
int size = files.length;
// Iterate through the files found
for (int i = 0; i < size; i++)
{
File file = files[i];
// Log something interesting about each file.
File aFile = file.getAbsoluteFile();
long fileSize = file.length();
String msg = aFile + " - Size: " + fileSize;
System.out.println(msg);
}
}
}
上类中使用到的FileExtensionFileFilter类
package com.haso.quartz;
import java.io.File;
import java.io.FileFilter;
public class FileExtensionFileFilter implements FileFilter {
private String extension;//文件后缀
public FileExtensionFileFilter(String extension)
{
this.extension = extension;
}
public boolean accept(File file)
{//只接受指定后缀的文件
// Lowercase the filename for easier comparison
String lCaseFilename = file.getName().toLowerCase();//小写化
return (file.isFile() && (lCaseFilename.indexOf(extension) > 0 )) ? true : false ;
}
}
在main()中执行
package com.haso.quartz;
import java.util.Date;
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;
import com.haso.utils.Utils;
public class SimpleScheduler {
public static void main(String[] args)
{
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();
System.out.println("Scheduler started at " + Utils.dateToStr(new Date(), "yyyy-MM-dd hh:mm:ss"));
} catch (SchedulerException ex) {
ex.printStackTrace();
}
}
public Scheduler createScheduler() throws SchedulerException
{//创建调度器
return StdSchedulerFactory.getDefaultScheduler();
}
//Create and Schedule a ScanDirectoryJob with the Scheduler
private void scheduleJob(Scheduler scheduler) throws SchedulerException
{
// Create a JobDetail for the Job
JobDetail jobDetail = new JobDetail("ScanDirectory1111",Scheduler.DEFAULT_GROUP,ScanDirectoryJob.class);
// Configure the directory to scan
jobDetail.getJobDataMap().put("SCAN_DIR","e://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秒触发一次
String cronTime = "0 26 13 * * ? ";
//CronTrigger trigger = new CronTrigger("MyJob111",Scheduler.DEFAULT_GROUP,cronTime);//每天13点26分触发一次
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);
}
}
相关文章推荐
- iOS开发:给项目工程里面添加Podfile配置文件,以及CocoaPods的简单使用
- 获取java web项目里面所有的properties文件并组装在一起,使用方便,配置简单
- iOS开发:给项目工程里面添加Podfile配置文件,以及CocoaPods的简单使用
- IDEA 使用Gradle构建项目后没有src文件,自己新建后不能创建Java文件的解决办法
- Web应用程序项目XXXX已配置为使用IIS。无法访问IIS 元数据库。您没有足够的特权访问计算机上的IIS
- 使用XML作为项目的配置文件使用,并解析之,获得数据作为链接数据库的参数
- Unity3D 使用XML进行简单的配置文件改动
- 在roathog配置的项目中使用webpack配置文件
- ThinkPHP多应用/项目配置技巧(使用同一配置文件)--(十六)
- 转: 使用maven给spring项目打可直接运行的jar包(配置文件内置外置的打法)
- ThinkPHP第七天(F函数使用,项目分组配置,项目分组模板文件放置规则配置)
- Quartz.Net的使用(简单配置方法)定时任务框架
- Windows8[Web应用程序项目***已配置为使用IIS。无法访问IIS元数据库,您没有足够的特权访问计算机上的IIS网站]
- 简单使用boost property_tree组件获取配置文件信息
- [Web]Servlet中使用Quartz【二】基于配置文件和jobs.xml触发任务执行
- MyEclipse中为struts项目添加和使用多个Struts-Config配置文件
- 大型项目使用Automake/Autoconf完成编译配置(标准的编译过程已经变成了简单的三部曲:configure/make/make install,)
- Spring Boot配置文件规则以及使用方法官方文档查找以及Spring项目的官方文档查找方法
- 使用websphere开发J2EE项目时,在服务器配置文件->“环境”->“类路径”中添加的ORACLE的JDBC文件classes12.zip文件是否有效呢?