[jboss-user] [JBoss Seam] - Re: Scheduling in Seam?

modoc do-not-reply at jboss.com
Sun Oct 29 22:16:00 EST 2006


Ok.  Here is the entire class in question.  Let me know if you need any other files..


  | package com.digitalsanctuary.seam;
  | 
  | import java.util.Collections;
  | import java.util.Date;
  | import java.util.HashMap;
  | import java.util.List;
  | import java.util.ListIterator;
  | import java.util.Map;
  | 
  | import javax.ejb.Remove;
  | 
  | import org.jboss.seam.ScopeType;
  | import org.jboss.seam.annotations.Asynchronous;
  | import org.jboss.seam.annotations.Create;
  | import org.jboss.seam.annotations.Destroy;
  | import org.jboss.seam.annotations.In;
  | import org.jboss.seam.annotations.Logger;
  | import org.jboss.seam.annotations.Name;
  | import org.jboss.seam.annotations.Scope;
  | import org.jboss.seam.annotations.Startup;
  | import org.jboss.seam.annotations.Synchronized;
  | import org.jboss.seam.annotations.timer.Expiration;
  | import org.jboss.seam.annotations.timer.IntervalDuration;
  | import org.jboss.seam.log.Log;
  | 
  | import com.digitalsanctuary.mail.IMAPClient;
  | import com.digitalsanctuary.mail.message.RenderableMessage;
  | 
  | @Name("emailManager")
  | @Scope(ScopeType.APPLICATION)
  | @Synchronized
  | @Startup
  | public class EmailManager {
  |     private int mEmailIndex;
  | 
  |     @Logger
  |     private Log mLog;
  | 
  |     @In(value = "IMAPClient", create = true)
  |     private IMAPClient mIMAPClient;
  | 
  |     @Asynchronous
  |     public void processEmailsRecurring(@Expiration
  |     Date pDate, @IntervalDuration
  |     long pInterval) {
  |         mLog.info("proccessEmailsReccurring running...");
  |         processNewEmails(mIMAPClient.getNewMessages());
  |     }
  | 
  |     /**
  |      * Map of client SessionMailQueues keyed by email address.
  |      */
  |     private Map<String, SessionMailQueue> mClientMap;
  | 
  |     @Create
  |     public void doStartService() {
  |         mLog.info("Starting up...");
  |         mClientMap = new HashMap<String, SessionMailQueue>();
  |         mLog.info("Kicking off recurring email processor.");
  |         processEmailsRecurring(new Date(), 2);
  |     }
  | 
  |     /**
  |      * Removes the email and it's associated client sessionmailqueue if it exists in the map.
  |      * 
  |      * @param pEmail
  |      *            the e-mail address to remove from the map.
  |      */
  |     public void removeEmail(String pEmail) {
  |         mLog.info("Removing email from Map:#0", pEmail);
  |         this.mClientMap.remove(pEmail);
  |     }
  | 
  |     /**
  |      * Generate a new email address.
  |      * 
  |      * @return the new e-mai address.
  |      */
  |     private String getNewEmailAddress() {
  |         // String newMail = "newMail" + this.mEmailIndex + "@digitalsanctuary.com";
  |         String newMail = "test at digitalsanctuary.com";
  |         mLog.info("Created new email:#0", newMail);
  |         mEmailIndex = mEmailIndex + 1;
  |         return newMail;
  |     }
  | 
  |     /**
  |      * This method sets up a new session. It generates a new e-mail address, adds the client's sessionMailQueue to the
  |      * Map keyed with the new e-mail address, and returns the new e-mail address.
  |      * 
  |      * @param pMailQueue
  |      *            the client's session scopes SessionMailQueue component.
  |      * @return the new e-mail address the client should use.
  |      */
  |     public String getNewEmail(SessionMailQueue pMailQueue) {
  |         String newMail = getNewEmailAddress();
  |         mLog.info("Adding mail queue to map for newMail: #0", newMail);
  |         mClientMap.put(newMail, pMailQueue);
  |         return newMail;
  |     }
  | 
  |     /**
  |      * This method takes in a List of RenderableMessages, iterates through them all, looking at all of the recipients on
  |      * each message, attempting to match those e-mail addresses to keys in the ClientMap Map. If it matches an entry in
  |      * the Map, it adds the RenderableMessage to the SessionMailQueue for that client.
  |      * 
  |      * @param pEmails
  |      *            the List of RenderableMessages to process.
  |      */
  |     public void processNewEmails(List<RenderableMessage> pEmails) {
  |         // Reverse the order so that the client side collections will always be correctly sorted by date, regardless of
  |         // how many e-mail arrive per batch.
  |         Collections.reverse(pEmails);
  |         // Loop through the new emails
  |         for (ListIterator<RenderableMessage> iter = pEmails.listIterator(); iter.hasNext();) {
  |             RenderableMessage currentEmail = iter.next();
  |             if (currentEmail != null) {
  |                 // Loop through the recipients of the incoming e-mail
  |                 for (String currentToAddress : currentEmail.getRecipientList()) {
  |                     mLog.info("Checking e-mail address: #0", currentToAddress);
  |                     SessionMailQueue clientQueue = mClientMap.get(currentToAddress);
  |                     // If we find a client SessionMailQueue in the map...
  |                     if (clientQueue != null) {
  |                         mLog.info("Matched e-mail address: #0", currentToAddress);
  |                         // Add the message to the client queue
  |                         clientQueue.addMessage(currentEmail);
  |                     }
  |                 }
  |             }
  |         }
  |     }
  | 
  |     @Destroy
  |     @Remove
  |     public void destroy() {
  |         mLog.info("Stopping...");
  |     }
  | }
  | 


I'm running JBoss 4.0.5, I used the JEMS installer and the ejb-3 profile.  I'm using the seam 1.1 beta release.

21:12:38,256 INFO  [Server] Starting JBoss (MX MicroKernel)...
  | 21:12:38,257 INFO  [Server] Release ID: JBoss [Zion] 4.0.5.GA (build: CVSTag=Branch_4_0 date=200610162339)


Let me know if there's anything else you need, or anything else I should try.

Thanks.

Modoc

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3981647#3981647

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3981647



More information about the jboss-user mailing list