1

I have an application scoped bean

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

And i want to call the allOn() method from a quartz-job

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

I tried to pass the FacesContext to the Job-Class via the JobDataMap

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

But it only throws an IllegalStateException when i try to call it in the CronJobAllOn Class

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

How can i call the allOn() method in MyController from a quartz-job?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • The ISE is explained here: http://stackoverflow.com/q/4605118 As to the concrete requirement, do you run a real Java EE server or a barebones servletcontainer? Do you have CDI available? (I know you tagged [java-ee], but using Quartz instead of standard EJB API suggests you're actually using a servletcontainer like Tomcat instead of a real Java EE server). – BalusC Apr 02 '16 at 13:07
  • Thanks for your answer, i don't know what you mean with ISE. I am not running a Java EE server, so i changed the tag you mentioned. Do i have more options when i change to Java-EE Server? The only solution to my problem who comes to my mind, is to write data to a database from the quartz-job and read it in the application-scoped bean. – Samuel Werner Apr 02 '16 at 14:08
  • I got now the meaning of ISE. ISE stands for IllegalStateException. Much to learn for me. An I read your helpful message after the link, that i never should assign FacesContext as instance variable of a view/session/application scoped managed bean, and reuse it in different requests. – Samuel Werner Apr 02 '16 at 14:48

1 Answers1

1

I got the solution for my Problem, the short comment from BalusC put me on the right path. I switched to TomEE, to get CDI.

To use the CDI-Bean injection in my jobs, i had to create my own JobFactory Class:

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

create the Factory

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

after that i was able to inject myController:

public class CronJobAllOn implements Job{

@Inject
private MyController mc;

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

    mc.allOn();

}