Java remote method call 4

zhaozj2021-02-08  440

Connection to existing servers

It is often said that RMI is mainly "from Java to Java", but this statement covers the fact that Java can be connected to existing and original system connections using a native method interface called JNI. The mixing of JNI and RMI is as simple as any other Java program. You can use JDBC to connect to RMI, connect with an existing relational database. That is, you can use the RMI to connect two and three-level system - even if both parties are not written in Java. This has a great advantage and advantage, and will be described in detail below. But first let's take a look at how it is done.

Suppose you have a server that stores the customer order information in the relational database. In any multi-level system, you have to design a remote interface to facilitate client access servers - using RMI as the REMOTE interface:

Import java.rmi. *;

Import java.sql.sqlexception;

Import java.util.vector;

Public interface orderserver extends remote {

Vector getunpaid () throws remoteption, sqlexception;

Void shutdown () throws remoteexception;

// ... Other Methods (getorderNumber, getshipped, ...)

}

Java.sql package contains JDBC packages. Each remote method can be called by the server using the actual database JDBC, or by using the native method of other database access mechanisms. The method shown above returns a VECTOR of an Order object. ORDER is defined in your system to save the class.

This section describes how to implement GetUnpaid using JDBC, and how to implement Shutdown using JNI.

JDBC - Connecting Database

Use JDBC to implement the orderserverImpl of Getunpaid as follows:

Import java.rmi. *;

Import java.rmi.server. *;

Import java.sql. *;

Import java.util.vector;

Public Class OrderServerImpl

Extends UnicastRemoteObject

Implements ORDERSERVER

