Direct Web Remoting
October 24, 2013 Leave a comment
I believe in change but applications developed by our companies do not use technology in adequate measure. They do not change with the times. This is caused by fear of software. Software breaks in production and if we have inadequate unit tests we are scared.
I have written a simple test using DWR several years back but I haven’t used it anywhere. DWR enables us to communicate with our browsers using Ajax in the reverse. I experimented with it again and this code updates the ‘div’ in the browser at periodic intervals.
The code is not written very well but it works. There are techniques to shutdown the threads properly.
The main purpose is to detect broken links to the browser and expire the sessions. That is a hard problem now.
I will add details if I am able to achieve it.
package com.test; import java.util.Collection; import java.util.Iterator; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.directwebremoting.Browser; import org.directwebremoting.Container; import org.directwebremoting.ScriptSession; import org.directwebremoting.ServerContextFactory; import org.directwebremoting.extend.ScriptSessionManager; import org.directwebremoting.ui.dwr.Util; public class ReversePublisher { ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5); public void publish(){ @SuppressWarnings("unchecked") ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(new Runnable() { public void run() { setDiv(); } }, 5, 5, TimeUnit.SECONDS); } /** * Update using reverse Ajax */ public void setDiv(){ try{ Browser.withAllSessions(new Runnable() { public void run(){ try{ Util.setValue("Timeout", "Reverse Ajax"); }catch(Exception e){ System.out.println( "Detecting broken link " + e.getMessage()); } } }); }catch(Exception e){ System.out.println( "Exception in Publishing in the reverse " + e.getMessage()); } } }
Update
After asking the DWR forum I understood that when I enable ‘Reverse Ajax’ in web.xml there is an implicit polling call being made by the browser. Mike Wilson answered my questions sent to the DWR user list. This particular problem does not require a scheduled Thread to make frequent polling calls to the browser. It is implicit.
This call is made at a certain configurable interval regularly. I coded a filter to trap this call and stored the access times. If a browser crashes or closes these calls are not made for a specific period. When the browser opens again, even if cookies or caches are reinstated this filter can check the last accessed time and the current time and decide to expire the HttpSession.
I believe at this time that can solve my problem. Network latency could cause concerns though.
Google Chrome’s JavaScript console shows this implicit polling call.