RMI Specification (2)

Source: Internet
Author: User
Tags extend object model object serialization serialization stub

RMI Specification--Chapter II

Java Distributed object Model

2.1 Distributed object Applications
RMI applications typically include two separate programs: Server programs and client programs. A typical service
The application will create multiple remote objects so that the remote objects can be referenced, and then wait for the client
Invokes methods on those remote objects. A typical client program gets one or more of the
A reference to the remote object, and then calls the method of the remote object. RMI communicates to the server and client
and information delivery provides a mechanism. Such applications are sometimes referred to as distributed object applications.

Distributed object applications require:

Locating remote Objects
An application can use one of two mechanisms to get a reference to a remote object. It can be used for both the RMI Jane
Single-named tool Rmiregistry to register its remote object, or remote object references as a general
Part of the operation to pass and return.
Communicating with remote objects
The details of communication between remote objects are handled by RMI; For programmers, remote communication looks like a standard
Quasi-Java method invocation. Loads the class byte code for an object passed as a parameter or return value because RMI
Allows the calling program to pass a pure Java object to a remote object, so RMI provides the necessary mechanism to
You can load the object's code and transfer the object's data.
The server invokes the registration service to associate the name with the remote object. Client registers service with server
The program uses the name of the remote object to find the remote object and then calls its method. RMI can use Java
Any URL protocol supported by the system (such as HTTP, FTP, file, etc.) loads the class byte code.


2.2 Definition of terms
In the Java distributed object model, remote object is an object: its methods can
Called from other Java virtual machines, which may be on different hosts. Objects of this type are composed of one or
A variety of remote interfaces (it is the Java interface that declares the remote object method) is described.
Remote method call (RMI) is the action that invokes the method of a remote interface on a remote object. More important.
Yes, the method invocation of the remote object is the same as the method invocation syntax for the local object.


2.3 Comparison of distributed and non-distributed models
The Java Distributed object model is similar to the Java object model in the following ways:

A reference to a remote object can be passed as a parameter or a knot in either method invocation (local or remote)
Fruit form return.
Remote objects can be coerced into any remote interface, as long as the interface is used for built-in Java syntax
Supported by the implementation of the coercion type conversion.
The built-in Java operator instanceof can be used to test remote interfaces supported by remote objects.
The Java Distributed object model differs from the Java object model in the following ways:
The client of the remote object interacts with the remote interface and never interacts with the implementation class of those interfaces.
Non-remote parameters and return results of the remote method are passed by copy rather than by reference. This is because
A reference to an object is only useful in a single virtual machine.

Instead of replicating the actual remote implementation, the remote object is passed as a reference.
Some Java.lang.Object classes define methods that are semantically specific to remote objects.
Because the failure mode that invokes the remote object is inherently more complex than the failed mode of invoking the local object, the customer
The machine must handle the extra exceptions that occur during a remote method call.

2.4 RMI Interface and class overview
2.4.1 Java.rmi.Remote Interface
In RMI, a remote interface declares a set of methods that can be invoked from a remote Java virtual machine. Remote Connect
The port must meet the following requirements:

