Direct Web Remoting tips

DZone 's Guide to

Direct Web Remoting tips

· Java Zone ·
Free Resource

We use DWR (http://www.directwebremoting.org) a lot where I'm working now. It's a Java library used to integrate JavaScript-based web development with Java middleware. In addition to providing a servlet that creates a nice JavaScript library (that delegates to your server-side Java beans), it also does server push (reverse Ajax) in a variety of flavors and situations. I'm really happy with it; you kind of just put it in your application and it works without a lot of trouble.

I recently had to dig a little into the source to learn how to do something I think might be generally useful (how to change, in real time, parameters which are typically only set via init-params in web.xml), so that's the topic here. If you aren't a DWR user, this might not be very interesting!

In my case, I needed to change DWR's maxWaitAfterWrite parameter, a DWR servlet init-param. Normally you set this once in web.xml, with:

(maxWaitAfterWrite refers to the amount of time DWR waits, after output has occurred, before closing the browser connection). DWR has an org.directwebremoting.Container class which is used to inject properties, including this one, but no immediately obvious API for changing it during an active deployment.

As it turns out, DWR's IoC container is accessible from its org.directwebremoting.ServerContext, and this is the path to changing this parameter, at least. The Container holds a number of beans. Some of these are java.lang.Strings (for example, the initial value of maxWaitAfterWrite) and are read-only, but some of these are DWR classes with APIs that you can use to your advantage.

As I mentioned, the value of maxWaitAfterWrite from web.xml is injected into a Container bean of type java.lang.String and named maxWaitAfterWrite. However, this parameter is itself injected at deployment time into any DWR class which extends org.directwebremoting.dwrp.BasePollHandler. You can iterate over the beans in Container and find (on my deployment, at least) two beans which are instances of BasePollHandler. Below is some code which gets a reference to the Container and iterates over its beans, outputting the details on the maxWaitAfterWrite bean and any instances of BasePollHandler:
package com.adamsresearch;

import java.util.*;
import org.directwebremoting.*;
import org.directwebremoting.dwrp.*;

public class JavaEndpoint
 public JavaEndpoint()
   ServerContext sc = ServerContextFactory.get();
   Container cont = sc.getContainer();
   Collection beanNamesColl = cont.getBeanNames();
   Iterator beanNames = beanNamesColl.iterator();
   while (beanNames.hasNext())
     String nextBeanName = beanNames.next();
     if (nextBeanName.equals("maxWaitAfterWrite"))
       System.out.println("Bean name:  '" + nextBeanName + "'");
       String staticBean = (String)cont.getBean(nextBeanName);
       System.out.println("  Class:  java.lang.String");
       System.out.println("  Value:  '" + staticBean + "'");
       Object nextBean = cont.getBean(nextBeanName);
       if (nextBean instanceof BasePollHandler)
         System.out.println("Bean name: '" + nextBeanName + "'");
         System.out.println("  Class:  " + nextBean.getClass().getName());

If you add a method to this Java bean and invoke it from DWR, when the constructor is called, you should see output like the following:
 Bean name:  'maxWaitAfterWrite'
   Class:  java.lang.String
   Value:  '500'
 Bean name: 'url:/call/htmlpoll/'
   Class:  org.directwebremoting.dwrp.HtmlPollHandler
 Bean name: 'url:/call/plainpoll/'
   Class:  org.directwebremoting.dwrp.PlainPollHandler

As subclasses of BasePollHandler, HtmlPollHandler and PlainPollHandler inherit the method setMaxWaitAfterWrite(). For my particular situation, then, I just grab all BasePollHandlers from the DWR Container (via their bean names, as output above) and set the parameter as needed.

This isn't rocket science, but it took me 2 or 3 hours to learn this, and there aren't any hits on the subject AFAIK. So, hopefully this will be useful to someone out there!


From http://wayne-adams.blogspot.com/2011/10/direct-web-remoting-tips.html


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}