7 Replies Latest reply on Jun 4, 2011 3:33 AM by Pieter Martin

    WebApplication Startup Event and RequestContet ?

    Pieter Martin Novice

      Hi,


      I am trying to use the WebApplication Startup event to do some persist some entities. We are however using the seam managed hibernate session via @Inject Session session


      This however requires the RequestContext to be active because it uses org.jboss.seam.persistence.transaction.EjbSynchronizations which is RequestScoped.


      Is there a way to make the web application startup call happen in a request scope?


      Thanks
      Pieter




        • 1. Re: WebApplication Startup Event and RequestContet ?
          Sebastian Sachtleben Novice
          @Named
          public class DataImporterMandatory {
          
            @PersistenceContext EntityManager em;
               
            @Inject Logger log;
            @Inject UserTransaction utx;
          
            public void loadData(@Observes @Initialized ServletContext ctx) throws IOException {
              persist(new Test());
            }
          
            public void persist(Object entity)
            {
              try {
                try {
               em.persist(entity);
                } catch (TransactionRequiredException e) {
               utx.begin();
               em.persist(entity);
               utx.commit();
                }
              } catch (Exception e) {
                log.error("Error importing data: " + entity, e);
                try {
               utx.rollback();
                } catch (Exception e1) {
                  log.error("Error rolling back transaction", e1);
                }
              }
            } 
          
          }



          • 2. Re: WebApplication Startup Event and RequestContet ?
            Pieter Martin Novice

            That won't do as we have to use the hibernate session. JPA does not support the @Any tag which is critical for our application. This means we do not have a persistence unit at all and are running pure hibernate. This is working very well with the hibernate session management that the seam persistence project provides, except for the RequestScope that is not available on startup. I am thinking of using sending a jms message on the webapplication startup event and putting the hibernate sesison persistence code in the jms listener.
            Hoping for a better solution though.


            Thanks
            Pieter

            • 3. Re: WebApplication Startup Event and RequestContet ?
              Dan Allen Master

              Can you file an issue in Seam persistence? The session should be able to work when outside of a request context (for various reasons), as it did in Seam 2.

              • 5. Re: WebApplication Startup Event and RequestContet ?
                Stefano Travelli Newbie

                What's the point on not having Request (and Conversation) context initialized here?


                This is causing major headache to me because I cannot use most of the components to load data at startup. For instance, I cannot use IdentitySession to create users. The same kind of problem applies on other parts of the framework like SEAM-CRON and SEAM-JMS.


                With Seam2 I was used to see all contexts initialized and the naming based injection always able to resolve a bean.


                Could someone elaborate a bit on this issue?

                • 6. Re: WebApplication Startup Event and RequestContet ?
                  Jason Porter Master

                  Request init you can get by using Seam Servlet. Conversation... there isn't one currently, but conversations are all started by the developer, there's nothing stopping you from firing an event when you start a conversation.

                  • 7. Re: WebApplication Startup Event and RequestContet ?
                    Pieter Martin Novice

                    We are using a dependent scoped hibernate session and manual transactioning to get pass these issues. In fact we are now using standard EE @Startup rather than webapplication as the request is not active anyways.


                    Else where we start EE timer which fires a bit later and then the contexts are active.


                    @Singleton(name = "LoadProcessScheduler")
                    @Startup
                    @DependsOn(value = { "InitializeHibernate", "DefaultDataCapture" })
                    public class LoadProcessScheduler {
                         @Resource
                         private TimerService timerService;
                         @EJB
                         private LoadProcessStarter loadProcessStarter;
                    
                         @Timeout
                         public void onTimeout(Timer timer) {
                              loadProcessStarter.checkForRawFiles();
                         }
                    
                         @PostConstruct
                         public void start() {
                              ScheduleExpression everySoManySeconds = new ScheduleExpression();
                              Calendar cal = Calendar.getInstance();
                              cal.add(Calendar.SECOND, 10);
                              everySoManySeconds.hour("*").minute("*").second("0/5").start(cal.getTime());
                              scheduleTask(everySoManySeconds, "loadProcess");
                         }
                    
                         public void scheduleTask(ScheduleExpression calendarSchedule, String taskName) {
                              TimerConfig timerConfig = new TimerConfig();
                              timerConfig.setPersistent(true);
                              timerConfig.setInfo(taskName);
                              Timer timer = this.timerService.createCalendarTimer(calendarSchedule, timerConfig);
                         }
                    
                    }
                    


                    Hope it helps a bit


                    Pieter