Quartz---Application学习(一)

演示创建一个Quartz应用的全过程,具体实现:

扫描指定目录的XML文件,如果指定目录中找到一个或多个XML文件,将会打印出文件的信息。可以进一步延伸:作业在检测到特定文件,可以将其FTP到一台远程机器上,或作为电子邮件发送。也可以做一些处理,保存到数据库中。

 

#创建一个Quartz Job类

每一个 Quartz Job  必须有一个实现了  org.quartz.Job  接口的具体类。这个接口仅有一个要你在 Job  中实现的方法, execute() ,方法  execute()  的原型如下:
public void execute(JobExecutionContext context) throws JobExecutionException;

当 Quartz  调度器确定到时间要激发一个  Job  的时候,它就会生成一个 Job  实例,并调用这个实例的  execute()  方法。可以在 execute()  方法中执行你的业务逻辑。

扫描一个目录中的文并显示文件的详细信息:

/**     
 * <p>     
 * A simple Quartz job that, once configured, will scan a     
 * directory and print out details about the files found     
 * in the directory.     
 * </p>     
 * Subdirectories will filtered out by the use of a     
 * <code>{@link FileExtensionFileFilter}</code>.     
 *     
 * @author Chuck Cavaness     
 * @see java.io.FileFilter     
 */      
public class ScanDirectoryJob implements Job {       
     static Log logger = LogFactory.getLog(ScanDirectoryJob.class);       
      
     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       
          logger.info(jobName + " fired at " + new Date());       
      
          // 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");       
      
          File[] files = dir.listFiles(filter);       
      
          if (files == null || files.length <= 0) {       
               logger.info("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;       
               logger.info(msg);       
          }       
     }       
}   

当 Quartz  调用  execute()  方法,会传递一个  org.quartz.JobExecutionContext  上下文变量,里面封装有 Quartz  的运行时环境和当前正执行的  Job 。通过  JobexecutionContext ,你可以访问到调度器的信息,作业和作业上的触发器的信息,还有更多更多的信息。 JobExecutionContext  被用来访问  org.quartz.JobDetail  类, JobDetail  类持有 Job  的详细信息。 JobDetail  又持有一个指向  org.quartz.JobDataMap  的引用。 JobDataMap  中有为指定 Job  配置的自定义属性。

/**     
 * A FileFilter that only passes Files of the specified extension.     
 * <p>     
 * Directories do not pass the filter.     
 *   
 * @author Chuck Cavaness     
 */       
public class  FileExtensionFileFilter  implements FileFilter {       
      
      private  String extension;       
      
      public  FileExtensionFileFilter(String extension) {       
           this .extension = extension;       
     }       
      
      /*     
      * Pass the File if it has the 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 ;       
     }       
}    

FileExtensionFileFilter  被用来屏蔽名称中不含字符串  .xml ” 的文件。它还屏蔽了子目录--这些子目录原本会让  listFiles()  方法正常返回。过滤器提供了一种很便利的方式选择性的向你的 Quartz  作业提供它能接受的作为输入的文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值