1. Creating and Binding a Remote Object to the Registry
importjava.rmi.registry.LocateRegistry;importjava.rmi.registry.Registry;importjava.rmi.server.UnicastRemoteObject;publicclassServer{publicstaticvoidmain(String[]args){try{ // Create a remote objectMyRemoteObject remoteObject =newMyRemoteObject(); // Create and start a registry on port 1099Registry registry =LocateRegistry.createRegistry(1099); // Bind the remote object to the registry with the name "MyRemoteObject"registry.bind("MyRemoteObject", remoteObject);System.out.println("Remote object bound to the registry");}catch(Exceptione){e.printStackTrace();}}privatestaticclassMyRemoteObjectimplementsRemoteInterface{@OverridepublicStringsayHello(Stringname){return"Hello, "+ name;}}}
2. Looking Up a Remote Object from the Registry
3. Using a Server Socket Factory to Customize Server Socket Creation
4. Using a Registry Handler to Customize Registry Behavior
5. Using a UnicastRemoteObject Stub for Remote Object Exports
6. Using a UnicastRemoteObject Exporter for Remote Object Exports
7. Using a custom object factory for registry lookups
8. Using a custom socket factory for registry connections
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
public static void main(String[] args) {
try {
// Create and start a registry on port 1099
Registry registry = LocateRegistry.getRegistry(1099);
// Look up the remote object bound to the name "MyRemoteObject"
RemoteInterface remoteObject = (RemoteInterface) registry.lookup("MyRemoteObject");
// Invoke a method on the remote object
String message = remoteObject.sayHello("John");
System.out.println("Message received from remote object: " + message);
} catch (Exception e) {
e.printStackTrace();
}
}
private interface RemoteInterface extends Remote {
String sayHello(String name) throws RemoteException;
}
}
import java.net.ServerSocket;
import java.net.ServerSocketFactory;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
public class ServerWithServerSocketFactory {
public static void main(String[] args) {
try {
// Create a custom server socket factory
ServerSocketFactory socketFactory = new MyServerSocketFactory();
// Create and start a registry using the custom socket factory
Registry registry = LocateRegistry.createRegistry(1099, null, socketFactory);
// Bind a remote object to the registry
registry.bind("MyRemoteObject", new MyRemoteObject());
System.out.println("Remote object bound to the registry");
} catch (Exception e) {
e.printStackTrace();
}
}
private static class MyRemoteObject implements RemoteInterface {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
private static class MyServerSocketFactory extends ServerSocketFactory {
@Override
public ServerSocket createServerSocket(int port) throws IOException {
// Customize the server socket creation here
return new ServerSocket(port);
}
}
}
import java.rmi.registry.Registry;
import java.rmi.registry.RegistryHandler;
public class CustomRegistryHandler implements RegistryHandler {
@Override
public Registry createRegistry(int port) throws RemoteException {
// Customize the registry creation here
return new MyRegistry(port);
}
private static class MyRegistry extends Registry {
public MyRegistry(int port) throws RemoteException {
super(port);
}
@Override
public RemoteLookup getRegistryServer() {
// Customize the registry server here
return new MyRegistryServer();
}
}
private static class MyRegistryServer extends RegistryServer {
public MyRegistryServer() {
super();
}
@Override
public boolean bind(String name, Remote object, boolean rebind) {
// Customize the binding logic here
return super.bind(name, object, rebind);
}
}
}
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
public class ServerWithUnicastRemoteObjectStub {
public static void main(String[] args) {
try {
// Create a registry on port 1099
Registry registry = LocateRegistry.getRegistry(1099);
// Create a remote object and export it using UnicastRemoteObject
MyRemoteObject remoteObject = new MyRemoteObject();
RemoteInterface stub = (RemoteInterface) UnicastRemoteObject.exportObject(remoteObject, 0);
// Bind the stub to the registry
registry.bind("MyRemoteObject", stub);
System.out.println("Remote object exported and bound to the registry");
} catch (Exception e) {
e.printStackTrace();
}
}
private static class MyRemoteObject implements RemoteInterface {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
}
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
public class ServerWithUnicastRemoteObjectExporter {
public static void main(String[] args) {
try {
// Create a registry on port 1099
Registry registry = LocateRegistry.getRegistry(1099);
// Create a remote object and export it using UnicastRemoteObject with a custom exporter
UnicastRemoteObjectExporter exporter = new MyUnicastRemoteObjectExporter();
MyRemoteObject remoteObject = new MyRemoteObject();
RemoteInterface stub = (RemoteInterface) exporter.exportObject(remoteObject, 0);
// Bind the stub to the registry
registry.bind("MyRemoteObject", stub);
System.out.println("Remote object exported and bound to the registry using a custom exporter");
} catch (Exception e) {
e.printStackTrace();
}
}
private static class MyRemoteObject implements RemoteInterface {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
private static class MyUnicastRemoteObjectExporter extends UnicastRemoteObjectExporter {
public MyUnicastRemoteObjectExporter() {
super();
}
@Override
public Remote toStub(Remote obj) {
// Customize the stub creation here
return super.toStub(obj);
}
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.registry.RegistryHandler;
public class ServerWithCustomObjectFactory {
public static void main(String[] args) {
try {
// Create a custom object factory
MyObjectFactory factory = new MyObjectFactory();
// Create and start a registry with the custom object factory
RegistryHandler handler = new MyRegistryHandler(factory);
Registry registry = LocateRegistry.createRegistry(1099, null, handler);
// Bind a remote object to the registry
registry.bind("MyRemoteObject", new MyRemoteObject());
System.out.println("Remote object bound to the registry with a custom object factory");
} catch (Exception e) {
e.printStackTrace();
}
}
private static class MyObjectFactory implements RegistryHandler {
@Override
public Registry createRegistry(int port) throws RemoteException {
// Customize the registry creation here
return new MyRegistry(port);
}
private static class MyRegistry extends Registry {
public MyRegistry(int port) throws RemoteException {
super(port);
}
@Override
public Remote lookup(String name) throws RemoteException, NotBoundException {
// Customize the lookup logic here
return super.lookup(name);
}
}
}
private static class MyRemoteObject implements RemoteInterface {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
}