- Distributed Computing in Java 9
- Raja Malleswara Rao Pattamsetti
- 380字
- 2021-07-02 21:02:34
Implementing a remote interface
The next step you need to take to define the remote interface is this: implement the remote interface by a class that could interact in the RMI. When the remote interface is implemented, the constructor of this remote object needs to invoke the superclass constructor and override all the unimplemented methods.
From the infrastructure setup perspective, we should create and install a security manager, create and export one or more remote objects, and register at least one remote object with the RMI registry or Java naming convention and directory interface for reference.
The following is the CalculateEngine class implementing the Calculate remote interface:
package remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class CalculateEngine extends UnicastRemoteObject implements Calculate {
private static final long serialVersionUID = 1L;
public CalculateEngine() throws RemoteException {
super();
}
@Override
public long add(long parameterOne, long parameterTwo) throws
RemoteException {
return parameterOne + parameterTwo;
}
@Override
public long sub(long parameterOne, long parameterTwo) throws
RemoteException {
return parameterOne - parameterTwo;
}
@Override
public long mul(long parameterOne, long parameterTwo) throws
RemoteException {
return parameterOne * parameterTwo;
}
@Override
public long div(long parameterOne, long parameterTwo) throws
RemoteException {
return parameterOne / parameterTwo;
}
}
After you define the remote interface and implementation, write the standalone Java program through which you will start the remote server, as follows:
package remote;
import java.rmi.Naming;
import java.rmi.Remote;
public class MyServer implements Remote{
public static void main(String[] args) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
Naming.rebind("rmi://localhost:5000/calculate",new
CalculateEngine());
System.out.println("CalculateEngine bound");
}
catch (Exception e) {
System.err.println("CalculateEngine exception:");
e.printStackTrace();
}
}
}
In the preceding program, the Naming method rebind() is used to bind the remote object with the name. The following is the list of methods Naming offers with their description:

Let's review all the steps we have taken to define the remote application:
- We declared the remote interfaces that are being implemented
- We defined a constructor for the remote object
- We provided implementations for each remote method
- We passed the objects in RMI
- We implemented the server's main method
- We created and installed a security manager
- We made the remote object available to the clients
The next step is to generate a client program that would interact with the remote object through the RMI.
- C#高級編程(第10版) C# 6 & .NET Core 1.0 (.NET開發經典名著)
- 零基礎學Scratch少兒編程:小學課本中的Scratch創意編程
- HTML5+CSS3基礎開發教程(第2版)
- Teaching with Google Classroom
- Learning ArcGIS for Desktop
- 大數據分析與應用實戰:統計機器學習之數據導向編程
- ASP.NET程序開發范例寶典
- Web編程基礎:HTML5、CSS3、JavaScript(第2版)
- Python面試通關寶典
- Visual C#(學習筆記)
- Expert Cube Development with SSAS Multidimensional Models
- Mastering Linux Kernel Development
- 計算機視覺增強現實應用平臺開發
- Java到Kotlin:代碼重構指南
- Professional Azure SQL Database Administration