quartz

1:maven仓库导入:

  1.         <dependency>  
  2.             <groupId>org.quartz-schedulergroupId>  
  3.             <artifactId>quartzartifactId>  
  4.             <version>2.2.1version>  
  5.         dependency>  
  6.           
  7.         <dependency>  
  8.             <groupId>org.springframeworkgroupId>  
  9.             <artifactId>spring-context-supportartifactId>  
  10.             <version>${project.spring.version}version>  
  11.         dependency> 

注意上面<version>${project.spring.version}version>是写和spring版本一样

2:新建一个名为spring-quartz.XML的文件

写入:


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
     

     
     
         
     

     
         
         
             10,15 * * * * ?
         

     

     
     
         
         
             
                 
             

         

     

注意上面 10,15 * * * * ?是配置定时器开始启动的时间代码

咋个弄可以参考这个https://blog.csdn.net/zdx1515888659/article/details/79158169

注意

3:在SPRING.xml(applicationontext.xml)文件里面配置注入:

   

还要有包扫描(注意包路径)

4:在包路径下新建一个JobFactory

写入:

@Service("f")
public class JobFactory extends AdaptableJobFactory {

    @Autowired
    private AutowireCapableBeanFactory capableBeanFactory;

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle)
            throws Exception {
        Object jobInstance = super.createJobInstance(bundle);
        capableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
    }

5:在包路径下新建PromotionJob

写入:public class PromotionJob implements Job{
    
    @Autowired
    private IInvoiceService invoiceServiceImpl;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("quartz执行");
        invoiceServiceImpl.updateDB(new Date());

    }

其中    @Autowired
    private IInvoiceService invoiceServiceImpl;是要在具体的方法里面做处理注入的一个服务


你可能感兴趣的:(java,quartz)