Presentation is loading. Please wait.

Presentation is loading. Please wait.

J2ME RMI 1 Java 2 Platform, Micro Edition Implementation of J2ME RMI Application Farooq Sheikh.

Similar presentations


Presentation on theme: "J2ME RMI 1 Java 2 Platform, Micro Edition Implementation of J2ME RMI Application Farooq Sheikh."— Presentation transcript:

1 J2ME RMI 1 Java 2 Platform, Micro Edition Implementation of J2ME RMI Application Farooq Sheikh

2 J2ME RMI2 Contents What’s J2ME RMI? What’s MeRMI? MeRMI core concepts How to use MeRMI HelloWorld Example Demo (PhoneBook Application)

3 J2ME RMI3 The Java 2 Micro Edition(J2ME), Remote Method Invocation (RMI) Optional Package is a J2ME Connected Device Configuration (CDC) Optional Package The J2ME CDC requires a complete implementation of the Java virtual machine (JVM)* specification and core Java APIs including java.lang, java.io, java.net, and java.util. Implementations of the RMI Optional Package can only function on devices that include support for the J2ME CDC and the Foundation Profile. The RMI Optional Package requires implementations of the Foundation Profile and the J2ME CDC. The RMI Optional Package for J2ME/CDC is not supported by the J2ME Connected Limited Device Configuration (CLDC).

4 J2ME RMI4 MeRMI MeRMI is Simplified RMI for Java2 Micro Edition (MIDP 1.0) It’s a Zonski project available https://mermi.dev.java.net/ https://mermi.dev.java.net/ MeRMI brings the concepts of RMI, such as interface driven RPC, to J2ME. It provides an API and tool-set for the development of applications that use RMI-like remote procedure calls from a J2ME client to a J2SE server. Current Release: Jan 2004 Mermi1.0.2 Beta API’s

5 J2ME RMI5 MeRMI Concept Like RMI, MeRMI hides the communication protocol from the developer. The default implementation uses serialized data over HTTP to ensure compatibility with all MIDP 1.0 implementations. MeRMI allows developers to write J2SE server side objects using a very similar technique to that used when developing remote objects in RMI. The MeRMI compiler is then used to generate appropriate server side skeletons for the remote interface. When a client connects to the server and attempts to use the object, this skeleton is used to translate the network request, invoking the appropriate method on the server side implementation.

6 J2ME RMI6 MeRMI Concept The MeRMI compiler also generates a client side stub, which the client can use to remotely invoke methods on the remote server. Unlike standard RMI, this stub does not implement the remote interface of the server object. Instead the stub is generated with methods that are similar to the remote interface but are more suitable to J2ME.

