spring3.1集成quartz时,spring依赖注入无法注入到quartz的job中的处理方法

  1. 定时任务类
package cn.customercard.controller;

import java.util.Date;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.customercard.quartz.InsertJob;

@Service
public class AddCustomerLogQuart implements ServletContextListener{
    @Autowired
    SchedulerFactoryBean schedulerFactoryBean;

    public void insert(){
        try {
            //创建一个scheduler
            //SchedulerFactory schedulerFactory=new StdSchedulerFactory();
            //Scheduler scheduler = schedulerFactory.getScheduler();
            Scheduler scheduler = schedulerFactoryBean.getScheduler();
            scheduler.start();
            // 创建一个JobDetail,指明name,groupname,以及具体的Job类名(定义具体的执行任务)
            JobDetail jobDetail=new JobDetail("wJob", "wJobGroup", InsertJob.class);
            jobDetail.getJobDataMap().put("type","null");
            System.out.println("-------------------------开始执行定时调度任务1-----------------------------------------");
            // 创建一个每周触发的Trigger,指明星期几几点几分执行
            Trigger trigger=TriggerUtils.makeDailyTrigger("wtrigger", 14, 01);
            trigger.setGroup("wTriggerGroup");
            //从当前时间的下一秒开始执行
            trigger.setStartTime(TriggerUtils.getEvenSecondDate(new Date()));
            // 指明trigger的name
            trigger.setName("wTrigger");
            //用scheduler将JobDetail与Trigger关联在一起,开始调度任务
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {

    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
        AddCustomerLogQuart bean = (AddCustomerLogQuart) applicationContext.getBean("addCustomerLogQuart");
        try {
            bean.insert();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

2 job类

package cn.customercard.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import cn.iap.model.Customer;
import cn.iap.service.CustomerService;
import cn.store.model.SmsLogs;
import cn.store.service.SmsLogsService;
import cn.store.service.WaterProofService;
import cn.store.service.impl.WaterProofServiceImpl;

public class InsertJob implements Job{

    @Autowired
    WaterProofService waterProofService;
    @Autowired
    CustomerService customerService;
    @Autowired
    SmsLogsService smsLogsService;

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        try {
            System.out.println("----------------------------开始查询至今已满2个月的亲友卡客户信息,并插入数据库中。开始执行--------------------------------------");
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //查询购买亲友卡时间距今2个月的客户信息
            List<Map<String, Object>> customeridlist = waterProofService.findTwoMonthsExpire();
            if(customeridlist.isEmpty()){
                return;
            }
            System.out.println("----------------------------插入客户信息数量"+customeridlist.size());
            for(Map<String, Object> map:customeridlist){
                String customerid = (String) map.get("CUSTOMERID");
                if(!"".equals(customerid)&&customerid!=null){
                    Customer customer = customerService.selectByPrimaryKey(customerid);
                    if(customer!=null){
                        String content = "尊贵的壹路安会员,赠送给您的水+计划名额,还有1个月有效期,请您尽快推送亲朋好友哦。您也可到店进行套餐服务。如需上门服务可拨打58506699进行预约。";
                        SmsLogs smsLogs=new SmsLogs();
                        smsLogs.setMakedate(sdf.format(new Date()));
                        smsLogs.setMessagecontent(content);
                        smsLogs.setSendstate("0");
                        smsLogs.setPhoneno(customer.getPhoneno());
                        String smsid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
                        smsLogs.setSmsid(smsid);
                        smsLogsService.insertSelective(smsLogs);
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

3 spring.xml配置增加

<bean id="jobFactory" class="cn.customercard.quartz.JobFactory"></bean>  
    <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="jobFactory" ref="jobFactory"></property>  
    </bean>  

4 继承SpringBeanJobFactory

package cn.customercard.quartz;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;

public class JobFactory extends SpringBeanJobFactory{
    @Autowired    
    private AutowireCapableBeanFactory capableBeanFactory;    

    @Override    
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {    
        //调用父类的方法    
        Object jobInstance = super.createJobInstance(bundle);    
        //进行注入    
        System.out.println("--------------------------------------测试自动注入------------------");
        capableBeanFactory.autowireBean(jobInstance);    
        return jobInstance;    
    }  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值