0% found this document useful (0 votes)
4 views3 pages

RMI(2)

Uploaded by

berihufsaha12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

RMI(2)

Uploaded by

berihufsaha12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java programming Assignment (10%)

Instructions:

Due date: 22/08/2016 Geez Calendar


Read and understand the concept of RMI with clear examples below and prepare at least
two your own examples.
Be neat, we can’t give you a credit for examples that we cannot understand.
Submit on or before the due date.
Individual Work

What is RMI? How it Works?


RMI registry?

The remote Interface?

How can Client and Server Communicate?

Remote Method Invocation


RMI, or Remote Method Invocation, is a Java API that allows an object running in one Java
Virtual Machine (JVM) to invoke methods on an object running in another JVM, typically on a
different machine. It enables distributed computing and facilitates the development of client-
server applications in Java. RMI provides a transparent communication mechanism where the
client interacts with remote objects as if they were local. It involves defining remote interfaces,
implementing remote objects, and managing registry services for object lookup.

Sure, here's a simple example of RMI in Java:

First, define the remote interface:

import java.rmi.Remote;

import java.rmi.RemoteException;

public interface Hello extends Remote {

String sayHello() throws RemoteException;

}
Then, implement the remote interface:

import java.rmi.RemoteException;

import java.rmi.server.UnicastRemoteObject;

public class HelloImpl extends UnicastRemoteObject implements Hello {

public HelloImpl() throws RemoteException {

super();

public String sayHello() throws RemoteException {

return "Hello, from the server!";

Next, create and start the RMI Registry:

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

public class Server {

public static void main(String[] args) {

try {

Registry registry = LocateRegistry.createRegistry(1099);

HelloImpl obj = new HelloImpl();

registry.bind("Hello", obj);

System.out.println("Server ready");

} catch (Exception e) {

System.err.println("Server exception: " + e.toString());


e.printStackTrace();

Finally,create the Client:

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

public class Client {

public static void main(String[] args) {

try {

Registry registry = LocateRegistry.getRegistry("localhost", 1099);

Hello stub = (Hello) registry.lookup("Hello");

String response = stub.sayHello();

System.out.println("Response from server: " + response);

} catch (Exception e) {

System.err.println("Client exception: " + e.toString());

e.printStackTrace();

}:

This is a basic example illustrating how RMI works in Java. The server registers an object with
the RMI registry, and the client looks up the object in the registry and invokes methods on it
remotely.

You might also like