Nov 20, 2009

Asynchronous mailing through Tomcat6 and Seam 2.1.2

This article assumes Seam application is already configured on Tomcat.

Steps to implement Asynchronous mailing

  • Copy following mail related jars from jboss-seam-2.1.2\lib to the projects’ \WEB-INF\lib folder.
    1. Jboss-seam-mail.jar
    2. Mail.jar
    3. Activation.jar
  • Copy quartz.jar file from jboss-seam-2.1.2\lib to the projects’ \WEB-INF\lib folder.
Modify component.xml file is as described below.
<?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:security="http://jboss.com/products/seam/security"
xmlns:transaction="http://jboss.com/products/seam/transaction"
xmlns:mail="http://jboss.com/products/seam/mail"
xmlns:async="http://jboss.com/products/seam/async"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.1.xsd
http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.1.xsd
http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.1.xsd
http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.1.xsd
http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.1.xsd
http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.1.xsd
http://jboss.com/products/seam/async http://jboss.com/products/seam/async-2.1.xsd"

>

<core:manager conversation-timeout="120000"
concurrent-request-timeout="500"
conversation-id-parameter="cid"/>

<persistence:hibernate-session-factory name="hibernateSessionFactory"/>

<persistence:managed-hibernate-session name="hbSession" session-factory="#{hibernateSessionFactory}" auto-create="true"/>

<security:identity authenticate-method="#{authenticator.authenticate}"/>
<transaction:hibernate-transaction session="#{hbSession}"/>

<component name="org.jboss.seam.core.init">
<property name="debug">true</property>
</component>

<mail:mail-session host="smtp.gmail.com" port="587" username="emaill@gmail.com" password="pswd"/>

<!-- Install the QuartzDispatcher -->
<async:quartz-dispatcher/>
</components>



  • Make sure seam.quartz.property file will be available in WEB-INF\classes folder. To do that you should put this file in projects’ src folder.



Sample seam.quartz.property:



#=============================================================
# Configure Main Scheduler Properties
#=============================================================

org.quartz.scheduler.instanceName Sched1
org.quartz.scheduler.instanceId AUTO
org.quartz.scheduler.rmi.export false
org.quartz.scheduler.rmi.proxy false

#=============================================================
# Configure ThreadPool
#=============================================================

org.quartz.threadPool.class org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount 3

#=============================================================
# Configure JobStore
#=============================================================

org.quartz.jobStore.misfireThreshold 60000
org.quartz.jobStore.class org.quartz.simpl.RAMJobStore



  • Create WebContent\email folder and place following message template (activatead.xhtml) into it.



<m:message xmlns="http://www.w3.org/1999/xhtml"
xmlns:m="http://jboss.com/products/seam/mail"
xmlns:s="http://jboss.com/products/seam/taglib">
<m:header name="X-Composed-By" value="xtimes.com"/>
<m:from name="xtimes!" address="members@xtimes.com" />
<m:replyTo address="noreply@xtimes.com"/>
<m:to>#{buysellrent.emailAdd}</m:to>
<m:subject>POST/EDIT/DELETE - #{buysellrent.title}</m:subject>
<m:body type="plain">
<s:div style="notes" styleClass="section">
IMPORTANT - YOU MUST TAKE FURTHER ACTION TO PUBLISH THIS
POST !!!
CLICK ON THE WEB ADDRESS BELOW TO PUBLISH, EDIT, OR DELETE THIS POSTING.
</s:div>
</m:body>
</m:message>



  • Create following asynchronous class to send an email.



@Name(“emailService”)
@AutoCreate
public class EmailService {
@Logger
private Log log;

@In(create = true)
private Renderer renderer;

@Asynchronous
public void sendMessage(@Duration long delay, BuySellRent buysellrent) {
log.info("inside send message");
try{
Contexts.getEventContext().set("buysellrent", buysellrent);
log.info("After setting context");
renderer.render("/email/activatead.xhtml");
}catch (Exception e) {
log.error("Error Email Send #0"+e.getMessage());
}
}
}


As shown sendMessage method will send email after delaying as per value of “delay” parameter. Also make sure you will set object to the context so that while sending  mail seam application can get hold of it and assigned values for required fields.




  • Here is sample BuySellRent class which is used as business object while sending mail.



public class BuySellRent implements java.io.Serializable {

private static final long serialVersionUID = 1L;
private String title;
private String emailAdd;

@Email
@Column(name = "EMAIL_ADD", nullable = false, length = 50)
public String getEmailAdd() {
return this.emailAdd;
}

public void setEmailAdd(String emailAdd) {
this.emailAdd = emailAdd;
}

@Column(name = "TITLE", nullable = false, length = 50)
public String getTitle() {
return this.title;
}

public void setTitle(String title) {
this.title = title;
}
}

0 comments:

Text Widget

Copyright © Vinay's Blog | Powered by Blogger

Design by | Blogger Theme by