[jboss-user] [JBoss Seam] - Re: Starting up Asynchronous Method at startup
damatrix
do-not-reply at jboss.com
Mon Aug 27 07:33:17 EDT 2007
Hi,
I tried your approach but had a problem which i expected will occur. Whiles seam is starting up, the contexts are not yet active. Since my email is made up of facelets templates, i could only send data to them when contexts are alive.
However i finally sorted it out.
|
| @Stateful
| @Scope(ScopeType.APPLICATION)
| @Name("startupManager")
| public class StartupManager implements StartupManagerLocal{ // create Interface
|
|
|
| @Create
| public void create() {
| TimerHandlerLocal timerHandler = (TimerHandlerLocal)Component.getInstance(TimerHandler.Class);
| timerHandler.sendNotifications;
| }
|
|
| @Remove
| public void destroy() {
| }
| }
|
The TimerHandler class:
| @Stateful
| @Name("timerhandler")
| public class TimerHandler implements TimerHandlerLocal { // create Interface
|
|
| @In
| private EntityManager entityManager;
|
| @In
| private MailRender mailRenderer;
|
| @Asynchronous
| @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
| public QuartzTriggerHandle createProcessing(String string) {
| List<Customer> customers = entityManager.createQuery("Select c
| from Customer where c.notification=true").getResultList();
| Context.getSessionContext().set("customers",customers);
| mailRenderer.sendNotification();
| Contexts.getSessionContxt().set("customers",null);
|
| return new QuartzTriggerHandle("notifications");
| }
|
Lastly, find somewhere to inject the StartupMonitor so that it starts up on its own. I think your login/authentication class is the best place.
| @Scope(ScopeType.SESSION)
| @Name("authenticator")
| public class Authenticator implements java.io.Serializable {
|
| @In(create=true)
| StartupMonitor startupMonitor
| ...
| }
|
Since the StartupMonitor is APPLICATION scoped, it's @Create method will be called when the first person logs into the application and will run until applications is redeployed.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4078307#4078307
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4078307
More information about the jboss-user
mailing list