4 Replies Latest reply on Jul 12, 2008 1:46 AM by tony.herstell1

    Getting something to run at Startup...

    tony.herstell1

      Finally back to doing some SEAM after a LONG LONG break!


      I want to run a Bean at startup... I simply want to pre-load some stuff in the database if it's not there already (another way would be to use import.sql but I don't want to guess all the Enum's)... I strongly use Enums (to cater for lack of Ada like Types. Anyhow...


      I try this (below) but I don't know what the incantation is to get the method to actually run (you can see me trying the constructor.


      This HAS to run before another bean... as he other bean expects the stuff to be in the database.


      @SuppressWarnings("serial")
      @Stateless
      @Scope(value=ScopeType.APPLICATION)
      @Startup
      @Name("initialisationController")
      public class InitialisationControllerImpl implements InitialisationController, Serializable {
      
              /**
               * Inject and leverage the Seam Logger.
               */
              @Logger
              private Log log;
              
              /**
               * Inject the EJB3 Persistence context in EXTENDED mode so will remain
               * "current" over multiple client/server round trips.
               */
              @PersistenceContext
              private EntityManager em;
              
              public InitialisationControllerImpl() {
                      log.info("Initialising Resource Kinds if required.");
                      for (ResourceKind eachResourceKind : ResourceKind.values()) {
                              Query theQuery = em.createQuery("from Resource r where r.kind = :kind").setParameter("kind", eachResourceKind);
                              if (theQuery.getResultList().isEmpty()) {
                                      Resource resource = new Resource();
                                      resource.setKind(eachResourceKind);
                                      resource.setOpen(true);
                                      log.info("adding " + eachResourceKind);
                                      em.persist(resource);
                              }
                      }
              }
      }



      If a bean outjects an object I can get it to work ok; but the above code doesnt outject anything; so I cant hang a factory off a method.



           @Out(scope=ScopeType.SESSION)
           Resource indoorArenaOpenStatus;
      
           @Factory (value="indoorArenaOpenStatus")
           public void initIndoorArenaStatus() {
                log.info (">initIndoorArenaStatus");
                indoorArenaOpenStatus = getResource(ResourceKind.INDOOR_ARENA);
           }