Contenido del Curso
Advanced Java 2077
Advanced Java 2077
Remote Method Invocation (RMI)
Remote Method Invocation (RMI) is a technology in Java that allows a program to invoke methods on an object that exists in another JVM (Java Virtual Machine). In other words, RMI allows a Java program to call a method on an object that exists in a different process or on a different machine as if it were calling a method on a local object.
How RMI works
RMI works by using a combination of Java's serialization and socket technologies. When a remote object is called, RMI serializes the arguments and sends them over the network to the remote object, which deserializes them and executes the method. The return value is then serialized and sent back to the calling process.
Defining the Remote Interface
The first step in using RMI is to define the remote interface that the server will implement, and the client will use it to invoke methods on the server. This interface must extend the java.rmi.Remote
interface, and each method must declare the java.rmi.RemoteException
.
Here's an example:
Main
import java.rmi.Remote; import java.rmi.RemoteException; public interface RemoteCalculator extends Remote { int add(int a, int b) throws RemoteException; int subtract(int a, int b) throws RemoteException; }
In this example, we define a remote interface called RemoteCalculator
that has two methods: add()
and subtract()
.
Implementing the Remote Interface
Once the remote interface is defined, we need to implement it on the server side. To do this, we create a class that implements the interface and extends the java.rmi.server.UnicastRemoteObject
class. This class is responsible for creating the actual objects that the client will call methods on.
Here's an example:
Main
import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class CalculatorImpl extends UnicastRemoteObject implements RemoteCalculator { public CalculatorImpl() throws RemoteException { super(); } public int add(int a, int b) throws RemoteException { return a + b; } public int subtract(int a, int b) throws RemoteException { return a - b; } }
In this example, we define a class called CalculatorImpl
that implements the RemoteCalculator
interface. This class extends the java.rmi.server.UnicastRemoteObject
class, which provides some of the necessary infrastructure for RMI.
Registering the Remote Object
Once the remote object is implemented, it needs to be registered with the RMI registry so that the client can find it. This is typically done on the server side during startup.
Here's an example:
Main
import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class Server { public static void main(String[] args) { try { // create an instance of the remote object RemoteCalculator calculator = new CalculatorImpl(); // start the RMI registry Registry registry = LocateRegistry.createRegistry(1099); // bind the remote object to the registry registry.rebind("Calculator", calculator); System.out.println("Server started"); } catch (Exception e) { System.err.println("Server exception: " + e.getMessage()); e.printStackTrace(); } } }
In this example, we create an instance of the CalculatorImpl
class and bind it to the RMI registry using the registry.rebind()
method. The name Calculator
is used to identify the remote object in the registry.
Invoking Remote Methods
Once the remote object is registered, the client can use it by obtaining a reference to it from the RMI registry.
Here's an example of how a client might use the RemoteCalculator
interface to call methods on the remote object:
Main
import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class Client { public static void main(String[] args) { try { // get a reference to the remote object from the registry Registry registry = LocateRegistry.getRegistry("localhost", 1099); RemoteCalculator calculator = (RemoteCalculator) registry.lookup("Calculator"); // call methods on the remote object int result1 = calculator.add(3, 4); int result2 = calculator.subtract(10, 5); System.out.println("Result1: " + result1); System.out.println("Result2: " + result2); } catch (Exception e) { System.err.println("Client exception: " + e.getMessage()); e.printStackTrace(); } } }
In this example, we obtain a reference to the remote object using the registry.lookup()
method and then call methods on the remote object as if it were a local object.
¡Gracias por tus comentarios!