3 Replies Latest reply on May 23, 2012 11:49 AM by Martin Gencur

    Cannot inject cache

    lucianob Newbie

      Hi everybody,

       

      I'm new to Infinispan and JBoss 7, so I tried the quickstart:

      https://github.com/infinispan/infinispan-quickstart/tree/master/jboss-as7

      I use JBoss 7.1.1, so I added jndi-name="java:jboss/infinispan/jboss-as7-quickstart" to the cache-container tag.

       

      It works It gives no problem at all.

       

      But I need cache only in "internal" functions of a servlet, so I tried with a Quartz job:

       

       

      {code}

      package org.infinispan.quickstart.jbossas7;

       

      import java.io.Serializable;

      import javax.enterprise.context.SessionScoped;

      import javax.inject.Inject;

      import javax.inject.Named;

      import org.infinispan.AdvancedCache;

      import org.jboss.logging.Logger;

      import org.quartz.Job;

      import org.quartz.JobExecutionContext;

      import org.quartz.JobExecutionException;

       

      @SessionScoped

      @Named

      public class InfinispanJob implements Job, Serializable {

                private static Logger log = Logger.getLogger(InfinispanJob.class);

                @Inject

                transient AdvancedCache<Object, Object> cache;

                @Override

                public void execute(JobExecutionContext context) throws JobExecutionException {

                          log.info("cache: " + cache);

                          String v = (String) cache.get("valore");

                          log.info("from cache: " + v);

                          long l = (long) (Math.random() * 5000 + 2000);

                          try {

                                    Thread.sleep(l);

                          } catch (InterruptedException e) {

                                    log.warn("interrupted");

                          }

                          log.info("to cache: " + l);

                          cache.put("valore", String.valueOf(l));

                }

      }

      {code}

       

      And this is the listener that initializes the Scheduler:

       

       

      {code}

      package it.infoblu.quickstart.quartz;

       

      import static org.quartz.CronScheduleBuilder.cronSchedule;

      import static org.quartz.JobBuilder.newJob;

      import static org.quartz.TriggerBuilder.newTrigger;

      import javax.servlet.ServletContextEvent;

      import javax.servlet.ServletContextListener;

      import org.infinispan.quickstart.jbossas7.InfinispanJob;

      import org.jboss.logging.Logger;

      import org.quartz.CronTrigger;

      import org.quartz.JobDetail;

      import org.quartz.Scheduler;

      import org.quartz.SchedulerException;

      import org.quartz.SchedulerFactory;

      import org.quartz.impl.StdSchedulerFactory;

       

      public class QuartzConfigurator implements ServletContextListener {

                private static Logger log = Logger.getLogger(InfinispanJob.class);

                private Scheduler sched;

                @Override

                public void contextInitialized(ServletContextEvent sce) {

                          log.info("Inizializzazione Quartz");

                          try {

                                    SchedulerFactory sf = new StdSchedulerFactory();

                                    sched = sf.getScheduler();

                                    JobDetail job = newJob(InfinispanJob.class)

                                                        .withIdentity("InfinispanJob", "InfinispanGroup")

                                                        .build();

                                    CronTrigger trigger = newTrigger()

                                                        .withIdentity("InfinispanTrigger", "InfinispanGroup")

                                                        .withSchedule(cronSchedule("*/30 * * * * ?"))

                                                        .build();

                                    sched.scheduleJob(job, trigger);

                                    sched.start();

                          } catch (SchedulerException e) {

                                    log.error(e);

                          }

                }

                @Override

                public void contextDestroyed(ServletContextEvent sce) {

                     // ...

                }

      }

      {code}

       

       

      But it does NOT work!!

       

      {quote}

      16:31:30,006 INFO  [org.infinispan.quickstart.jbossas7.InfinispanJob] (DefaultQuartzScheduler_Worker-1) cache: null

      16:31:30,007 ERROR [org.quartz.core.JobRunShell] (DefaultQuartzScheduler_Worker-1) Job InfinispanGroup.InfinispanJob threw an unhandled Exception: : java.lang.NullPointerException

                at org.infinispan.quickstart.jbossas7.InfinispanJob.execute(InfinispanJob.java:33) [classes:]

                at org.quartz.core.JobRunShell.run(JobRunShell.java:213) [quartz-2.1.4.jar:]

                at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557) [quartz-2.1.4.jar:]

       

       

      16:31:30,011 ERROR [org.quartz.core.ErrorLogger] (DefaultQuartzScheduler_Worker-1) Job (InfinispanGroup.InfinispanJob threw an exception.: org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.NullPointerException]

                at org.quartz.core.JobRunShell.run(JobRunShell.java:224) [quartz-2.1.4.jar:]

                at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557) [quartz-2.1.4.jar:]

      Caused by: java.lang.NullPointerException

                at org.infinispan.quickstart.jbossas7.InfinispanJob.execute(InfinispanJob.java:33) [classes:]

                at org.quartz.core.JobRunShell.run(JobRunShell.java:213) [quartz-2.1.4.jar:]

                ... 1 more

      {quote}

       

       

      Other experiments did not succeed as well.

       

      May someone give me a hint?

       

      Thank you very much!!!

       

      LB