1 Reply Latest reply on Aug 30, 2007 1:41 PM by Matt Drees

    @In-jection fails on randomly applicaion scoped components w

    Troy Tarrrant Newbie

      Hi,

      I have two seam application scoped components, one holds an internal list which is added to by the other. So..

      @Name("entryGate")
      @Scope(ScopeType.APPLICATION)
      public class EXAccessPoint {
      
       @Logger Log log;
       @In EntityManager em;
       List<EntryExit> exs = new ArrayList<EntryExit>();
      ...
       public void add(String pid, Date time, boolean isExit) {
       log.info("add():pid=" + pid + ";time=" + time + ";em=" + em);
       EntryExit ex = null;
      ...
       exs.add(0, ex);
       }
      


      and the other uses this component (via @In) to add to the list

      @Name("entrySimulation")
      @Scope(ScopeType.APPLICATION)
      public class EXSimulation {
      
       @Logger Log log;
       @In(create=true) EXAccessPoint entryGate;
      ...
       public void simulate() {
      ...
       entryGate.addEntry("PID" + (int)((Math.random() * 5) + 1), current.getTime());
       }
      
      


      Now here comes the tricky bit. Two web pages are operating using <a:poll>. the first polls EXSimulation every ten seconds to simulate another series of additions to the list held by EXAccessPoint. The other polls every 1 sec to read the contents of the list from EXAccessPoint and renders them to the UI. What I'm trying to do is simulate people arriving at an EXAccessPoint and have the UI in 'near' real time display them in a security booth (hence all the Ajax)

      In this scenario, every so often (but predictable) em is NOT @In-jected into EXAccessPoint when EXAccessPoint is itself injected into EXSimulation. I get a NullPointerException and my logs show:

      20:53:16,533 INFO [EXAccessPoint] add():pid=PID1;time=Thu Aug 30 20:53:25 NZST 2007;em=org.jboss.seam.persistence.EntityManagerProxy@9fa638
      20:53:16,533 INFO [EXAccessPoint] add():pid=PID1;time=Thu Aug 30 20:53:20 NZST 2007;em=org.jboss.seam.persistence.EntityManagerProxy@9fa638
      ...
      20:53:17,023 INFO [EXAccessPoint] add():pid=PID5;time=Thu Aug 30 20:53:26 NZST 2007;em=org.jboss.seam.persistence.EntityManagerProxy@9fa638
      ...
      20:53:39,749 INFO [EXAccessPoint] add():pid=PID2;time=Thu Aug 30 20:53:46 NZST 2007;em=null
      20:53:39,751 FATAL [application] /cgsimulateentry.xhtml @61,102 action="#{entrySimulation.simulate}": java.lang.NullPointerException
      


      There is concurrency and @In-jection going on here with an application scoped component, this is probably not the right design as the two threads are potentially @In-jecting a different em proxy and changing the reference to the em while the other thread is midway through using the component.

      What is the design pattern that should be used here to avoid the changing of an @In-jected em on an application scoped component across multiple threads?

      Thanks

      Troy