2. RMI over HTTPOffering services over HTTP is the most common solution employed today. The following sections explain how to prepare and deploy a Java Object as a service over HTTP with SwitchRMI. Preparing Your ClassYour class does not have to be a subclass of any SwitchRMI class, it does not have to implement a "remote" interface. There is no code generation processing step involoved in preparing your class. It must, however, declare an interface with the methods available for remote access and live within the restrictions documented below for the supported protocols.
Writing the Deployment DescriptorThe SwitchRMI web application requires a deployment description for each object to be published. The descriptor gives:
The deployment descriptor is placed into an XML formatted file, containing all of the information required by the SwitchRMI servlet. The full file format will be documented at a later date, however, the following example shows an excerpt of the test-suite file with the deployment descriptor for the XMLRPC validation object: <object name="echo" scope="session"">; <java class="com.mjh.switchrmi.transport.test.IEchoImpl" /> <interface class="com.mjh.switchrmi.transport.test.IEcho" />; <description> The echo object is included to allow testing of the SwitchRMI installation. </description> </object> The format is simple to follow.
Your object's deployment descriptor must be pasted into a <?xml version="1.0"?> <switchrmi> <service> <!-- Your deployment descriptors go here. -->; <service> <switchrmi> The full format of this file will be documented at a later date.
In the <servlet> <servlet-name>rmi</servlet-name> <servlet-class>com.mjh.switchrmi.transport.http.HttpRmiServlet</servlet-class> <init-param> <param-name>switchrmi.configuration</param-name> <param-value>http://localhost:8080/test/jndi/TestSuiteConfiguration.xml</param-value> </init-param> </servlet>
Once you have deployed the object and started your web application you should be able to browse it with the HTML interfac
with the URL: Invoking a Remote MethodIn order to invoke a remote method from a client application you must have access to the class files for one, or more, of the interfaces with with the object is deployed in the RMI Servlet. You also need to know the URL of the deployed object.
The URL format is straightforward, URLs are valid HTTP UIRLs in all respects. The distinguishing factor is the last element
in the path part of the URL. This last element consists of a "filename" part, which is the published name of the object as
declared in the For example in the JUnit test suite which comes with the full SwitchRMI distribution you will see the URL's:
Both URLs refer to the same published object, "echo", but the first suffix, "raw" declares to the client-side API that the raw, serializable RMI protocol will be used and the second that the XMLRPC protocol will be used. The same published object in the same servlet instance can be accessed by different clients using different protocols. SwitchRMI uses a dynamic proxy interface to remote services. The client API is simple, obtain a RMIClient for the underlying transport to be used (HTTP, SMTP, etc.) and then use the client to connect to the remote object via a proxy: import com.mjh.switchrmi.*; private RmiClient rmi; private IEcho proxy; try { rmi = new RmiClient(); proxy = rmi.connect("http://localhost:8080/rmi/echo.xmlrpc", IEcho.class); } catch (Exception ex) { } Once you have successfully obtained a proxy to the remote object you can then invoke the methods from the requested interface on the proxy: try { String string = "this is a string"; String echoed = proxy.echo(string); System.out.println("echoed.equals(string) = " + echoed.equals(string)); } catch (Exception ex) { } Since this is a remote method invocation you should always wrap method calls to the proxy in a try/catch block in case any network problem breaks the RMI call. |