Direct Web Remoting

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 =

	    public void publish(){
			ScheduledFuture<?> scheduledFuture =
	    	    scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
	    	        public void run() {


	     * Update using reverse Ajax
	    public void setDiv(){

	    		Browser.withAllSessions(new Runnable()
		    		public void run(){ 
		    	    		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());


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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: