public class RemoteObjectImpl implements RemoteObject {
@Override
public String getMessage(String name) throws RemoteException {
return "Hello, " + name;
}
}
try {
// Invoke a method on the remote object
System.out.println(stub.getMessage());
} catch (RemoteException e) {
// Handle the exception
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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!";
}
}
@Remote
public interface RemoteObject {
String getMessage() throws RemoteException;
}
@RemoteObject
public class RemoteObjectImpl implements RemoteObject {
@Override
public String getMessage() throws RemoteException {
return "Hello, world!";
}
}