3 Replies Latest reply on Jul 16, 2009 9:52 PM by jamathison

    Cant send emails out of my stateless component

    rufs
      Hi, i want to send reports from database as emails. I created a component EmailHandler that sends the mails..

      When i invoke the methods from a button out of a page

         <h:commandButton id="mailExample" value="send" action="#{emailHandler.sendEmail}"/>
         <h:commandButton id="mailExampleasync" value="sendasync" action="#{emailHandler.sendEmailAsync}"/>
                 

      everything works great and the emails are sent successfully.....


      but when i inject this EmailHandler into my Component that generates the reports, i always get the following exception

      any ideas??


      17:34:55,204 ERROR [EmailHandler] Error sending mail
      org.jboss.seam.InstantiationException: Could not instantiate Seam component: org.jboss.seam.ui.facelet.facesContextFactory
              at org.jboss.seam.Component.newInstance(Component.java:2106)
              at org.jboss.seam.Component.getInstance(Component.java:1988)
              at org.jboss.seam.Component.getInstance(Component.java:1967)
              at org.jboss.seam.Component.getInstance(Component.java:1961)
              at org.jboss.seam.Component.getInstance(Component.java:1934)
              at org.jboss.seam.Component.getInstance(Component.java:1929)
              at org.jboss.seam.ui.facelet.RendererFacesContextFactory.instance(RendererFacesContextFactory.java:72)
              at org.jboss.seam.ui.facelet.RendererRequest.init(RendererRequest.java:45)
              at org.jboss.seam.ui.facelet.RendererRequest.run(RendererRequest.java:72)
              at org.jboss.seam.ui.facelet.FaceletsRenderer.render(FaceletsRenderer.java:43)
              at org.domain.testspider.session.EmailHandler.sendEmailAsync(EmailHandler.java:43)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:597)
              at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
              at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
              at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:77)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.bpm.BusinessProcessInterceptor.aroundInvoke(BusinessProcessInterceptor.java:51)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.async.AsynchronousInterceptor.aroundInvoke(AsynchronousInterceptor.java:52)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
              at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
              at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
              at org.domain.testspider.session.EmailHandler_$$_javassist_2.sendEmailAsync(EmailHandler_$$_javassist_2.java)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:597)
              at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
              at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144)
              at org.jboss.seam.async.AsynchronousInvocation$1.process(AsynchronousInvocation.java:62)
              at org.jboss.seam.async.Asynchronous$ContextualAsynchronousRequest.run(Asynchronous.java:80)
              at org.jboss.seam.async.AsynchronousInvocation.execute(AsynchronousInvocation.java:44)
              at org.jboss.seam.async.ThreadPoolDispatcher$RunnableAsynchronous.run(ThreadPoolDispatcher.java:136)
              at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
              at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
              at java.util.concurrent.FutureTask.run(FutureTask.java:138)
              at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
              at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:207)
              at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
              at java.lang.Thread.run(Thread.java:619)
      Caused by: java.lang.IllegalStateException: Application was not properly initialized at startup, could not find Factory: javax.faces.context.FacesContextFactory
              at javax.faces.FactoryFinder$FactoryManager.getFactory(FactoryFinder.java:725)
              at javax.faces.FactoryFinder.getFactory(FactoryFinder.java:239)
              at org.jboss.seam.ui.facelet.RendererFacesContextFactory.create(RendererFacesContextFactory.java:47)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:597)
              at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
              at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144)
              at org.jboss.seam.Component.callComponentMethod(Component.java:2211)
              at org.jboss.seam.Component.callCreateMethod(Component.java:2134)
              at org.jboss.seam.Component.newInstance(Component.java:2094)
              ... 49 more






      my class..

      @Name("emailHandler")
      @Scope(ScopeType.APPLICATION)
      @Startup
      public class EmailHandler
      {
        
         @Logger
         private Log log;
        
         @In
         private FacesMessages facesMessages;
        
         @In
         private Renderer renderer;
        

         public void sendEmail() {
            try {
              renderer.render("/messages/simple.xhtml");
              facesMessages.add("Email sent successfully");
            } catch (Exception e) {
               log.error("Error sending mail", e);
               facesMessages.add("Could not send the Email " + e.getMessage());
            }
         }
        
         @Asynchronous
         public void sendEmailAsync() {
                    try {
                      renderer.render("/messages/simple.xhtml");
                      facesMessages.add("Email sent successfully");
                    } catch (Exception e) {
                       log.error("Error sending mail", e);
                       facesMessages.add("Could not send the Email " + e.getMessage());
                    }
                 }
        


      @Stateless
      @Name("searchResultSender")
      public class SearchResultSenderBean implements SearchResultSender {
              @Logger
              private Log log;

              @In
              StatusMessages statusMessages;

              @PersistenceContext
              EntityManager entityManager;
             
              @In
              EmailHandler emailHandler;

              @SuppressWarnings("unchecked")
              public void searchResultSender() {
                     
                      log.info("searchResultSender.searchResultSender() action called");
                      statusMessages.add("searchResultSender");

                      // get unsent resultsets

                      List<ResultSet> resultList =entityManager.createQuery(
                                      "select r from ResultSet r where r.sent = false ")
                                      .getResultList();
                     
                     
                     
                      if(resultList.size()>0){
                             
                              for (ResultSet resultSet : resultList) {
                                     
             
                                      List<KeywordResult> resultlist =  resultSet.getKeywordResultList();
                                     
                                      for (KeywordResult keywordResult : resultlist) {
                                             
                                              System.out.println("Keyword: "+keywordResult.getKeyword().getKeywordtext());
                                             
                                              List<Htmldata> htmldatalist =  keywordResult.getHtmldatalist();
                                             
                                              for (Htmldata htmldata : htmldatalist) {
                                                     
                                                      System.out.println("\u0009 Site Found: "+htmldata.getUrl());
                                                     
                                              }
                                      }
                                     
                                      emailHandler.sendEmailAsync();

                              }
                             
                             
             
                      }
                     

              }

              // add additional action methods

      }
        • 1. Re: Cant send emails out of my stateless component
          cash1981

          If you look at this blog it was very helpful for me:


          http://gariboldi.blogspot.com/2007/12/seam-and-asynchronou.html


          Your problem is probably that you havent set the component in the asynchronous scope. You must remember that you cannot retrieve the seam components the normal way when running asychronous.
          So you will need to set it manually in the asynch method:



          Contexts.getConversationContext().set("account", account);





          Also remember to update components.xml



          <async:quartz-dispatcher/>




          • 2. Re: Cant send emails out of my stateless component
            jamathison

            Did you find a solution to this?


            I'm getting this exact same exception, only it's when I call my email handler from a webservice, implemented as a SLSB. It kindof make sense that there is no FacesContext available since it's a non-faces request.


            I've stepped into Render.render, and into RendererRequest.init() where it calls RendererFacesContextFactory.instance() which looks in the Application context for an instance of itself, which fails.


            I can't find where the Application context is primed with an instance of RendererFacesContextFactory however, and am stuck...


            • 3. Re: Cant send emails out of my stateless component
              jamathison

              Actually, I see that the RendererFacesContextFactory is indeed being instantiated, and its' @Create method is called, which in turn calls javax.faces.FactoryFinder.getFactory which throws an IllegalStateException...  which I would've seen if I'd looked at the stack trace more carefully...