1. 程式人生 > >spring3.1整合quartz時,spring依賴注入無法注入到quartz的job中的處理方法

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;    
    }  
}