JavaRMI

Share Embed Donate


Short Description

Download JavaRMI...

Description

George Blank University Lecturer 

Java

RMI

Introduction to RMI

Java

RMI

Introduction to RMI

Java and the Web Java¶s popularity is due to its suitability for use on the World Wide Web (WWW). Seve Severa rall Web brow browse sers rs hav have e supp suppor ortt for  for  Java:  ± Nets Netsca cape pe Nav Navig igat ator  or   ± Inte Intern rnet et Expl Explor orer  er  ± HotJava Java

incorporates audio, video, and animation directly on the web page.

Method Modifiers Instance Methods Class Methods Abstract Methods Native Methods Final Methods

Method Modifiers with Effect on Scoping public: Method can be accessed by any class private: methods accessed only by class

methods friendly: methods accessed by other methods in the same package protected : methods accessed by subclass methods.

Interfaces A Java interface is similar to a class, except there is no data associated with the interface. Example: public interface MyInterface { methods-with no implementation details final variables

}

Properties

of Interfaces

The variables in an interface must be final. The methods are only declarations. A class that extends another class is guaranteed to support the contracts entered into by its superclass. Interfaces are used to approximate multiple inheritances

Java

Native Interface (JNI)

The Java Native Interface (JNI) is part of the core JDK and provides a framework for interfacing to native code. The native code is not easily portable across different hardware platforms. So using native code takes way one of the major advantages of java. JNI was developed to bridge the gap. (JNI section by Rekha Telkar)

When to use JNI When the standard Java class library does not support the platform-dependent features needed by the application. You need to use a library written in another language, and wish to make it accessible to Java code through the JNI. You want to implement a small portion of  time-critical code in a lower-level language such as assembly.

JNI

Capabilities

You can use native methods to: ± Create, inspect, and update Java objects (including arrays and strings). ± Call Java methods. ± Catch and throw exceptions. ± Load classes and obtain class information. ± Perform runtime type checking.

Enabling Java code with JNI You can use the JNI with the Invocation API to enable an arbitrary native application to embed the Java VM. Programmers can make their existing applications Java-enabled without having to link with the VM source code.

Remote Method Invocation RMI provides the means to invoke methods remotely. RMI allows for applications to communicate and execute across multiple systems on a network. RMI is supported by the java.rmi, java.rmi.server, and java.rmi.registry Enhanced security of  Java 2 requires a security policy implementation.

Parts

in a RMI System

Interface definitions for remote services Implementations of the remote services Stub and Skeleton files A server to host the remote services An RMI Naming service that allows clients to find the remote services A class file provider (an HTT P or FTP server)

RMI process Java Client

Java Server  

Client Method arguments

Called Method results

Client Stub

Server Skeleton  Not needed   In Java 2

Network transport

Network Transport

 Network 

RMI Server, Client, and Registry The server process registers the remote object X with the registry using the N aming.bind() method. The client calls N aming.lookup() , which contacts the registry and obtains a stub object for X. The client then uses the stub as if it is a local object.

Stub Class A stub for a remote object is the clientside proxy for the remote object. Such a stub implements all the interfaces that are supported by the remote object implementation. The client-side stub responsibilities are shown on the next slide.

Stub Class Responsibilities Initiating a call to the remote object (by calling the remote reference layer). Marshaling arguments to a marshal stream (obtained from the remote reference layer). Informing the remote reference layer that the call should be invoked. Unmarshaling the return value or exception from a marshal stream. Informing the remote reference layer that the call is complete .

Skeleton Class A skeleton for a remote object is a server-side entity that contains a method which dispatches calls to the actual remote object implementation. The skeleton is responsible for: ± Unmarshaling arguments from the marshal stream. ± Making the up-call to the actual remote object implementation. ± Marshaling the return value of the call or an exception (if one occurred) onto the marshal stream.

Remote Reference Layer  The remote reference layer deals with the lower level transport interface and is responsible for carrying out a specific remote reference protocol which is independent of the client stubs and server skeletons. The remote reference layer has two cooperating components: the client-side and the serverside components.

Remote Reference Layer (2) The client-side component contains information specific to the remote server (or servers, if the remote reference is to a replicated object) and communicates via the transport to the server-side component. During each method invocation, the client and server-side components perform the specific remote reference semantics.

Remote Reference Layer (3) For example, if a remote object is part of a replicated object, the client-side component can forward the invocation to each replica rather than just a single remote object. In a corresponding manner, the server-side component implements the specific remote reference semantics prior to delivering a remote method invocation to the skeleton.

Remote Reference Layer (4) For example, the server side could handle ensuring atomic multiple delivery by communicating with other servers in the replica group.The remote reference layer transmits data to the transport layer via the abstraction of a stream-oriented connection. The transport takes care of the implementation details of connections. Although connections present a streams-based interface, a connectionless transport may be implemented beneath the abstraction

RMI Registry The Registry tracks the addresses of the remote objects exported by applications It is the central management point for RMI Does not actually invoke remote methods Bind() links the object in the registry Rebind() replaces object with a new one

Parameter Passing