{

Connection DB; // Connection to the DB

PreparedStatement unpaidQuery; // unpaid order query

ORDERSERVERIMPL () throws receexception, sqlexception {

DB = DriverManager.getConnection ("JDBC: ODBC: Orders");

UnpaidQuery = db.preparedStatement ("...");

}

Public Vector getunpaid () throws sqlexception {

ResultSet Results = unpaidQuery.executeQuery ();

Vector List = new vector ();

While (results.next ())

List.addelement (New Order (Results);

Return List;

}

Public native void shutdown ();

}

Most of them are JDBC tasks. In addition to the type of Order is part of your system, all types you see are part of JDBC or RMI. The constructor initializes the ORDERSERVERIMPL object to create a connection to the database specified in the JDBC URL. With this connection, we can use PrepareStatement to define a query that can find all unpaid orders. Other queries can also be defined for other methods. ORDERSERRERVERIMPL is running on the same system as a database, and may also be in the same process (SHUTDOWN will now). When the getUnpaid method is called on the RMI server object ORDERRERVERIMPL, a pre-compiled query is performed and a JDBC Resultset object containing all matching elements is returned. Subsequently, we create a new ORDER object for each project in the result set and add it to the Vector object (Dynamic array of Java). After ending the read result, we return this vector to the client, and the latter can display the results to the user, or other related people.

JNI - Machine Method

RMI servers and clients can be connected to existing and original systems using this unit. You can use the native method to implement remote methods that cannot directly access the database, or easier to implement by using existing code. You can write C and C programs using this unit interface JNI to implement this method on the Java method and the Java object. The procedure for implementing the shutdown is as follows:

Jniexport void Jnicall

Java_OrderServerImpl_shutdown (jnienv * env, jobject this)

{

JCLASS CLS;

JFIELDID FID;

DataSet * DS;

CLS = (* ENV) -> GetObjectClass (ENV, this);

FID = (* ENV) -> GetfieldID (ENV, CLS, "Dataset", "J");

DS = (Dataset *) (* ENV) -> GetObjectField (ENV, THIS, FID);

/ * With a Dataset Pointer We can use the Original API * /

DSSHUTDOWN (DS);

}

This is assumed that the existing server has been referenced by the DataSet type defined by its API. Pointer pointing to the server Dataset is stored in the DataSet field. When the client calls ShutDown, the server's shutdown method will be called. Since the server implementation is declared in the server implementation to implement the shutdown method, the RMI will directly call this native method. This machine method finds the DataSet field of the object, get its value, and use it to call the existing API function DSSHUTDOWN.

Sun is currently working with Ilog companies to develop a product called TwinPeaks. TwinPeaks will be able to compatibility with the current C and C APIs and generate Java classes, which contains calls to the API in the Java class. This way you can call any existing API from Java. After TWINPEAKS, it will be possible to completely use Java (rather than JNI calls). Methods such as Shutdown.

Architecture

The RMI system provides a simple and direct foundation for distributed object-oriented computing. Its architecture can allow the server and the reference type to extend the RMI to add functions in a continuous manner.

When the service program is output, its reference type is defined. In the above example, we output the server as a UnicastRemoteObject server, which is the point to the point non-copy server. Quote for these objects is very suitable for such servers. Different types of servers have different reference syntax. For example, MulticastRemoteObject has a reference syntax that allows replication services. When the client receives the reference to the server, RMI will download a call to the call to the server-oriented remote call. As shown in FIG. 3, the stub uses the object serialization method to group the parameter into the method, and send the group to the server through the network. At the server side, the RMI system receives calls and connects to a frame, and the framework is responsible for unpacking the parameter group and calls the implementation of the method on the server. When the execution of the server is completed, the framework sends a response by grouping a value or throwing an exception, the framework sends a response to the server's stub. The stub relieves the response group and returns a value or throws an exception as needed. The stub and the frame is generated by the server's implementation, usually used is the program RMIC. The stub uses a reference to the framework. This architecture enables the reference to define the properties of the communication. The reference to the UnicastRemoteObject server communicates with a single server object running on a particular host and port. With the separation function of the stub / reference, RMI can add a new reference type. The reference to handling the replication server can send the server request multiplex to a set of correct replication programs, collect the response, and then return the correct result according to a variety of responses. If the service program is not running on a virtual machine, another reference type can activate the program. The client can work with all of these reference types.

Confidentiality and security

Safe is absolutely guaranteed when the RMI request is executed. RMI can provide security channels between clients and servers, and can put the downloaded executor running in a secure Sandbox, protecting your system from unknown client possible attacks.

First, you must define your security needs, which is very important. If you are implementing programs such as ComputeServer inside the enterprise network, you just need to know who is using the computing loop so you can track the abuse of the system. If you need a business computing server, you need to prevent a variety of malicious destruction. These will affect the design of the interface - in the company, you may only require each Task object to come with the human name and department number for tracking. In business sectors, you may need higher security, including digital signature identification, and some contract languages ​​that can help you eliminate malicious tasks that will exceed their time.

There may be a security channel between the client and the server. RMI allows you to provide a socket factory that can create any type of jack you need, including encryption sockets. Starting with JDK 1.2, you will be able to specify the requirements of the services provided by the server socket (by giving a description of these requirements). This new technology can be used in small applications, and most browsers refuse to set up a socket plant. The socket requirements can include encryption and other requirements.

There is also a security problem in the downloaded class. Java processes security issues through SecurityManager objects, and this object can pass all judgments related to security, such as opening files and network connections. RMI uses this standard Java mechanism by requesting you to install the security manager before you output any service object or call any method on the server. RMI provides a small application (no file access, just connecting to a host, etc.) to limit the same RMISECURityManager type. This prevents the downloaded executing program from reading or writing data from the computer, or connects to other systems behind the firewall. You can also write and install your own security management program to perform different security restrictions.

Firewall

RMI provides a method of communicating with a remote server for a client behind a firewall. This allows you to deploy a customer using RMI on the Internet, such as in a small application on the World Wide Web. The firewall through the client will reduce the communication speed, so the RMI successfully adopts the fastest technology to connect the client and server. When the client first tries the following three possible methods, attempting to establish communication with the server, the technology is directly communicated with the server's ports discovered by UnicastRemoteObject reference.

If it fails, the URL connected to the server host and port is established, and the HTTP POST request is used on the URL, and the information is sent to the frame as the main body of the POST. If successful, the result of the POST is the response of the frame to the stub.

If it fails, use port 80 (standard HTTP port) to establish a URL connected to the server host and can be used? The MI request is sent to the CGI program of the server.

Which technology is first successful, and will be used first to communicate with the server. If these technologies are unsuccessful, the remote method call failed.

This three-step strategy enables the client to communicate with as high as possible, and mostly use direct jack connections. On systems where the firewall is installed, or in the communication behind the internal firewall, the client will connect directly to the server using the socket. The speed of secondary communication technologies is much slower than direct communication, but allows you to write a widely used client program that can be widely used in Internet and World Wide.

转载请注明原文地址:https://www.9cbs.com/read-887.html

New Post(0)