7 J2ME RMI7 MeRMI Concept For instance if the remote interface was defined as follows public interface MyRemote extends Remote { public int doSomething(int param1, String param2) throws RemoteException; } Then the following stub would be generated: public class MyRemoteMicro extends HttpRemoteProxy { public int doSomething(int param1, String param2) throws IOException { // generated code to make remote invocation } }

8 J2ME RMI8 How to Use MeRMI (Server) In MeRMI many of the concepts and techniques are same as in standard RMI programming. In fact server side MeRMI components use the classes from the java.rmi package. As with standard RMI it is first necessary to create a remote interface to our server side object. This interface must be declared to extend the java.rmi.Remote interface. Each method can optionally be declared to throw java.rmi.RemoteException, however this is not required by MeRMI.

9 J2ME RMI9 How to Use MeRMI (Server) Once the Remote interface has been defined, we must then provide an implementation for it. This implementation has no restrictions, it may start threads, call out to remote servers, or perform any other normal Java operation. package example.hello; public class HelloWorldImpl implements HelloWorld { public String sayHello(String name) { return "Hello " + name; } }

10 J2ME RMI10 How to Use MeRMI (Server) Once we have server side classes we then need to generate the server side skeletons and the client side stubs These classes handle the network communications between the client and the server. MeRMI provides a tool, called mermic, to generate these classes The following commmands will compile the server classes and generate the MeRMI stubs and skeletons. javac -d./classes -classpath mermi-server.jar./example/hello/*.java mermic -d./generated -cp./classes example.hello.HelloWorld

11 J2ME RMI11 How to Use MeRMI (Server) Next Step to compile them and include them into a server side jar file. The following commands do this. javac -d./classes -classpath mermi-server.jar;./classes./generated/j2se/example/hello/*.java jar -cvf hello.jar./classes The server then needs to be deployed into a MeRMI server. The MeRMI server should already be installed as a servlet. (Tomcat) Two Steps need to Deploy the Server Step 1: hello.jar into the mermi/WEB-INF/lib directory of your MeRMI server. copy hello.jar /mermi/WEB-INF/lib Step 2: register the Hello implementation with the server Edit dregistrations.txt and add following line HelloWorld=example.hello.HelloWorldImpl

12 J2ME RMI12 How to Use MeRMI (Client) It is a point that You will see MeRMI begins to differ to the approach used in standard RMI In standard RMI, the remote interface used by the server is also used by the client. Unfortunately J2ME devices find this approach too heavy. MeRMI generates new classes for the client to use These classes have the same methods as the remote interface with slight changes, making them more J2ME friendly

13 J2ME RMI13 How to Use MeRMI (Client) The following client class is generated by MeRMI for our HelloWorld interface. package example.hello; import com.zonski.mermi.http.HttpRemoteProxy; public class HelloWorldMicro extends HttpRemoteProxy { public String sayHello(String name) throws IOException { return (String)super.invoke(0, new Object[] {name}); } o The generated class extends HttpRemoteProxy, a base class provided by MeRMI, which performs the remote invocations on the server o Each method on the remote interface is reproduced on the generated client class. o Our sayHello method is here, taking the same parameters and returning the same value. o The method however, does not throw a RemoteException. Instead it throws an IOException, which is part of the MIDP 1.0 API.

14 J2ME RMI14 How to Use MeRMI (Client) You can see that our sayHello method has been implemented to use the base class method invoke. All generated methods use this same approach. This method translates the invocation request into a remote call and then reads the response from the server. The details of how this works are hidden and it is not nescessary to deal with this implementation. In order to use this generated class we must create a MIDlet that locates a reference to the remote object.

15 J2ME RMI15 How to Use MeRMI (Client) MeRMI provides a Directory for performing this operation. Currently only the HttpDirectory exists, however in the future new Directory implementations may be created to support other protocols The HttpDirectory is created with the url of the running MeRMI server The name used to register the hello implementation is then used to lookup a reference to the object.

16 J2ME RMI16 How to Use MeRMI (Client) import example.hello.HelloWorldMicro; public class HelloMIDlet extends MIDlet { protected void startApp() { try { HttpDirectory directory = new HttpDirectory("http://localhost:8080/mermi/mermi"); HelloWorldMicro hello = (HelloWorldMicro)directory.locate("HelloWorld"); String message = hello.sayHello( “ Farooq"); // } catch (IOException e) { // handle exception } } // other MIDlet code here }

17 J2ME RMI17 How to Use MeRMI (Client) import example.hello.HelloWorldMicro; public class HelloMIDlet extends MIDlet { protected void startApp() { try { HttpDirectory directory = new HttpDirectory("http://localhost:8080/mermi/mermi"); HelloWorldMicro hello = (HelloWorldMicro)directory.locate("HelloWorld"); String message = hello.sayHello( “ Farooq"); // } catch (IOException e) { // handle exception } } // other MIDlet code here } HttpDirectory is created with the url of the running MeRMI server HttpDirectory directory = new HttpDirectory("http://localhost:8080/mermi/mermi");

18 J2ME RMI18 How to Use MeRMI (Client) import example.hello.HelloWorldMicro; public class HelloMIDlet extends MIDlet { protected void startApp() { try { HttpDirectory directory = new HttpDirectory("http://localhost:8080/mermi/mermi"); HelloWorldMicro hello = (HelloWorldMicro)directory.locate("HelloWorld"); String message = hello.sayHello( “ Farooq"); // } catch (IOException e) { // handle exception } } // other MIDlet code here } lookup a reference to the object HelloWorldMicro hello = (HelloWorldMicro)directory.locate("HelloWorld");

19 J2ME RMI19 How to Use MeRMI (Client) import example.hello.HelloWorldMicro; public class HelloMIDlet extends MIDlet { protected void startApp() { try { HttpDirectory directory = new HttpDirectory("http://localhost:8080/mermi/mermi"); HelloWorldMicro hello = (HelloWorldMicro)directory.locate("HelloWorld"); String message = hello.sayHello( “ Farooq"); // Remote Method Invocation } catch (IOException e) { // handle exception } } // other MIDlet code here } MeRMI performs the remote invocations on the server String message = hello.sayHello(“Farooq"); // Remote Method Invocation

20 J2ME RMI20 Demo Phone Book My SQL Client

21 J2ME RMI21 The END! You can download all the demos JARs from: http://students.cs.wichita.edu/~fasheik h/cs843/ http://students.cs.wichita.edu/~fasheik h/cs843/ Special Thanks to Dr. Li Jia Bioinformatics Dept WSU using facilities


Download ppt "J2ME RMI 1 Java 2 Platform, Micro Edition Implementation of J2ME RMI Application Farooq Sheikh."

Similar presentations


Ads by Google