1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[JBoss] Bean Quartz Inject - Null pointer exception

Discussão em 'StackOverflow' iniciado por Stack, Setembro 27, 2021.

  1. Stack

    Stack Membro Participativo

    I have a problem, injecting a CDI Bean in a Quartz schedule Job. I have added custom CDIJobFactory for Inject my interface in quartz. when quartz method executed on Jboss eap 6.4, it's throw error


    java.lang.IllegalArgumentException: JobFactory cannot be set to null!

    Initially I have tried with EJB Timer (@Schedule annotation) but EJB timer not worked and console not have any issue so have no idea to move further with EJB timer then I have start to work on quartz.

    So kindly let me know if anything I have missed in quartz or did anything wrong because I'm totally new for quartz, EJB and jboss server

    CDIJobFactory.class

    @ApplicationScoped
    public class CDIJobFactory implements JobFactory {
    @Inject @Any
    private Instance<Job> jobs;

    @Override
    public Job newJob(TriggerFiredBundle bundle, Scheduler scheduler) throws SchedulerException {
    final Class<? extends Job> clazz = bundle.getJobDetail().getJobClass();
    final Instance<? extends Job> bean = jobs.select(clazz);
    if (bean.isUnsatisfied()) throw new SchedulerException("No job bean of type " + clazz + " found.");
    return bean.get();
    }
    }


    ScheduleJob.class

    @ApplicationScoped
    public class ScheduleJob {
    private Scheduler scheduler;

    @Inject
    private CDIJobFactory jobFactory;

    public void cronInitialized() {
    try {
    scheduler = StdSchedulerFactory.getDefaultScheduler();
    scheduler.setJobFactory(jobFactory); // jobFactory throwing null exception
    } catch (final SchedulerException | RuntimeException schedEx) {
    System.out.println("Problem loading Quartz!"+ schedEx);
    }
    JobDetail jobDetail = JobBuilder.newJob(messageJob.class)
    .withIdentity(ImagesProcessJob.class.getSimpleName())
    .build();
    CronTrigger trigger = TriggerBuilder.newTrigger()
    .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
    .forJob(jobDetail).build();
    try {
    scheduler.scheduleJob(jobDetail, trigger);
    scheduler.start();
    } catch (SchedulerException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }


    messageJob.class

    @ApplicationScoped
    public class messageJob implements Job {
    @Inject DNOWorkflowBO dnoWorkflowBO;
    public messageJob() {}
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
    try {
    dnoWorkflowBO.dnoCronJob(); // this line throwing null pointer exception
    } catch(Exception e) {
    throw new JobExecutionException(e);
    }
    }
    }


    BODAppInitServlet.class

    public class BODAppInitServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public void init(ServletConfig config) throws ServletException {
    try {
    new ScheduleJob().cronInitialized();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    Continue reading...

Compartilhe esta Página