3 Replies Latest reply on Jan 16, 2011 7:40 PM by Alceu Medeiros

    Tomcat, Seam and Scheduling

    Israel Fonseca Apprentice

      I want to send emails at determined time intervals (everey dat at hour X). How can i do that with Seam and Tomcat.


      Is there any easy way?


      Thks,


      Israel

        • 1. Re: Tomcat, Seam and Scheduling
          Clint Popetz Apprentice

          Israel Fonseca wrote on May 22, 2009 18:27:


          I want to send emails at determined time intervals (everey dat at hour X). How can i do that with Seam and Tomcat.

          Is there any easy way?

          Thks,

          Israel


          For cron-like asynch jobs in seam you can use quartz and for email you'll need to add javamail to your classpath (tomcat doesn't bundle it by default) and add a resource and resource-ref to your tomcat web.xml and server.xml to expose it via jndi, and configure seam to use it in components.xml:


               <mail:mail-session session-jndi-name="java:comp/env/mail/Session"/>
          




          Then you can use the javamail api from within an @Asynchronous method with a @IntervalDuration parameter, as described in the above quartz link.  You'll want to add an @Observer("org.jboss.seam.postInitialization") on a method in an application-scoped class, which will then invoke your @Asynchronous method so that it starts repeating on application startup.

          • 2. Re: Tomcat, Seam and Scheduling
            Israel Fonseca Apprentice

            Thks for the answer Clint, i will try this later.


            Israel

            • 3. Re: Tomcat, Seam and Scheduling
              Alceu Medeiros Newbie

              Clint Popetz wrote on May 22, 2009 19:43:


              For cron-like asynch jobs in seam you can use quartz and for email you'll need to add javamail to your classpath (tomcat doesn't bundle it by default) and add a resource and resource-ref to your tomcat web.xml and server.xml to expose it via jndi, and configure seam to use it in components.xml:

                   <mail:mail-session session-jndi-name="java:comp/env/mail/Session"/>
              




              Hi Clint,


              I'm facing a issue with similar scenario (without asynch jobs): when I call Renderer.instance().render(/templates/mail/welcome.xhtml) it doesn't send e-mail neither throws an exception.


              My /templates/mail/welcome.xhtml is like this:


              <m:message xmlns="http://www.w3.org/1999/xhtml"
                   xmlns:m="http://jboss.com/products/seam/mail"
                   xmlns:h="http://java.sun.com/jsf/html" importance="high" >
              
                   <m:from name="noreply@myapp.fakecom" address="noreply@myapp.fakecom" />
                   <m:to name="#{profile.name}">#{profile.email}</m:to>
                   <m:subject>Welcome to My App!</m:subject>
                   <m:body type="plain">
                        <p><h:outputText value="Dear #{profile.name}" />,</p>
                        <p>Welcome! You can try out My App by visiting <a
                             href="http://www.myapp.com/fakeurl">http://www.myapp.com/fakeurl</a>.</p>
                        <p>Regards,</p>
                        <p>My App's Team.</p>
                   </m:body>
              </m:message>
              



              My sendEmail method is like this:


              public void sendWelcomeMail(Profile newProfile) {
                      Contexts.getEventContext().set("profile", newProfile);
                      Renderer.instance().render("/templates/mail/welcome.xhtml");
              }
              



              My /WEB-INF/components.xml is:


              <?xml version="1.0" encoding="UTF-8"?>
              <components xmlns="http://jboss.com/products/seam/components"
                   xmlns:core="http://jboss.com/products/seam/core" xmlns:persistence="http://jboss.com/products/seam/persistence"
                   xmlns:drools="http://jboss.com/products/seam/drools" xmlns:bpm="http://jboss.com/products/seam/bpm"
                   xmlns:security="http://jboss.com/products/seam/security" xmlns:mail="http://jboss.com/products/seam/mail"
                   xmlns:async="http://jboss.com/products/seam/async" xmlns:remoting="http://jboss.com/products/seam/remoting"
                   xmlns:resteasy="http://jboss.com/products/seam/resteasy" xmlns:web="http://jboss.com/products/seam/web"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:transaction="http://jboss.com/products/seam/transaction"
                   xsi:schemaLocation="http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.2.xsd
                               http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.2.xsd
                               http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.2.xsd
                               http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.2.xsd
                               http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.2.xsd
                               http://jboss.com/products/seam/resteasy http://jboss.com/products/seam/resteasy-2.2.xsd
                               http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd
                               http://jboss.com/products/seam/async http://jboss.com/products/seam/async-2.2.xsd
                               http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.2.xsd
                               http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd
                               http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.2.xsd
                               http://jboss.com/products/seam/remoting http://jboss.com/products/seam/remoting-2.2.xsd">
              
                   <core:init debug="true" jndi-pattern="@jndiPattern@" />
              
                   <!-- (...) other stuff, please tell me if it's needed -->
              
                   <mail:mail-session debug="true" session-jndi-name="java:comp/env/mail/Session" />
              
              </components>
              



              Because I use Tomcat 6, following this HOWTO my /WEB-INF/web.xml is like this:


              <?xml version="1.0" encoding="UTF-8"?>
              <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                   id="WebApp_ID" version="2.5">
                   <display-name>bysecret</display-name>
                   <welcome-file-list>
                        <welcome-file>index.html</welcome-file>
                        <welcome-file>index.htm</welcome-file>
                        <welcome-file>index.shtm</welcome-file>
                        <welcome-file>index.jsp</welcome-file>
                        <welcome-file>default.html</welcome-file>
                        <welcome-file>default.htm</welcome-file>
                        <welcome-file>default.jsp</welcome-file>
                   </welcome-file-list>
                   <servlet>
                        <servlet-name>Faces Servlet</servlet-name>
                        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
                        <load-on-startup>1</load-on-startup>
                   </servlet>
                   <session-config>
                        <session-timeout>30</session-timeout>
                   </session-config>
                   <servlet-mapping>
                        <servlet-name>Faces Servlet</servlet-name>
                        <url-pattern>*.seam</url-pattern>
                   </servlet-mapping>
                   <context-param>
                        <param-name>org.richfaces.SKIN</param-name>
                        <param-value>blueSky</param-value>
                   </context-param>
                   <listener>
                        <listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
                   </listener>
                   <filter>
                        <display-name>RichFaces Filter</display-name>
                        <filter-name>richfaces</filter-name>
                        <filter-class>org.ajax4jsf.Filter</filter-class>
                   </filter>
              
                   <filter-mapping>
                        <filter-name>richfaces</filter-name>
                        <servlet-name>Faces Servlet</servlet-name>
                        <dispatcher>REQUEST</dispatcher>
                        <dispatcher>FORWARD</dispatcher>
                        <dispatcher>INCLUDE</dispatcher>
                   </filter-mapping>
              
                   <filter>
                        <filter-name>Seam Filter</filter-name>
                        <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
                   </filter>
              
                   <filter-mapping>
                        <filter-name>Seam Filter</filter-name>
                        <url-pattern>/*</url-pattern>
                   </filter-mapping>
              
                   <servlet>
                        <servlet-name>Seam Resource Servlet</servlet-name>
                        <servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
                   </servlet>
                   <servlet-mapping>
                        <servlet-name>Seam Resource Servlet</servlet-name>
                        <url-pattern>/seam/resource/*</url-pattern>
                   </servlet-mapping>
                   <context-param>
                        <param-name>facelets.DEVELOPMENT</param-name>
                        <param-value>true</param-value>
                   </context-param>
                   <context-param>
                        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
                        <param-value>.xhtml</param-value>
                   </context-param>
                   <security-constraint>
                        <display-name>Restrict raw XHTML Documents</display-name>
                        <web-resource-collection>
                             <web-resource-name>XHTML</web-resource-name>
                             <url-pattern>*.xhtml</url-pattern>
                        </web-resource-collection>
                        <auth-constraint />
                   </security-constraint>
                   <resource-ref>
                        <res-ref-name>jdbc/myDatasource</res-ref-name>
                        <res-type>javax.sql.DataSource</res-type>
                        <res-auth>Container</res-auth>
                   </resource-ref>
                   <resource-ref>
                        <res-ref-name>mail/Session</res-ref-name>
                        <res-type>javax.mail.Session</res-type>
                        <res-auth>Container</res-auth>
                        <res-sharing-scope>Shareable</res-sharing-scope>
                   </resource-ref>
              </web-app>
              



              ... And my /META-INF/context.xml is like this:


              <Context>
                   <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
                        maxActive="20" maxIdle="10" maxWait="100" name="jdbc/myDatasource"
                        type="javax.sql.DataSource" username="me" password="myPassword"
                        url="jdbc:mysql://localhost/myDb" />
              
                   <Resource name="mail/Session" auth="Container" type="javax.mail.Session" 
                        mail.smtp.host="localhost" />
              
              </Context>
              



              Because is not mentioned in HOWTO, I think that add a resource in server.xml as you mentioned is no longer needed in Tomcat 6, so I didn't. My console shows a LOT of prints, but no one single line about this configuration, neither about some exception, warning or even debug lines about SMTP connection or sending mail headers data. My log4j.xml is like this:


              <?xml version="1.0" encoding="UTF-8"?>
              <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
              
              <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
                   debug="true">
              
                   <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
              
                        <param name="Target" value="System.out" />
              
                        <layout class="org.apache.log4j.PatternLayout">
                             <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c{1}] %m%n" />
                        </layout>
                   </appender>
                   <appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
              
                        <param name="File" value="${catalina.base}/logs/yourwebappname.log" />
              
                        <param name="DatePattern" value="'.'yyyy-MM-dd" />
                        <layout class="org.apache.log4j.PatternLayout">
                             <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c{1}] %m%n" />
                        </layout>
                   </appender>
              
                   <root>
                        <priority value="DEBUG" />
              
                        <appender-ref ref="FILE" />
                        <appender-ref ref="CONSOLE" />
                   </root>
              
                   <category name="org.jboss.seam">
                         <priority value="DEBUG"/>
                   </category>
                   
                   <category name="org.jboss.seam.mail">
                         <priority value="DEBUG"/>
                   </category>
              
                   <category name="javax.mail">
                         <priority value="DEBUG"/>
                   </category>
              
                   <category name="javax.persistence">
                         <priority value="DEBUG"/>
                   </category>
              
                   <category name="org.jboss.seam.resteasy">
                         <priority value="DEBUG"/>
                   </category>
              </log4j:configuration>
              



              I'm using maven2 and my pom.xml is like this:


              <?xml version="1.0" encoding="UTF-8"?>
              <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
                   <modelVersion>4.0.0</modelVersion>
                   <groupId>br.com.outboxer</groupId>
                   <artifactId>myApp</artifactId>
                   <version>0.0.1</version>
                   <packaging>war</packaging>
                   <name>myApp</name>
                   <build>
                        <extensions>
                             <extension>
                                  <groupId>org.apache.maven.archetype</groupId>
                                  <artifactId>archetype-packaging</artifactId>
                                  <version>2.0-alpha-4</version>
                             </extension>
                        </extensions>
                        <plugins>
                             <plugin>
                                  <groupId>org.codehaus.mojo</groupId>
                                  <artifactId>tomcat-maven-plugin</artifactId>
                                  <version>1.0-beta-1</version>
                                  <configuration>
                                       <charset>utf-8</charset>
                                       <username>support</username>
                                       <password></password>
                                  </configuration>
                                  <dependencies>
                                       <dependency>
                                            <groupId>mysql</groupId>
                                            <artifactId>mysql-connector-java</artifactId>
                                            <version>5.1.12</version>
                                       </dependency>
                                       <dependency>
                                            <groupId>javax.mail</groupId>
                                            <artifactId>mail</artifactId>
                                            <version>1.4.2</version>
                                       </dependency>
                                  </dependencies>
                             </plugin>
                             <plugin>
                                  <groupId>org.apache.maven.plugins</groupId>
                                  <artifactId>maven-compiler-plugin</artifactId>
                                  <version>2.0.2</version>
                                  <configuration>
                                       <source>1.6</source>
                                       <target>1.6</target>
                                       <encoding>utf-8</encoding>
                                  </configuration>
                             </plugin>
                             <plugin>
                                  <artifactId>maven-archetype-plugin</artifactId>
                                  <version>2.0-alpha-4</version>
                                  <extensions>true</extensions>
                             </plugin>
                        </plugins>
                   </build>
                   <dependencies>
                        <dependency>
                             <groupId>com.sun.jersey</groupId>
                             <artifactId>jersey-client</artifactId>
                             <version>${jersey.version}</version>
                        </dependency>
              
                        <dependency>
                             <groupId>com.sun.jersey</groupId>
                             <artifactId>jersey-core</artifactId>
                             <version>${jersey.version}</version>
                        </dependency>
              
                        <dependency>
                             <groupId>com.sun.jersey</groupId>
                             <artifactId>jersey-json</artifactId>
                             <version>${jersey.version}</version>
                        </dependency>
              
                        <dependency>
                             <groupId>com.sun.jersey</groupId>
                             <artifactId>jersey-server</artifactId>
                             <version>${jersey.version}</version>
                        </dependency>
              
                        <dependency>
                             <groupId>org.jboss.resteasy</groupId>
                             <artifactId>resteasy-jaxrs</artifactId>
                             <version>${resteasy.version}</version>
                             <exclusions>
                                  <exclusion>
                                       <groupId>javax.el</groupId>
                                       <artifactId>el-api</artifactId>
                                  </exclusion>
                             </exclusions>
                        </dependency>
              
                        <dependency>
                             <groupId>org.jboss.resteasy</groupId>
                             <artifactId>resteasy-multipart-provider</artifactId>
                             <version>${resteasy.version}</version>
                             <exclusions>
                                  <exclusion>
                                       <groupId>javax.el</groupId>
                                       <artifactId>el-api</artifactId>
                                  </exclusion>
                                  <exclusion>
                                       <groupId>javax.mail</groupId>
                                       <artifactId>mail</artifactId>
                                  </exclusion>
                             </exclusions>
                        </dependency>
              
                        <dependency>
                             <groupId>org.jboss.resteasy</groupId>
                             <artifactId>resteasy-jettison-provider</artifactId>
                             <version>${resteasy.version}</version>
                             <exclusions>
                                  <exclusion>
                                       <groupId>javax.el</groupId>
                                       <artifactId>el-api</artifactId>
                                  </exclusion>
                             </exclusions>
                        </dependency>
              
                        <dependency>
                             <groupId>org.jboss.resteasy</groupId>
                             <artifactId>resteasy-jaxb-provider</artifactId>
                             <version>${resteasy.version}</version>
                             <exclusions>
                                  <exclusion>
                                       <groupId>javax.el</groupId>
                                       <artifactId>el-api</artifactId>
                                  </exclusion>
                             </exclusions>
                        </dependency>
              
                        <dependency>
                             <groupId>org.jboss.resteasy</groupId>
                             <artifactId>resteasy-atom-provider</artifactId>
                             <version>${resteasy.version}</version>
                             <exclusions>
                                  <exclusion>
                                       <groupId>javax.el</groupId>
                                       <artifactId>el-api</artifactId>
                                  </exclusion>
                             </exclusions>
                        </dependency>
              
                        <dependency>
                             <groupId>org.jboss.resteasy</groupId>
                             <artifactId>jaxrs-api</artifactId>
                             <version>${resteasy.version}</version>
                             <exclusions>
                                  <exclusion>
                                       <groupId>javax.el</groupId>
                                       <artifactId>el-api</artifactId>
                                  </exclusion>
                             </exclusions>
                        </dependency>
              
                        <dependency>
                             <groupId>org.jboss.seam</groupId>
                             <artifactId>jboss-seam-resteasy</artifactId>
                             <version>${seam.version}</version>
                             <exclusions>
                                  <exclusion>
                                       <groupId>javax.el</groupId>
                                       <artifactId>el-api</artifactId>
                                  </exclusion>
                             </exclusions>
                        </dependency>
              
                        <dependency>
                             <groupId>junit</groupId>
                             <artifactId>junit</artifactId>
                             <version>4.5</version>
                             <scope>test</scope>
                        </dependency>
                        <dependency>
                             <groupId>javax.faces</groupId>
                             <artifactId>jsf-api</artifactId>
                             <version>1.2_08</version>
                        </dependency>
                        <dependency>
                             <groupId>javax.servlet</groupId>
                             <artifactId>servlet-api</artifactId>
                             <version>2.5</version>
                        </dependency>
                        <dependency>
                             <groupId>org.hibernate</groupId>
                             <artifactId>hibernate-validator</artifactId>
                             <version>3.1.0.GA</version>
                        </dependency>
                        <dependency>
                             <groupId>org.hibernate</groupId>
                             <artifactId>hibernate-search</artifactId>
                             <version>3.1.0.GA</version>
                        </dependency>
                        <dependency>
                             <groupId>org.hibernate</groupId>
                             <artifactId>hibernate-entitymanager</artifactId>
                             <version>3.4.0.GA</version>
                        </dependency>
                        <dependency>
                             <groupId>org.jboss.seam</groupId>
                             <artifactId>jboss-seam-ui</artifactId>
                             <version>${seam.version}</version>
                             <exclusions>
                                  <exclusion>
                                       <artifactId>el-api</artifactId>
                                       <groupId>javax.el</groupId>
                                  </exclusion>
                             </exclusions>
                        </dependency>
                        <dependency>
                             <groupId>javax.faces</groupId>
                             <artifactId>jsf-impl</artifactId>
                             <version>1.2_08</version>
                        </dependency>
                        <dependency>
                             <groupId>com.sun.facelets</groupId>
                             <artifactId>jsf-facelets</artifactId>
                             <version>1.1.14</version>
                        </dependency>
                        <dependency>
                             <groupId>org.jboss.seam</groupId>
                             <artifactId>jboss-seam-debug</artifactId>
                             <version>${seam.version}</version>
                        </dependency>
                        <dependency>
                             <groupId>javassist</groupId>
                             <artifactId>javassist</artifactId>
                             <version>3.8.0.GA</version>
                        </dependency>
                        <dependency>
                             <groupId>org.slf4j</groupId>
                             <artifactId>slf4j-nop</artifactId>
                             <version>1.5.8</version>
                        </dependency>
                        <dependency>
                             <groupId>org.slf4j</groupId>
                             <artifactId>slf4j-api</artifactId>
                             <version>1.5.8</version>
                        </dependency>
                        <dependency>
                             <groupId>org.slf4j</groupId>
                             <artifactId>slf4j-log4j12</artifactId>
                             <version>1.5.8</version>
                        </dependency>
                        <dependency>
                             <groupId>log4j</groupId>
                             <artifactId>log4j</artifactId>
                             <version>${log4j.version}</version>
                        </dependency>
              
                        <!-- Richfaces dependencies -->
                        <dependency>
                             <groupId>org.richfaces.framework</groupId>
                             <artifactId>richfaces-api</artifactId>
                             <version>${richfaces.version}</version>
                        </dependency>
                        <dependency>
                             <groupId>org.richfaces.framework</groupId>
                             <artifactId>richfaces-impl</artifactId>
                             <version>${richfaces.version}</version>
                             <exclusions>
                                  <exclusion>
                                       <groupId>javax.faces</groupId>
                                       <artifactId>jsf-api</artifactId>
                                  </exclusion>
                                  <exclusion>
                                       <groupId>javax.faces</groupId>
                                       <artifactId>jsf-impl</artifactId>
                                  </exclusion>
                             </exclusions>
                        </dependency>
                        <dependency>
                             <groupId>org.richfaces.ui</groupId>
                             <artifactId>richfaces-ui</artifactId>
                             <version>${richfaces.version}</version>
                             <exclusions>
                                  <exclusion>
                                       <groupId>javax.faces</groupId>
                                       <artifactId>jsf-api</artifactId>
                                  </exclusion>
                                  <exclusion>
                                       <groupId>javax.faces</groupId>
                                       <artifactId>jsf-impl</artifactId>
                                  </exclusion>
                             </exclusions>
                        </dependency>
                        <dependency>
                             <groupId>javax.persistence</groupId>
                             <artifactId>persistence-api</artifactId>
                             <version>1.0</version>
                        </dependency>
                        <dependency>
                             <groupId>org.drools</groupId>
                             <artifactId>drools-core</artifactId>
                             <version>${drools.version}</version>
                        </dependency>
                        <dependency>
                             <groupId>org.drools</groupId>
                             <artifactId>drools-compiler</artifactId>
                             <version>${drools.version}</version>
                             <exclusions>
                                  <exclusion>
                                       <artifactId>xercesImpl</artifactId>
                                       <groupId>xerces</groupId>
                                  </exclusion>
                             </exclusions>
                        </dependency>
                        <dependency>
                             <groupId>cglib</groupId>
                             <artifactId>cglib-nodep</artifactId>
                             <version>2.2</version>
                        </dependency>
                        <dependency>
                             <groupId>asm</groupId>
                             <artifactId>asm</artifactId>
                             <version>3.2</version>
                        </dependency>
                        <dependency>
                             <groupId>asm</groupId>
                             <artifactId>asm-commons</artifactId>
                             <version>3.2</version>
                        </dependency>
                        <dependency>
                             <groupId>commons-fileupload</groupId>
                             <artifactId>commons-fileupload</artifactId>
                             <version>1.2.2</version>
                        </dependency>
                        <dependency>
                             <groupId>commons-io</groupId>
                             <artifactId>commons-io</artifactId>
                             <version>1.4</version>
                        </dependency>
                        <dependency>
                             <groupId>hsqldb</groupId>
                             <artifactId>hsqldb</artifactId>
                             <version>1.7.3.3</version>
                        </dependency>
                        <dependency>
                             <groupId>mysql</groupId>
                             <artifactId>mysql-connector-java</artifactId>
                             <version>5.1.12</version>
                             <scope>provided</scope>
                        </dependency>
                        <dependency>
                             <groupId>net.sourceforge.jtds</groupId>
                             <artifactId>jtds</artifactId>
                             <version>1.2.2</version>
                        </dependency>
                   </dependencies>
                   <properties>
                        <jersey.version>1.0.3</jersey.version>
                        <richfaces.version>3.3.3.Final</richfaces.version>
                        <log4j.version>1.2.9</log4j.version>
                        <seam.version>2.2.0.GA</seam.version>
                        <resteasy.version>2.0-beta-4</resteasy.version>
                        <drools.version>5.0.1</drools.version>
                   </properties>
              </project>
              
              



              Can you (or anyone) help me, please? I think it can be some missed part of JNDI and/or richfaces/facelets configuration. If I missed (or omited) some important code to figure this out, please let me know.


              Thanks in advance.
              Best Regards.