The remote interface must at least extend the Java.rmi.Remote interface directly or indirectly.
The method declaration in the remote interface must meet the requirements of the following remote method declarations:
The remote method declaration, in addition to containing application-related exceptions in its throws clause, notes that the
Application-related exceptions do not need to extend java.rmi.RemoteException), you must also package
Enclose a java.rmi.RemoteException exception (or its superclass, such as
Java.io.IOException or Java.lang.Exception).
In a remote method declaration, declared as a parameter or return value (declared directly in a parameter table or embedded in a parameter

Remote object must be declared as a remote interface, not as an implementation class for that interface.
The Java.rmi.Remote interface is a markup interface that does not define a method:

public interface Remote


The remote interface must extend at least the Java.rmi.Remote interface (or other extension
Java.rmi.Remote remote interface). However, the remote interface can be extended in the following cases, not far
Process interface:

The remote interface can also extend other non-remote interfaces, as long as all the methods of the extended interface (if any) satisfy
Requirements for remote method declarations.
For example, the following interface BankAccount defines a remote interface for accessing a bank account. It packs
Remote method with account deposit, account balance and withdrawal from account:

Public interface BankAccount extends Java.rmi.Remote
{
public void deposit (float amount)
Throws Java.rmi.RemoteException;
public void withdraw (float amount)
Throws Overdrawnexception, Java.rmi.RemoteException;
public float getbalance ()
Throws Java.rmi.RemoteException;
}


The following example illustrates a valid remote interface Beta. It extends the non-remote interface Alpha (with remote methods) and

Interface Java.rmi.Remote:

public interface Alpha
{
Public final String okay = "Constants are okay too";
public object Foo (object obj)
Throws Java.rmi.RemoteException;
public void Bar () throws java.io.IOException;
public int Baz () throws java.lang.Exception;
}


Public interface Beta extends Alpha, java.rmi.Remote {
public void Ping () throws java.rmi.RemoteException;
}

2.4.2 RemoteException Class
The Java.rmi.RemoteException class is thrown by the RMI runtime during a remote method call
Super class for the exception. To ensure the robustness of applications that use the RMI system, declared in the remote interface
The remote method must specify a java.rmi.RemoteException in its throws clause (or its
Super class, such as Java.io.IOException or java.lang.Exception.

When a remote method call fails for some reason, it throws a java.rmi.RemoteException
Often. The reasons for the remote method call failure include:

Communication failed (remote server unreachable or denied connection, connection was shut down by server, etc.). )
Parameter or return value failed while transmitting or reading
Protocol error
The RemoteException class is a checked exception that must be handled by the caller of the remote method and
Compiler-checked exceptions, not runtimeexception.

2.4.3 RemoteObject class and its subclasses
RMI server functions by Java.rmi.server.RemoteObject and their subclasses
Java.rmi.server.RemoteServer, Java.rmi.server.UnicastRemoteObject
and java.rmi.activation.Activatable provided.

Java.rmi.server.RemoteObject is sensitive to remote objects Java.lang.Object
Methods, Hashcode, Equals, and toString provide implementations.
The methods needed to create and export remote objects (making them available to remote clients) are determined by the class
Provided by UnicastRemoteObject and Activatable. Subclasses can recognize the semantics of remote references,

For example, a server is a simple remote object or a remote object that can be activated (the remote object that will execute when invoked).
The Java.rmi.server.UnicastRemoteObject class defines the monomer (single route) remote pair
, the reference is valid only if the server process is alive.
Class Java.rmi.activation.Activatable is an abstract class that defines the Activatable
The remote object starts executing when its remote method is invoked and shuts itself down when necessary.

2.5 Implement remote Interface
The general rules for the classes that implement the remote interface are as follows:

This class typically extends the Java.rmi.server.UnicastRemoteObject and thus inherits the class
Java.rmi.server.RemoteObject and Java.rmi.server.RemoteServer provide
Remote behavior.
This class can implement any number of remote interfaces.
This class can extend other remote implementation classes.
This class can define methods that do not appear in the remote interface, but these methods can be used locally only and not remotely

Use.
For example, the following class Bankacctimpl implements the BankAccount remote interface and expands
Java.rmi.server.UnicastRemoteObject class:

Package mypackage;

Import java.rmi.RemoteException;
Import Java.rmi.server.UnicastRemoteObject;

public class Bankaccountimpl extends UnicastRemoteObject implements
BankAccount

{
private float balance = 0.0;

Public Bankaccountimpl (float initialbalance)
Throws RemoteException
{
balance = initialbalance;
}

public void deposit (float amount) throws RemoteException
{
...
}

public void withdraw (float amount) throws Overdrawnexception,
RemoteException
{
...
}

public float GetBalance () throws RemoteException
{
...
}
}


Note: When necessary, classes that implement remote interfaces can extend the
Some classes other than the Java.rmi.server.UnicastRemoteObject class. But implementing classes this
Must assume a certain responsibility, that is, the export of objects (by the UnicastRemoteObject constructor
Responsible) and implements the Hashcode, equals, and the inherited from the Java.lang.Object class
The correct remote semantics of the ToString method, if required.


2.6 parameter passing in a remote method call
The parameter passed to the remote object or the return value originating from it can be any serializable Java object. The bag

Java basic type, remote? Java objects and implementations of the Java.io.Serializable interface
A non-Remote Java object. For more information about how to serialize a class, see Java object serialization

Specification ". A class that is not locally available as a parameter or return value can be downloaded dynamically through the RMI system.

For more information about how to download parameters and return value classes for RMI read parameters, return values, and exceptions, the parameter

See the section "Dynamic Class Loading" (3.4).

2.6.1 passing non-remote objects
When a non-remote object is passed as a parameter of a remote method call or returned as a result of a remote method call,
is passed through replication, that is, the object is serialized using the Java object serialization mechanism.
Therefore, during a remote object invocation, when a non-remote object is passed as a parameter or return value, a non-remote
The contents of the object are replicated before the remote object is invoked.
When you return a non-remote object from a remote method call, a new object is created in the calling virtual machine.

2.6.2 Passing remote objects
When a remote object is passed as a parameter or return value of a remote method call, the stub program for the remote object
is passed out. A remote object passed as a parameter can only implement a remote interface.

Integrity of 2.6.3 References
If the two references of an object are in the form of arguments (or return values) in a single remote method call from the
A virtual machine is uploaded to another virtual machine, and they point to the same object in the sending virtual machine, two
A reference will point to the same copy of the object in the receiving virtual machine. It is further said that in a single remote party
In a method call, the RMI system retains the integrity of the reference in an object that is passed as a call parameter or return value

2.6.4 class Annotation
When an object is sent from a virtual machine to another virtual machine in a remote call, the RMI system calls the
The stream describes alphanumeric annotations with class information (URLs) to the class so that the class can be loaded on the receiver. In the FAR
The call can download the class at any time during the process method call.

2.6.5 parameter transmission
To serialize an RMI called parameter to a remote call's destination file, you need to write the parameter to the
Java.io.ObjectOutputStream in the stream of subclasses of the class. ObjectOutputStream Subclass
The Replaceobject method is overwritten to replace each remote object with its corresponding stub class.
The object parameters are written to the stream through the ObjectOutputStream WriteObject method. and
ObjectOutputStream the WriteObject method for each object in the stream (the package
Contains the object referenced by the Write object) calls the Replaceobject method. Rmiobjectoutputstream
The Replaceobject method of a subclass returns the following values:
If the object passed to Replaceobject is an instance of Java.rmi.Remote, it returns a remote pair
Like a stub program. The stub program for the remote object passes the
Java.rmi.server.RemoteObject.toStub
method is obtained by invoking the
If the object passed to Replaceobject is not an instance of Java.rmi.Remote, only returns
The object.
The ObjectOutputStream subclass of RMI also implements the Annotateclass method, which uses the class
Position annotation call stream so that the class can be downloaded in the receiver. about how to use the Annotateclass
For more information, see the "Dynamic Class loading" section.
Because the parameter writes to only one objectoutputstream, it points to a reference to the calling program's same object
The same copy of the object will be pointed at the receiver. On the receiver, the parameter will be a single
Read by ObjectInputStream.

ObjectOutputStream for writing objects (similar to those used for reading objects)
All other default behavior of ObjectInputStream) will remain in the parameter pass. For example, write to
The call to the Writereplace and the Readresolve when the object is read by the RMI
The parameter grouping and the solution flow are completed.

Similar to the RMI parameter passing method, the return value (or exception) is written to the
ObjectOutputStream
and is the same as the substitution behavior of the parameter transmission.


2.7 Locating remote Objects
We specialize in providing a simple boot-name server for storing named references to remote objects
。 A URL-based method that uses the class java.rmi.Naming can store remote object references.
To invoke a method of a remote object, the client must first get a reference to that object. An introduction to a remote object
is obtained in the form of a return value, usually in a method call. RMI system provides a simple boot name
Server, through which the remote objects on a given host are obtained. Java.rmi.Naming class provides based on the unified
A method of a resource locator (URL) that is used to bind, rebind, unlock, and list a host and
The name on the port-object pair.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.