Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hantsy/seam3-mail-demo

Demotrate how to use Seam3 mail module in Java EE 6 application.
https://github.com/hantsy/seam3-mail-demo

Last synced: 3 days ago
JSON representation

Demotrate how to use Seam3 mail module in Java EE 6 application.

Awesome Lists containing this project

README

        

#Send email with Seam 3 Mail and JMS

JavaLobby post link [http://java.dzone.com/articles/send-email-seam-3-mail-and-jms](http://java.dzone.com/articles/send-email-seam-3-mail-and-jms)

Seam 3 Mail module provides simple API to use Java Mail API to send email message.

##Basic Configuration

Assume you have already created a Maven based Java EE 6 application.

Add seam mail dependency to your *pom.xml* file.


org.jboss.seam.mail
seam-mail-api
compile


org.jboss.seam.mail
seam-mail
compile


Add basic mail configuration in your *META-INF/seam-beans.xml*.





In your Java codes, inject `MailMessage` and `Session`.

@Inject
private transient Instance mailMessage;

@Inject
private transient Instance session;

`MailMessage` is provided by Seam3 module, it is a fluid API to build message object and send message. `Session` is from the standard Java Mail API.

MailMessage msg = mailMessage.get();
msg.subject("test subject")
.from("[email protected]")
.to("[email protected]")
.send();

Everything works well.

But if you try to send an email from jsf pages, the page will be blocked when the email is being sent.

EJB 3.1 provides a simple way to execute asynchronous action. You can simply create a `@Stateless` EJB and put the logic in a `@Asynchronous` method. But unfortunately when you try to use @Asynchronous and CDI together, it does not work.

JMS provides standard asynchronous processing capability for Java EE, Seam3 also includes a JMS module.

##Send email asynchronously with JMS

Seam 3 also provides a JMS module which simplify JMS and CDI integration, we can utilize JMS to process the asynchronous work.

Configure the JMS connection factory in your *META-INF/seam-beans.xml*.




java:/ConnectionFactory



Create a standard JMS listener to handle JMS message.

@MessageDriven(....)
public class MailProcessorMDB extends MessageListener {

@EJB MailProcessor processor;

public void send()...//

@Override
protected void onMessage(Message _msg) throws JMSException {
if (log.isDebugEnabled()) {
log.debug("call handleMessage...");
}

ObjectMessage objMessage = (ObjectMessage) _msg;
EmailMessage mailMessage = (EmailMessage) objMessage.getObject();
processor.send(mailMessage);
}
}

`MailProcessor` is a `@Stateless` EJB which is use for sending mail.

When a message is arrived, the listener will call `MailProcessor` to process email.

Use an interface to observe the CDI event and route it to the JMS queue.

@Outbound
public void mapStatusToQueue(@Observes @NoneBlocking EmailMessage message,
@JmsDestination(jndiName = "java:/queue/test") Queue q);

In the presentation layer, fire an event directly. The event object will be routed as the payload of JMS message automatically.

messageEventSrc.fire(message);


##Run the project

I assume you have installed the latest Oracle JDK 7, JBoss AS 7.1.1.Final and Apache Maven 3.0.4.

1. Check out the complete codes from github.com.

git clone git://github.com/hantsy/seam3-mail-demo.git

2. Start JBoss AS with standalone full profile which includes JMS support.

\bin\standalone.bat --server-config=standalone-full.xml

3. Deploy the application into the running JBoss AS.

mvn clean package jboss-as:deploy

4. Open your browser and go to http://localhost:8080/seam3-mail-demo.