When a remote procedure is executed, the  java.rmi runtime encodes the arguments and sends them over the network to a server that decodes them. The server then invokes the method, encodes the results, and sends it back. Finally, the client-side java.rmi runtime decodes the result.

Parameter

Marshalling

RMI stubs are responsible for packaging parameters used in a remote method in a block of bytes using the big-endian byte order. This is called parameter marshalling.  A receiver object on the server must unmarshall the parameters or report errors.

Building RMI Applications Define remote interfaces Create classes that implement the interfaces Create stub and skeleton classes for the implementation classes. Create Security Policy

 A Distributed H ello World Program Using RMI It uses an applet to make a remote method call to the server from which it was downloaded to retrieve the message "Hello World!". When the applet runs, ³Hello World!´ is displayed on the client browser.

Steps Involved Write The HTML and Java Source Files. Compile and Deploy Class Files and HTML Files. Start the Remote Object Registry, Server, and Applet

Source Files The Java remote interface. (Hello.java) The Java remote object (server) which implements the remote interface. (HelloImpl.java) The Java applet that remotely invokes the remote method, sayHello(). (HelloApplet.java) The HTML code for the web page that references the applet. (hello.html)

The Remote Interface Must be public. Extends the interface java.rmi.Remote. Each method must declare  java.rmi.RemoteException in its throws clause A remote object passed as an argument or  return value must be declared as the remote interface.

Remote Interface  package examples.hello; import java.rmi.Remote; import java.rmi.RemoteException;  public interface Hello extends java.rmi.Remote { String sayHello() throws java.rmi.RemoteException; }

The Implementation Class Specify the remote interface(s) being implemented. Define the constructor for the remote object. Provide implementations for the methods that can be invoked remotely. Create and install a security manager.

The Implementation Class (Cont¶d) Create one or more instances of a remote object. Register at least one of the remote objects with the RMI remote object registry, for  bootstrapping purposes.

Server Code (1)  package examples.hello; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.RMISecurityManager; import java.rmi.server.UnicastRemoteObject;  public class HelloImpl extends UnicastRemoteObject implements Hello {  public HelloImpl() throws RemoteException { super(); }

Server Code (2)

// Implementation of remote method 

 public String sayHello() { return "Hello World!"; }

Server Code (3) main begins  public static void main(String args[]) { // Create and install a security manager

if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager());

}

Server Code (4) main ends try { HelloImpl obj = new HelloImpl(); // Bind this object instance to the name "HelloServer"  Naming.rebind(" //afsxx.njit.edu/HelloServer", obj); System.out.println("HelloServer bound in registry"); } catch (Exception e) { System.out.println("HelloImpl err: " + e.getMessage()); e.printStackTrace(); } } }

Notes on Server Code By extending remote class UnicastRemoteObject, the HelloImpl class can be used to create a remote object that:  ± Uses RMI's default sockets-based transport for  communication  ± Runs all the time (In Java 2 SDK, the object can be activated (created) when a client requests it, using Remote Object  Activation, rather than running all the time ± extend  java.rmi.activation.Activatable ) To bind to a different port, for example 2001, use: Naming.rebind("//afsxx.njit.edu:2001/HelloServer", obj);

Notes on Server Code (contd)

Arguments to, or return values from, remote methods can be any data type for the Java platform, including objects, as long as those objects implement the interface java.io.Serializable. By default, local objects are passed by copy, which means that all data members (or fields) of an object are copied, except those marked as static or transient Remote objects are passed by reference. A reference to a remote object is actually a reference to a stub, which is a clientside proxy for the remote object.

Notes on Server Code (contd)

Security manager guarantees that the classes that get loaded perform only allowed operations. If no security manager is specified, no class loading, by RMI clients or servers, is allowed, aside from what can be found in the local CLASSP ATH. Client Applets use the security manager already installed in the client browser . If the client were an application rather than an applet, Security manager would need to be installed. A security manager is required in any JVM that needs to download code, and RMI clients need to download RMI stubs (as well as any other custom classes or interfaces needed to communicate with the RMI server).

 A Remote Service Applet  package examples.hello; import java.applet.Applet; import java.awt.Graphics; import java.rmi.Naming; import java.rmi.RemoteException;  public class HelloApplet extends Applet { String message = ""; // "obj" is the identifier that we'll use // to refer to the remote object that // implements the "Hello" interface Hello obj = null;

Remote Applet Code (2)  public void init() { try { Hello obj = (Hello)Naming.lookup("//" + getCodeBase().getHost() + "/HelloServer");  message = obj.sayHello(); } catch (Exception e) { System.out.println("Applet exception: " + e.getMessage()); e.printStackTrace(); } }

Remote Applet Code (3)  public void paint(Graphics g) { g.drawString(message, 25, 50); } }

Notes on Client Applet The applet gets a reference to the remote object implementation (advertised as "HelloServer") from the server host's rmiregistry. The applet invokes the remote sayHello method on the server's remote object. The applet invokes the paint method, causing the string "Hello World!" to be displayed in the drawing area of the applet.

The Web Page (hello.html)     Hello World        Hello World      

 p>   <  myclasses/ "  
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF