Java Remote Method Call 3

zhaozj2021-02-08  445

Compute server

Examples of spending reports show how clients get attributes from the server. Attributes can be passed along both directions - the client can also pass the new type to the user. The simplest example is the computing server as shown in Figure 2, which can perform any task, so that the client within the entire enterprise can utilize high-end or dedicated computers.

The task is defined by a simple local (non-remote) interface:

Public interface task {

Object Run ();

}

When running, it will make some calculations and return an object containing the result. This is entirely general task - almost all computing tasks can be implemented under this interface. Remote Interface ComputeServer is also simple:

Import java.rmi. *;

Public Interface ComputeServer Extends Remote {

Object Compute (Task Task) THROWS RemoteException;

}

The unique purpose of this remote interface is to enable the client to create a task object and send it to the server, and finally return the result. The basic implementation of the server is as follows:

Import java.rmi. *;

Import java.rmi.server. *;

Public Class ComputeServerImpl

Extends UnicastRemoteObject

Implements ComputeServer

{

Public computeServerImpl () throws remoteException {}

Public Object Compute (Task Task) {

Return task.run ();

}

Public static void main (string [] args) throws exception {

// use the default, restrictive security manager

System.SetSecurityManager (New RMISecurityManager ());

ComputeServerImpl Server = New ComputeServerIMPL ();

Naming.Rebind ("ComputeServer", Server);

System.out.println ("Ready to Receive Tasks);

Return;

}

}

If you look at the Compute method, it will find that it is very simple. It just accepts the object passed to it and returns the result of the calculation. The main method includes the server's startup code - it installs the default security manager of RMI to prevent others from accessing the local system and create a ComputeServerImpl object that can process access, and associates with the name "ComputeServer". At this time, the server is ready to receive tasks to be executed, while Main has completed its settings.

As mentioned above, this is actually a comprehensive and practical service. The system can be improved, for example, to add parameters to be calculated, and billing the department using the service program. However, in many cases, the above interface and its implementation allow remote calculations using high-end computers. This is also the simplicity of RMI - if you type the above class, compile it, and start the service, you have a running calculation server that can perform any task.

Here is an example of using this computing service. Assume that you purchased a very high-end system that runs a lot of calculation of operating applications. Administrators can launch a Java virtual machine on that system and run the ComputeServerImpl object. This object now accepts tasks to run.

It is now assumed that a group is preparing to train a neural network through a set of data to help develop a procurement strategy. The steps they can use are as follows:

Define a class - temporarily called PURCHASENET. It accepts a set of data and runs training data, returns a trained neural network. The PurchaseNet will implement the Task interface and perform its work in its RUN method. They may also need a NEURON class to describe nodes in the returned network, and it is likely to require other classes to describe the process. The RUN method will return a NEURALNET object consisting of a set of training NEURON objects. When these classes are written and a small-scale test, use a PURCHASENET object to call the ComputeServer's Compute method.

When the RMI system in the ComputeServerImpl is received as a PURCHASENET object as an entry parameter, it downloads the implementation of the PURCHASENET, and calls the server's Compute method, and uses the object as the Task parameter.

Task, that is, the PurchaseNet object, will start the execution program. When executing programs require new classes such as Neuron and Neural, they can download it as needed.

All calculations will be executed on the computing server, while the client line is waiting for the results. (Another thread on the client system displays "Waiting" cursor or uses a built-in parallel mechanism using Java to execute another task). This object will be passed back to the client as the result of the Compute method when running back to the NeuralNet object.

This doesn't need to install other software on the server - all the tasks that must be completed are complete by each department on their own computer, and then transfer to the compute server host as needed.

This simple computing server architecture provides powerful conversion capabilities for system distributed functions. The calculation of a task can be transferred to a system that provides its best support. Such systems can be used:

On the host of the computeServerImpl, support the data mining application on the host with data mining needs. This allows you to easily move any calculations to where the data is located.

Run the calculation task on the server where the current stock price, shipping information, or other real-time information is available.

The task is distributed across multiple servers by running ComputeServer (accepted access to requests and transferring it to the server running ComputeServerImp).

proxy

Because RMI allows download properties to be implemented using Java, you can write a proxy system using RMI. The simplest format of the agent is as follows:

Import java.rmi. *;

Public interface agentserver extends remote {

Void Accept (Agent Agent)

THROWS RemoteException, InvalidAgentException;

}

Public Interface agent extends java.io.serializable {

Void Run ();

}

Starting a proxy also created an Agent interface, locating a server, activating the class that accepts the agent object. The agent's executor will be downloaded to the server. The Accept method will initiate a new thread of the agent, activate its RUN method, then return, so that the agent has always been executed to the RUN method. The agent is ported to the host by activating the ACCEPT method of the service program running on another host, and itself is transmitted as a proxy that will accept, and ends the thread on the original host.

Object-oriented code reuse and design pattern

Object-oriented programming is a powerful technology that allows code reuse. Many business organizations use object-oriented programming to mitigate the burden of the creation program and improve the flexibility of the system. RMI is the full-faced object-information being sent to the remote object, and the object can be passed and returned.

Design Patterns (Design Mode) is currently a considerable success in describing the practice of object-oriented design. The first is because of the innovation of Design Patterns, which is popular, which is a way to formally describe a complete method of solving a particular problem. All of these design patterns rely on creating one or more abstract concepts, which allow for different implementations, allowing and enhanced software reuse. Software reuse is the main advantage for object-oriented technology, and design model is one of the most popular technologies that promote reuse. All design patterns rely on object-oriented polymorphisms - this is an object (such as Task) with multiple implementation capabilities. The normal part of the algorithm (such as Compute Method) Don't have to know which implementation is used, it only needs to know what operations should be taken after you get an object. In particular, the compute server is an example of a Command mode, which allows you to expand the request (task) as an object and scheduling it.

Such polymorphism only when the full object including the execution program can be passed between the client and the server. Traditional RPC systems such as DCE and DCOM and CORBA-based RPC systems cannot download and execute programs because they cannot pass real objects as parameters, but can only pass data.

RMI can pass all types of execution programs, so you can use the object-oriented programming in a distributed computing solution, not just local computing - including design. If there is no system such as a full-scale object, you must abandon the design methods in many distributed computing systems - and other forms of object-oriented software reuse.

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

New Post(0)