在Java Web App中使用Google Guice / Dogger在Quartz Job执行中创建对象链

问题描述

我开始在Java Web应用程序中使用Quartz创建一个cron作业,以执行清理任务之一。实际工作按预期进行,没有 Quatrz作业执行出现问题。从下面的代码片段中,当我尝试使用MyService作为工作的一部分来调用所需的方法时,依赖项无法正常工作。

MyService类的对象未创建。所以我得到一个空指针异常。为了避免这种情况,我尝试使用Google Guice(也尝试过dogger)进行管理 依赖项注入,但是MyService对象的创建依赖于其他类。

在这个示例中,让我们说CustomerService和CustomerService依赖于其他类,而这些类依赖于其他类。因此,我将最终使用Guice或Dogger创建对象创建链。

代码段:

  import com.google.inject.Inject;
    import com.google.inject.Injector;
    import com.google.inject.Singleton;
    import org.quartz.Job;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    import org.quartz.spi.JobFactory;
    import org.quartz.spi.TriggerFiredBundle;
    
        @Singleton
        public class CustomJobFactory implements JobFactory {
        
           @Inject
           private Injector injector;
        
           @Override
           public Job newJob(TriggerFiredBundle bundle,Scheduler scheduler) throws SchedulerException {
              return (Job) injector.getInstance(bundle.getJobDetail().getJobClass());
           }
    }


public class QuartzJobHelper  {

   private final Scheduler scheduler;


   @Inject
   public QuartzSchedulerImpl(Scheduler scheduler,CustomJobFactory customJobFactory) {
      this.scheduler = scheduler;
      try {
         this.scheduler.setJobFactory(customJobFactory);
      } catch (SchedulerException e) {
         e.printStackTrace();
      }
   }

   public static void executeJob() throws Exception {

      // The Guice injector used to create instances.
      Injector injector = Guice.createInjector(new MyModule());
      JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity("myjob","mygroup")
            .build();
    Trigger trigger = TriggerBuilder.newTrigger().withIdentity("mytrigger","mygroup")
            .withSchedule(CronScheduleBuilder.cronSchedule("0/1 * * * * ?")).build();

      SchedulerFactory schedulerFactory = new StdSchedulerFactory();
      Scheduler scheduler = schedulerFactory.getScheduler();

      // Inform Quartz scheduler to use our factory.
      scheduler.setJobFactory((injector.getInstance(CustomJobFactory.class));
      scheduler.scheduleJob(jobDetail,trigger);

      // Start the scheduler.
      scheduler.start();
   }

}

导入org.quartz.spi.JobFactory; 导入java.io.IOException;

public class MyModule extends AbstractModule {



   @Override
   protected void configure() {

       bind(JobFactory.class).to(MyJobFactory.class);
       bind(MyService.class).to(MyServiceImpl.class);
       bind(CustomerService.class).to(CustomerServiceImpl.class);
       bind(MyDao.class).to(MyDaoImpl.class);
       bind(CustomCache.class).to(CustomCacheImpl.class);
       bind(ProxyDao.class).to(ProxyDaoImpl.class);
       bind(RequestDao.class).to(DefaultRequestDao.class);
       |
       |
       |
       etc.. Likewise so many dependencies are there to inject but finding 100's of dependencies.
     
   }

}

public class MyJob extends org.quartz.Job {

 @Inject
 MyService service;

 public void execute(JobExecutionContext jobContext) throws JobExecutionException {
      service.cleanupJunkData();
     }    
}

class MyService {
 void cleanupJunkData();
}

@Named
class MyServiceImpl implments MyService {
   
   private CustomerService cs; 
   private MyDao dao;
   private CustomCache cache;
   
   @Inject 
   public MyServiceImpl(CustomerService cs,MyDao dao,CustomCache cache) {
     this.cs = cs;
     this.dao=dao;
     this.cache = cache;
   }
   
   @Override
   public void cleanupJunkData() {
     List<Customer> customerData = cs.getCustomerData(dao.getExpiredCustId());
     cache.clear(customerData);
   }
}

public Class MainClass {
  public static void main(Stirng[] args) {
      QuartzJobHelper.executeJob();
  }
}

有人可以建议如何处理Quartz作业中的对象创建吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)