Sonntag, 11. Dezember 2011

Cross-side RPC Calls with the GWT

In developer mode you have to make rpc calls for example from the jetty-server, running on localhost:8888 to the tomcat 7 server, running on localhost:8080.

In this tutorial I will describe how you can do it.

First you need to download the proxy from: http://www.siafoo.net/snippet/258 or you paste the following code from the other side directly: http://gwtdebugtomcateclipse.blogspot.com/2011/12/quelltext-proxy.html

First let us create the gwt application, to do so click: File -> New -> Project -> Window Builder -> GWT Designer -> Model -> GWT Java Project
I will give it the following properties:
Project name: RPCSender
Create GWT Module: yes
Module name: RPCViewer
Package name: de.tu_freiberg.sfb799

Next we are creating an rpc-service, so please click with your right mouse button at the gwt project and select:
New -> Other -> Window Builder -> GWT Designer -> Model -> GWT Remote Service
I will use the following properties:
client package: RPCSender/src/de.tu_freiberg.sfb799.client
service name: SimpleRPC

Now the SimpleRPC.java opens in Eclipse:
change the java file to the following:
...
@RemoteServiceRelativePath("SimpleRPC")
public interface SimpleRPC extends RemoteService {
    public String getNumber(int i);
...

When you now press the save icon in eclipse, it will change automatically the following files:  SimpleRPCAsync.java, SimpleRPCImpl.java (in the server package)

 Now we want to change the SimpleRPCImpl.java to:
...
@Override
    public String getNumber(int i) {
        // TODO Auto-generated method stub
        switch(i) {
        case 1: return "one";
        default: return "not yet implemented";
        }

    }
...

Next we will change the following file RPCViewer.java to:
...
            public void onClick(ClickEvent event) {
                AsyncCallback<String> cb = new AsyncCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        clickMeButton.setText(result);
                    }
                    @Override
                    public void onFailure(Throwable caught) {
                        // TODO Auto-generated method stub
                    }
                };
                SimpleRPCAsync sRPC = (SimpleRPCAsync)GWT.create(SimpleRPC.class);
                sRPC.getNumber(1, cb);

            }
...

Now you can test this rpc with: right click on this project -> Run As ->  WebApplication

When you now click on the button, you will see that the button text changes to one.
And if you have installed Firebug in Firefox for example you will see that the following post will be fired after pressing on the button:
POST http://127.0.0.1:8888/de.tu_freiberg.sfb799.RPCViewer/SimpleRPC

This is the adress we need to proxy with the servlet you downloaded at the beginning of this article.
That means that the proxy will send this request to: http://127.0.0.1:8080/de.tu_freiberg.sfb799.RPCViewer/SimpleRPC where my Tomcat server is listening for the proxy.

So first create ProxyServlet.java in the package de.tu_freiberg.sfb799.server
Now insert the java code from the beginning of this article, and insert at the beginning of the file the line:
package de.tu_freiberg.sfb799.server;

Ok, now we need to change the web.xml file in /RPCSender/war/WEB-INF/web.xml to:
...
<web-app>

    <!-- Default page to serve -->
    <servlet>
        <servlet-name>ProxyServlet</servlet-name>
          <servlet-class>de.tu_freiberg.sfb799.server.ProxyServlet</servlet-class>
     </servlet>
     <servlet-mapping>
          <servlet-name>ProxyServlet</servlet-name>
          <url-pattern>/de.tu_freiberg.sfb799.RPCViewer/SimpleRPC</url-pattern>
     </servlet-mapping>

    <welcome-file-list>
        <welcome-file>RPCViewer.html</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>SimpleRPC</servlet-name>
        <servlet-class>de.tu_freiberg.sfb799.server.SimpleRPCImpl</servlet-class>
    </servlet>
    <!-- 
    <servlet-mapping>
        <servlet-name>SimpleRPC</servlet-name>
        <url-pattern>/de.tu_freiberg.sfb799.RPCViewer/SimpleRPC</url-pattern>
    </servlet-mapping>
    -->
</web-app>

Now we are done with reconfiguring the gwt-application. Now I will create the dynamic webproject:
Create now with File -> New -> Other -> Web -> Dynamic Webproject a new Webproject which will run on your Tomcat 7 or whatever else server.
I will use the following properties:
Project name: RPCReceiver
Target Runtime: Apache Tomcat v 7.0
ContextRoot: de.tu_freiberg.sfb799.RPCViewer
Generate web.xml ..: yes 

Now copy the two packages de.tu_freiberg.sfb799.server and de.tu_freiberg.sfb799.client from the RPCSender project to the RPCReceiver project. You can delete the following files: SimpleRPCAsync.java, RPCViewer and ProxyServlet.java because we do not need them anymore.
Now we need to copy the gwt-servlet.jar from /RPCSender/war/WEB-INF/lib/gwt-servlet.jar to /RPCReceiver/WebContent/WEB-INF/lib/gwt-servlet.jar
This is needed because Tomcat needs the library because our servlet SimpleRPCImpl extends RemoteServiceServlet.

Next we need to insert the following in the web.xml file at /RPCReceiver/WebContent/WEB-INF/web.xml:
    <servlet>
        <servlet-name>SimpleRPC</servlet-name>
        <servlet-class>de.tu_freiberg.sfb799.server.SimpleRPCImpl</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>SimpleRPC</servlet-name>
        <url-pattern>/SimpleRPC</url-pattern>
    </servlet-mapping>

Please note that the url-pattern is just /SimpleRPC and not de.tu_freiberg.sfb799.RPCViewer/SimpleRPC, because de.tu_freiberg.sfb799.RPCViewer is our ContextRoot.


Now it would run, but to see a difference please add the following line to /RPCReceiver/src/de/tu_freiberg/sfb799/server/SimpleRPCImpl.java:
...
        case 1: return "one";
        case 2: return "two";
        default: return "not yet implemented";

...


And make the following change in /RPCSender/src/de/tu_freiberg/sfb799/client/RPCViewer.java:
sRPC.getNumber(2, cb);


Now please start the Tomcatserver by clicking on the RPCReceiver project with the right mouse button -> Run As -> Run on Server
And start your RPCSender like you did it before.
If you now click on the button, you should see the word two.


To verify to 100% that the answer from the call comes from apache, please stop the apache server and reload the page.
If you now click on the button, then you will see that the text will not change.
And Firebug will show the following:
POST http://127.0.0.1:8888/de.tu_freiberg.sfb799.RPCViewer/SimpleRPC 500 Connection to http://localhost:8080 refused


"NetworkError: 500 Connection to http://localhost:8080 refused - http://127.0.0.1:8888/de.tu_freiberg.sfb799.RPCViewer/SimpleRPC"







If you see this error, then you know that the proxy works well and you can do cross-script rpc calls. So you can use the big adventage of Tomcat debugging in database pools and you can debug with the jetty server in the javascript source -> generated from gwt.

Keine Kommentare:

Kommentar veröffentlichen