Seam Asynchronous Email
From Foochal
Contents |
[edit]
Write an async mailer class
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.async.Asynchronous;
import org.jboss.seam.annotations.async.Duration;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.faces.Renderer;
@Name("emailService")
@AutoCreate
public class EmailService {
private static final Log logger = LogFactory.getLog(EmailService.class);
@In(create = true)
private Renderer renderer;
@Asynchronous
public void sendMessage(@Duration
long delay, String template, Object infoNeededForTemplate) {
try {
Contexts.getEventContext().set("info", infoNeededForTemplate);
renderer.render(template);
} catch (Exception e) {
// suppress information.
logger.error(e.getMessage());
}
}
}
[edit]
Call the mailer class
@In
private EmailService emailService;
public void takeAction() {
// do something
// ...
// send email asynchrously
emailService.sendMessage(500, "location-of-template.xhtml");
}
[edit]
Common errors
[edit]
could not create Component org.jboss.seam.async.dispatcher
Exception sending context initialized event to listener instance of class org.jboss.seam.servlet.SeamListener java.lang.RuntimeException: Could not create Component: org.jboss.seam.async.dispatcher at org.jboss.seam.init.Initialization.addComponent(Initialization.java:989) at org.jboss.seam.init.Initialization.installComponents(Initialization.java:911) at org.jboss.seam.init.Initialization.init(Initialization.java:589) at org.jboss.seam.servlet.SeamListener.contextInitialized(SeamListener.java:34) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3856) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4361) ... Caused by: java.lang.IllegalArgumentException: You must specify org.jboss.seam.core.init.jndiPattern or use @JndiName: org.jboss.seam.async.dispatcher at org.jboss.seam.Component.getJndiName(Component.java:438) at org.jboss.seam.Component.<init>(Component.java:243) at org.jboss.seam.Component.<init>(Component.java:217) at org.jboss.seam.init.Initialization.addComponent(Initialization.java:974)
[edit]
Cause
This error occurs if you specify the following in components.xml
<async:timer-service-dispatcher/>
[edit]
Solution
Remove the above line from components.xml
[edit]
name not bound
Caused by: javax.faces.el.EvaluationException: org.jboss.seam.InstantiationException: Could not instantiate Seam component: org.jboss.seam.async.dispatcher at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:91) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91) ... 42 more Caused by: org.jboss.seam.InstantiationException: Could not instantiate Seam component: org.jboss.seam.async.dispatcher at org.jboss.seam.Component.newInstance(Component.java:1986) at org.jboss.seam.Component.getInstance(Component.java:1876) ... 43 more Caused by: javax.naming.NameNotFoundException: foochal not bound at org.jnp.server.NamingServer.getBinding(NamingServer.java:529) at org.jnp.server.NamingServer.getBinding(NamingServer.java:537) at org.jnp.server.NamingServer.getObject(NamingServer.java:543)

