JavaMailAsyncFilterpublic class JavaMailAsyncFilter extends Object implements com.sun.enterprise.web.connector.grizzly.AsyncFilterThis 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. |
Fields Summary |
---|
private static ConcurrentHashMap | handlersCollection used to store JavaMailAsyncHandler registration. | private static ConcurrentLinkedQueue | mailFetcherCacheCache instance of MailFetcher | private static final ScheduledThreadPoolExecutor | schedulerScheduler used to wait between execution of
JavaMailAsyncHandler | private Session | mailSessionThe current JavaMal Session. | private Properties | propsProperties 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 boolean | doFilter(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 void | register(java.lang.String contextPath, JavaMailAsyncFilterHandler handler)Register a JavaMailAsyncFilterHandler
handlers.put(contextPath,handler);
|
|