FileDocCategorySizeDatePackage
JavaMailAsyncFilter.javaAPI DocGlassfish v2 API8912Thu Jun 15 13:21:06 BST 2006org.glassfish.grizzly.async.javamail

JavaMailAsyncFilter

public class JavaMailAsyncFilter extends Object implements com.sun.enterprise.web.connector.grizzly.AsyncFilter
This AsyncFilter connect to an email account and look for new messages. If there is no new message, the connection is keep-alived until a new message arrives. This is just an example on how asynchronous request processing works in Grizzly.
author
Jeanfrancois Arcand

Fields Summary
private static ConcurrentHashMap
handlers
Collection used to store JavaMailAsyncHandler registration.
private static ConcurrentLinkedQueue
mailFetcherCache
Cache instance of MailFetcher
private static final ScheduledThreadPoolExecutor
scheduler
Scheduler used to wait between execution of JavaMailAsyncHandler
private Session
mailSession
The current JavaMal Session.
private Properties
props
Properties used to store JavaMal configuration.
Constructors Summary
public JavaMailAsyncFilter()
AsyncFilter which implement a JavaMail client.


    
    // ---------------------------------------------------------------------//
    
    
                
      
        try {
            InitialContext ic = new InitialContext();
            String snName = "mail/MailSession";
            MailConfiguration mailConfig = 
                     (MailConfiguration)ic.lookup(snName); 
            
            props = mailConfig.getMailProperties();      
            props.setProperty("mail.pop3.ssl", "true");
            String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
            props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
            props.setProperty("mail.pop3.socketFactory.fallback", "false");
 
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    
Methods Summary
public booleandoFilter(com.sun.enterprise.web.connector.grizzly.AsyncExecutor asyncExecutor)
Execute the the filter by looking at the request context path. If the context path is a registered JavaMailAsyncFilterHandler, the request will be executed asynchronously. If not, the request will be executed synchronously.

        AsyncTask asyncProcessorTask 
                = asyncExecutor.getAsyncTask();
        ProcessorTask processorTask = asyncProcessorTask.getProcessorTask();
        AsyncHandler asyncHandler = processorTask.getAsyncHandler();
        
        Request request = processorTask.getRequest();
        String contextPath = request.requestURI().toString();
        contextPath = contextPath.substring(contextPath.lastIndexOf("/"));  
        
        JavaMailAsyncFilterHandler handler = handlers.get(contextPath);
        if ( handler == null){
            processorTask.invokeAdapter();    
            return true;
        } 
        MailFetcher mf = mailFetcherCache.poll();
        if ( mf == null ){
            mf = new MailFetcher();
        }
        mf.handler = handler;           
        mf.processorTask = processorTask;           
        mf.asyncProcessorTask = asyncProcessorTask;           
        mf.asyncHandler = asyncHandler;           
        
        scheduler.schedule(mf,1L,TimeUnit.SECONDS);
        return false;
    
public static voidregister(java.lang.String contextPath, JavaMailAsyncFilterHandler handler)
Register a JavaMailAsyncFilterHandler

        
        handlers.put(contextPath,handler);