我有一个应用程序范围的bean

@ManagedBean(name = "myController")
@ApplicationScoped
public class MyController implements Serializable{
...
public void allOn(){...}


我想从石英作业中调用allOn()方法

import org.quartz.Job;
public class CronJobAllOn implements Job{
     @Override
     public void execute(..){
          //call allOn();}
}


我试图通过JobDataMap将FacesContext传递给Job-Class

JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("facesContext", FacesContext.getCurrentInstance());
JobDetail job = newJob(CronJobAllOn.class)
        .usingJobData(jobDataMap)
        .withIdentity("job1", "group1")
        .build();


但是当我尝试在CronJobAllOn类中调用它时,它只会引发IllegalStateException。

public void execute(JobExecutionContext context) throws JobExecutionException {
     FacesContext fc= (FacesContext) context.getMergedJobDataMap().get("facesContext");
     MyController test  = (MyController)fc.getExternalContext().getApplicationMap().get("MyController");
     test.allOn();}


如何从石英作业中调用MyController中的allOn()方法?

最佳答案

我为我的问题找到了解决方案,BalusC的简短评论使我走上了正确的道路。
我切换到TomEE,以获得CDI。

要在我的工作中使用CDI-Bean注入,我必须创建自己的JobFactory类:

public class CdiJobFactory implements JobFactory {

@Inject
@Any
private Instance<Job> jobs;

@Override
public Job newJob(TriggerFiredBundle triggerFiredBundle, Scheduler scheduler) throws SchedulerException {
    final JobDetail jobDetail = triggerFiredBundle.getJobDetail();
    final Class<? extends Job> jobClass = jobDetail.getJobClass();

    for (Job job : jobs) {
        if (job.getClass().isAssignableFrom(jobClass)) {
            return job;
        }
    }

    throw new RuntimeException("Cannot create a Job of type " + jobClass);
}


创建工厂

Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.setJobFactory(cdiJobFactory);


之后,我能够注入myController:

public class CronJobAllOn implements Job{

@Inject
private MyController mc;

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {

    mc.allOn();

}

10-05 18:51