Dagim T.
asked 01/08/15hi there, is any body can help me about java rmi server,client ,databse
1 Expert Answer
Henry T. answered 11/30/23
Experienced Java Tutor: CS Graduate with 5+ Years Professional Ex
Hello! I'd be glad to help you with Java RMI (Remote Method Invocation). RMI is a Java API that allows an object to invoke methods on an object running in another Java Virtual Machine. It's a key concept for building distributed applications in Java.
Basic Concepts:
RMI Server: This is the application that contains the remote objects. The server provides the implementation of the remote interfaces.
RMI Client: The client application obtains a remote reference to one or more remote objects on the server and invokes methods on them.
Remote Interface: A remote interface in Java RMI must extend java.rmi.Remote and declares the methods that can be called remotely.
Stub and Skeleton: In RMI, a stub for a remote object acts as a client-side proxy, while the skeleton is the server-side proxy. However, in modern Java versions, skeletons are no longer used.
Registry: RMI uses a registry service (usually rmiregistry) to look up remote objects.
How It Works:
Define a Remote Interface: Create an interface that extends java.rmi.Remote. Each method must declare java.rmi.RemoteException in its throws clause.
Implement the Remote Interface: Create a class that implements the remote interface. This class is the actual implementation of the remote service.
Create a Server Application: The server application creates instances of the remote object, then registers one or more of these instances with the RMI registry.
Create a Client Application: The client looks up the remote object in the registry and invokes methods on it.
Database Integration: If your application requires a database, you will typically handle database interactions on the server-side.
Example:
Here's a very simple example to illustrate these concepts:
Remote Interfaceimport java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
Implementation
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
public class HelloImpl extends UnicastRemoteObject implements Hello {
protected HelloImpl() throws RemoteException {
super();
}
public String sayHello() throws RemoteException {
return "Hello, world!";
}
}
Server
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 implementation class
HelloImpl obj = new HelloImpl();
// Bind this instance to the name "Hello"
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", obj);
System.out.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
Client
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private Client() {}
public static void main(String[] args) {
try {
// Obtain a reference to the object from the registry and cast it to the remote interface class
Registry registry = LocateRegistry.getRegistry(null);
Hello stub = (Hello) registry.lookup("Hello");
// Call the remote method
String response = stub.sayHello();
System.out.println("response: " + response);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Anthony F.
11/09/15