Question

i want to use annotated DWR classes in my project. So i created 2 classes. Assume Dwr1.java & Dwr2.java

in my web.xml i have the following entry to load the classes:

<servlet>
    <display-name>DWR Servlet</display-name>
    <servlet-name>dwr-invoker</servlet-name>
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>activeReverseAjaxEnabled</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>classes</param-name>
        <param-value>
           de.package.Dwr1,
           de.another.package.Dwr2
       </param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

The Proplem is that the Dwr2 Class does not get instantiated. If i remove Dwr1 from my web.xml, Dwr2 gets instantiated. But if i have both entries in my web.xml only Dwr1 gets instantiated.

Dwr1 is a very simple class:

@RemoteProxy
public class Dwr1 {

    @RemoteMethod
    public String getString(String key) {
        return Util.GetString(key);
    }
}

Dwr2 is a little bit more complex like:

@RemoteProxy
public class Dwr2 {

    @RemoteMethod
    public Dwr2() {
        ServerUtil.registerOnFE(new UpdateClient());
    }

    private void updateBrowser(final String arg) {
        Browser.withPage("/page.do", new Runnable() {
            @Override
            public void run() {
                ScriptSessions.addFunctionCall("update", arg);
           }
        });
    }

    private class UpdateClient {
        @Override
        onRecieveUpdate(String arg) {
            updateBrowser(arg);
        }
    }
}

Are there any Mistakes in my classes or in my web.xml? Or how can i instantiate multiple DWR Classes with Annotations?

Was it helpful?

Solution

I found a solution to fix this issue. It seems like DWR doesn´t instantiate every class on the server-startup. (But it apparently does if there is only one class defined in the config file). However, I just implemented a "dummy-method" in dwr-classes, called "start()". The Method does nothing, but when it get's called from javascript, the class gets instantiated. My solution looks like this:

<servlet>
    <display-name>DWR Servlet</display-name>
    <servlet-name>dwr-invoker</servlet-name>
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>activeReverseAjaxEnabled</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>classes</param-name>
        <param-value>
           de.package.Dwr1,
           de.another.package.Dwr2
       </param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
  • Dwr1

    @RemoteProxy
    public class Dwr1 {
    
        @RemoteMethod
        public String getString(String key) {
            return Util.GetString(key);
        }
    }
    
  • Dwr2

    @RemoteProxy 
    public class Dwr2 {
    
        public Dwr2() {
            ServerUtil.registerOnFE(new UpdateClient());
        }
    
        @RemoteMethod
        public void start() {
             //Do nothing
        }
    
        private void updateBrowser(final String arg) {
            Browser.withPage("/page.do", new Runnable() {
                @Override
                public void run() {
                    ScriptSessions.addFunctionCall("update", arg);
               }
            });
        }
    
        private class UpdateClient {
            @Override
            onRecieveUpdate(String arg) {
                updateBrowser(arg);
            }
        } 
    }
    

A call of the start()-method of Dwr2 class triggers the insantiation of it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top