4 Replies Latest reply on Jun 13, 2013 8:47 AM by baddeley84

    Quartz, Locale and Mail - FacesContext is null

    frankyb

      Hi,


      I'm trying to send e-mails from an Quartz scheduled process. The mails contain text from a resource bundle and must be sent in different languages. My classes are as follows:




      @Name("scheduleController")
      @AutoCreate
      public class ScheduleController implements java.io.Serializable { 
      
              @In 
              Processor processor;   
      
              public void scheduleTimer() {
                      
                  //run task every minute
                  String intervalLogs = "0 0/1";
                  processor.createLogs(new Date(), intervalLogs);
               }
      }






      @Name("processor")
      @AutoCreate
      @Scope(ScopeType.APPLICATION)
      public class Processor { 
          
              @In(create = true)
              private Renderer renderer;
      
      
              @In(create=true)
              LocaleSelector localeSelector;
      
              @Asynchronous
              @Transactional
              public QuartzTriggerHandle createLogs(@Expiration Date when, @IntervalCron String interval){
                
                //...
                //Code to get the Locale for this mail (country code is stored in a database
                //...
      
                //try to set locale and to send mail
                localeSelector.setLocale(locale);
                      
                //this is required to confirm/save the selection and force the reload of message bundles!
                localeSelector.select();  //THIS IS THE LINE WHERE IT BREAKS DOWN
      
                renderer.render("/mymail.xhtml");
              }
              
      }



      It crashes in org.jboss.seam.international.LocaleSelector.select(LocaleSelector.java:65) with a NullPointerException. You can view the source of LocaleSelector here:Click


      Obviously, the javax.faces.context.FacesContext.getCurrentInstance() returns null.


      What can I to?

        • 1. Re: Quartz, Locale and Mail - FacesContext is null
          kapitanpetko

          Inside your async method, you don't have access to session and conversation scopes. It doesn't run inside the JSF lifecyle,
          so no FacesContext either. What you can do is try to install your own Locale component and set it up to return the locale you read from the DB (Cf. org.jboss.seam.core.Locale. Then Seam's Messages component will use it and should load the right bundle.


          HTH


          • 2. Re: Quartz, Locale and Mail - FacesContext is null
            frankyb

            Thanks a lot for your response. Since injection works fine inside the async method, I thought there would be a FacesContext, too ;-)


            What exactly do you mean with install your own Locale?


            Do I have to create a Seam component with the name locale that inherits from org.jboss.seam.core.Locale?
            Well, I will try this right now!


            • 3. Re: Quartz, Locale and Mail - FacesContext is null
              kapitanpetko

              Frank Bitzer wrote on Dec 16, 2009 11:39:


              What exactly do you mean with install your own Locale?

              Do I have to create a Seam component with the name locale that inherits from org.jboss.seam.core.Locale?
              Well, I will try this right now!



              Yes. The name has to be org.jboss.seam.core.locale and you need to give it APPLICATION precedence.


              • 4. Re: Quartz, Locale and Mail - FacesContext is null
                baddeley84

                Sorry to dig up the dead, did you manage to get this working? I have the same problem (setting Locale for message bundles from a MDB with no FacesContext)....if so can you provide an example?? Thanks