1

I´m trying to get an application attribute inside the Quartz Job, but I´m getting always a NPE:

import java.util.List;

import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.context.FacesContext;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.SubjectThreadState;
import org.apache.shiro.util.ThreadState;
import org.omnifaces.util.Faces;
import org.omnifaces.util.FacesLocal;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ApplicationScoped
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class DisallowConcurrentQuartzJob implements Job {

@Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
            
        FacesContext facesContext = FacesContext.getCurrentInstance();
        // -> IS NULL

             List<Object> customActionTypeClasses = Faces.getApplicationAttribute("TEST");
// -> IS NULL           

    }

Any ideas why this is not working? I´ve created also a CDI Factory:

   @ApplicationScoped
    public class SchedulerQuartzServiceBean {
    
    @Inject
    private CdiSchedulerJobFactory cdiSchedulerJobFactory;
    
          // Start
         public void startSchedule() {
            Scheduler scheduler = schedulerFactory.getScheduler(defaultSchedulerName);
           scheduler.setJobFactory(cdiSchedulerJobFactory);
          }
     }

@ApplicationScoped
public class CdiSchedulerJobFactory implements JobFactory {

    @Inject
    private BeanManager beanManager;

    @Override
    public Job newJob(final TriggerFiredBundle bundle, final Scheduler scheduler) throws SchedulerException {

        Class<? extends Job> jobClass = bundle.getJobDetail().getJobClass();
        Set<Bean<?>> beans = beanManager.getBeans(jobClass);

        if (beans.isEmpty()) {
            throw new SchedulerException("No job instance found for job class: " + jobClass.getName());
        }

        Bean<?> bean = beanManager.getBeans(jobClass).iterator().next();
        CreationalContext<?> ctx = beanManager.createCreationalContext(bean);

        return (Job) beanManager.getReference(bean, jobClass, ctx);
    }
}

Does anybody have an idea what´s wrong here? The Faces.getApplicationAttribute will throw a NPE because FacesContext is null.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
vished2000
  • 164
  • 1
  • 11
  • Answer depends on the functional requirement. Which problem exactly are you trying to solve for which you thought that this would be the correct solution? For now this is the closest matching duplicate: https://stackoverflow.com/q/2803160 – BalusC Jan 09 '23 at 12:27
  • Thank you for your answert... What I´m currently do is the following: I save objects in the Omnifaces Faces.setApplicationAttribute(myObject)... So I can pass my objects from my "primary project" also to my "external jar project". Information like: "currentUser", "currentLanguage" etc. This is working also pretty good, but not for Quartz scheduling.... Maybe Quartz scheduler runs in its own threading mechanism on the server and has no idea about the UI and thus no FacesContext etc.? Any idea how I can save such objects also that I can use it in Quartz? – vished2000 Jan 09 '23 at 12:46
  • Why don't you just save them as a property of an `@ApplicationScoped` bean? – BalusC Jan 09 '23 at 12:50
  • I guess ```Faces.setApplicationAttribute(myObject)``` is doing the same as a ```@ApplicationScoped``` ? – vished2000 Jan 09 '23 at 12:53
  • Basically yes, as `@ApplicationScoped public class MyObject {}`. – BalusC Jan 09 '23 at 13:01

0 Answers0