0% found this document useful (0 votes)
18 views5 pages

Project 3

The document outlines the steps to create a Remote Method Invocation (RMI) application, starting with defining a remote interface that extends java.rmi.Remote and throws RemoteException. It details the implementation of the remote interface, the generation of stubs, starting the RMI registry, and creating both server and client applications to facilitate remote method calls. The instructions also include commands for compiling classes and running the server and client applications.

Uploaded by

robaa3874
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)
18 views5 pages

Project 3

The document outlines the steps to create a Remote Method Invocation (RMI) application, starting with defining a remote interface that extends java.rmi.Remote and throws RemoteException. It details the implementation of the remote interface, the generation of stubs, starting the RMI registry, and creating both server and client applications to facilitate remote method calls. The instructions also include commands for compiling classes and running the server and client applications.

Uploaded by

robaa3874
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/ 5

1.

Define the Remote Interface

The first step in creating an RMI application is to define a remote interface. This interface specifies the
methods that can be invoked remotely.

Key Points:

Extends java.rmi.Remote: The interface must extend java.rmi.Remote.

Methods Throw RemoteException: All methods declared in the interface must throw RemoteException,
which is a subclass of IOException.

Example: Remote Interface

java

import java.rmi.Remote;

import java.rmi.RemoteException;

public interface Hello extends Remote {

String sayHello() throws RemoteException;

2. Implement the Remote Interface

Next, you need to implement the remote interface. This implementation class will provide the actual
logic for the methods declared in the interface.

Key Points:

Extends UnicastRemoteObject: The implementation class typically extends UnicastRemoteObject, which


provides a simple way to create remote objects.
Constructor Throws RemoteException: The constructor of the implementation class must throw
RemoteException.

Example: Implementation Class

java

import java.rmi.RemoteException;

import java.rmi.server.UnicastRemoteObject;

public class HelloImpl extends UnicastRemoteObject implements Hello {

protected HelloImpl() throws RemoteException {

super();

@Override

public String sayHello() throws RemoteException {

return "Hello, world!";

3. Generate Stub and Skeleton

Historically, RMI required generating stub and skeleton files using the rmic tool. However, since Java 5,
the stub generation is automatic at runtime, so you don't need to manually run rmic unless you're using
an older version of Java.

Note:

Automatic Stub Generation: In modern Java versions, stubs are generated automatically when the server
starts.
4. Start the RMI Registry

The RMI registry is a service that allows clients to look up remote objects by name. You need to start the
registry before running the server.

Command:

bash

rmiregistry 1099

Port Number: The default port for RMI is 1099. You can specify a different port if needed.

5. Create and Run the Server Application

The server application creates an instance of the remote object and binds it to a name in the RMI
registry.

Key Points:

Use Naming.rebind(): Bind the remote object to a name using Naming.rebind().

Handle Exceptions: Handle any exceptions that might occur during binding.

Example: Server Application

java

import java.rmi.Naming;

public class Server {

public static void main(String[] args) {

try {

Hello hello = new HelloImpl();


Naming.rebind("rmi://localhost:1099/HelloService", hello);

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

} catch (Exception e) {

e.printStackTrace();

6. Create and Run the Client Application

The client application looks up the remote object by name and invokes its methods.

Key Points:

Use Naming.lookup(): Retrieve the remote object using Naming.lookup().

Cast to Remote Interface: Cast the retrieved object to the remote interface type.

Invoke Remote Methods: Call methods on the remote object.

Example: Client Application

java

import java.rmi.Naming;

public class Client {

public static void main(String[] args) {

try {

Hello hello = (Hello) Naming.lookup("rmi://localhost:1099/HelloService");


System.out.println(hello.sayHello());

} catch (Exception e) {

e.printStackTrace();

Running Instructions

Compile Classes: Compile all Java classes using javac.

Start RMI Registry: Run rmiregistry 1099 in a separate terminal window.

Run Server: Execute the server application using java Server.

Run Client: Execute the client application using java Client.

This setup allows the client to invoke methods on the server remotely using RMI.

You might also like