# javax.rmi

***

**1. Creating an RMI Server**

```java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class RMIServer {
    public static void main(String[] args) {
        try {
            // Create a remote object
            RemoteObjectImpl remoteObject = new RemoteObjectImpl();

            // Export the remote object
            RemoteObject stub = (RemoteObject) UnicastRemoteObject.exportObject(remoteObject, 0);

            // Create a registry
            Registry registry = LocateRegistry.createRegistry(1099);

            // Register the remote object with the registry
            registry.bind("RemoteObject", stub);

            System.out.println("Server running...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**2. Creating an RMI Client**

```java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class RMIClient {
    public static void main(String[] args) {
        try {
            // Create a registry
            Registry registry = LocateRegistry.getRegistry("localhost", 1099);

            // Lookup the remote object
            RemoteObject stub = (RemoteObject) registry.lookup("RemoteObject");

            // Invoke a method on the remote object
            System.out.println(stub.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**3. Passing Arguments to RMI Methods**

```java
import java.rmi.RemoteException;

public interface RemoteObject {
    String getMessage(String name) throws RemoteException;
}
```

```java
public class RemoteObjectImpl implements RemoteObject {
    @Override
    public String getMessage(String name) throws RemoteException {
        return "Hello, " + name;
    }
}
```

**4. Handling Exceptions in RMI**

```java
try {
    // Invoke a method on the remote object
    System.out.println(stub.getMessage());
} catch (RemoteException e) {
    // Handle the exception
}
```

**5. Using RMI with JNDI**

```java
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JNDIClient {
    public static void main(String[] args) {
        try {
            // Create a JNDI context
            Context context = new InitialContext();

            // Lookup the remote object
            RemoteObject stub = (RemoteObject) context.lookup("java:rmi://localhost:1099/RemoteObject");

            // Invoke a method on the remote object
            System.out.println(stub.getMessage());
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}
```

**6. Using RMI with SSL**

```java
import java.rmi.server.RMISecurityManager;
import java.rmi.server.RemoteObject;
import java.rmi.server.RMISocketFactory;
import java.rmi.server.UnicastRemoteObject;

public class SSLRMIServer {
    public static void main(String[] args) {
        try {
            // Set the security manager
            System.setSecurityManager(new RMISecurityManager());

            // Create a socket factory
            RMISocketFactory socketFactory = new SSLRMISocketFactory();

            // Create a remote object
            RemoteObjectImpl remoteObject = new RemoteObjectImpl();

            // Export the remote object
            RemoteObject stub = (RemoteObject) UnicastRemoteObject.exportObject(remoteObject, 0, socketFactory, socketFactory);

            // Create a registry
            Registry registry = LocateRegistry.createRegistry(1099);

            // Register the remote object with the registry
            registry.bind("RemoteObject", stub);

            System.out.println("Server running...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**7. Using RMI with CORBA**

```java
import javax.rmi.CORBA.Stub;

public class CORBARMIClient {
    public static void main(String[] args) {
        try {
            // Get the CORBA object reference
            org.omg.CORBA.Object objRef = ...;

            // Narrow the object reference to a remote object
            RemoteObject stub = (RemoteObject) Stub.unbox(objRef);

            // Invoke a method on the remote object
            System.out.println(stub.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**8. Using RMI with SOAP**

```java
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;

public class SOAPRMIClient {
    public static void main(String[] args) {
        try {
            // Create a service factory
            ServiceFactory factory = ServiceFactory.newInstance();

            // Create a service
            Service service = factory.createService(new URL("http://localhost:8080/RemoteObject?wsdl"));

            // Get the port
            RemoteObject stub = (RemoteObject) service.getPort(RemoteObject.class);

            // Invoke a method on the remote object
            System.out.println(stub.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**9. Using RMI with REST**

```java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/remote-object")
public class RESTRMIServer {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getMessage() {
        return "Hello, world!";
    }
}
```

**10. Using RMI withAnnotations**

```java
@Remote
public interface RemoteObject {
    String getMessage() throws RemoteException;
}
```

```java
@RemoteObject
public class RemoteObjectImpl implements RemoteObject {
    @Override
    public String getMessage() throws RemoteException {
        return "Hello, world!";
    }
}
```
