动态指定定时时间

 

 

思路:
传统的xml配置:

触发器配置部分:

  

< bean  id ="companyCronSJGSR3F2"  class ="org.springframework.scheduling.quartz.CronTriggerBean"   >
        
< property  name ="jobDetail" >
            
< ref  bean ="companyReportSJGSR3F2" />
        
property >
        
< property  name ="cronExpression" >
            
< value > 0 35 08 ? * * value >
        
property >
bean >

 


如果能动态的给 cronExpression 赋值,就能实现我们的要求。

自定义一个类,继承了 CronTriggerBean

MyCronTrigger.java

 

public   class  MyCronTrigger  extends  CronTriggerBean
{
    
public  JobManager jobManager;

    
public  JobManager getJobManager()
    {
        
return  jobManager;
    }

    
public   void  setJobManager(JobManager jobManager)
    {
        
this .jobManager  =  jobManager;
        String cronExpression 
=  getCroExpression();
        
try
        {
            setCronExpression(cronExpression);  
// 关键,获得cronExpression,然后set回去
        }
        
catch  (ParseException e)
        {
            e.printStackTrace();
        }
    }

    
private  String getCroExpression()
    {
        
return  jobManager.getExpression();
    }
}

 

JobManager.java

 

public   class  JobManager
{
    
// 关键点,此类可以通过获得数据库连接,
    
// 可以从数据库中取指定的expression
     public  String getExpression()
    {    
        String s
= " 0 52 19 ? * * " ;
        
return  s;
    }
}

 

croExpression.xml

 

xml version="1.0" encoding="UTF-8" ?>
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "/web_data/dtd/spring-beans-2.0.dtd" >
< beans >
    
< bean  id ="jobManager"  class ="com.dbjob.cro.JobManager" > bean >
    
    
< bean  id ="JobTask"  class ="com.dbjob.cro.JobTask" > bean >
    
    

    
< bean  id ="schedulerJobDetail"
        class
="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
        
< property  name ="targetObject"  ref ="JobTask"   />
        
< property  name ="targetMethod"  value ="dojob"   />
        
< property  name ="concurrent"  value ="false"   />
    
bean >

    
< bean  id ="cronTrigger"  class ="com.dbjob.cro.MyCronTrigger" >
        
< property  name ="jobDetail"  ref ="schedulerJobDetail"   />
        
< property  name ="jobManager"  ref ="jobManager"   />
    
bean >

    
< bean  id ="schedulerFactory"
        class
="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
        
< property  name ="triggers" >
            
< list >
                
< ref  local ="cronTrigger"   />
            
list >
        
property >
    
bean >
beans >

 

运行:主函数加载 spring 配置文件即可:

public   static   void  main(String[] args)
{
    ApplicationContext cxt
= new  FileSystemXmlApplicationContext( " config/croExpression.xml " );
}

 

在web中应用 只要在web.xml中加载配置文件即可。

你可能感兴趣的:(spring)