rmi components java.rmi: client-side rmi classes, interfaces, and exceptions java.rmi.server:...
TRANSCRIPT
![Page 1: RMI Components java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry:](https://reader036.vdocument.in/reader036/viewer/2022082319/56649e405503460f94b31bd6/html5/thumbnails/1.jpg)
RMI Components
• java.rmi: client-side RMI classes, interfaces, and exceptions
• java.rmi.server: server-side RMI classes, interfaces, and exceptions
• java.rmi.registry: Classes for naming services
• java.rmi.dgc: distribute garbage collection
![Page 2: RMI Components java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry:](https://reader036.vdocument.in/reader036/viewer/2022082319/56649e405503460f94b31bd6/html5/thumbnails/2.jpg)
• java.rmi.activation: Implements activate on demand RMI services. Objects instantiated on-demand by client-requests. References persistent over server crashes.
• rmic: compiler to generate stubs and skeletons• rmiregistry: Utility server that provides
naming service for RMI. Associates names with objects
• rmid: utility server to RMI activation framework
![Page 3: RMI Components java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry:](https://reader036.vdocument.in/reader036/viewer/2022082319/56649e405503460f94b31bd6/html5/thumbnails/3.jpg)
RMI Server Properties
• java.rmi.server.codebase: a url indicating code base from which classes should be loaded to network clients
• java.rmi.server.disableHttp: if true, RMI will not use Http to try to tunnel through firewalls. Default is false, meaning that Http wraps RMI calls
![Page 4: RMI Components java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry:](https://reader036.vdocument.in/reader036/viewer/2022082319/56649e405503460f94b31bd6/html5/thumbnails/4.jpg)
• java.rmi.server.hostname: sets servers fully qualified host name, if name unavailable via DNS
• java.rmi.server.logCalls: if true, RMI will log information about calls. Default is false.
• java.rmi.dgc.leaseValue: The time in milliseconds until server notices client is no longer connected. Default is 10 minutes.
![Page 5: RMI Components java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry:](https://reader036.vdocument.in/reader036/viewer/2022082319/56649e405503460f94b31bd6/html5/thumbnails/5.jpg)
RMI Naming Methods
• Remote lookup(String name): Look up remote object by URL and return it
• bind(String name, Remote obj): bind an object to a specific URL
• unbind(String name): unbind an object at a URL
• rebind(String name, Remote obj): Replace the object currently bound with a new one
• String[] list(String name): list of URL’s from specified Registry
![Page 6: RMI Components java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry:](https://reader036.vdocument.in/reader036/viewer/2022082319/56649e405503460f94b31bd6/html5/thumbnails/6.jpg)
Creating RMI Application
• Create and compile remote object interface that extends java.rmi.Remote
• Implement a server application that contains a remote object class that implements your interface
• Compile the server application and run rmic to generate stubs classes.
![Page 7: RMI Components java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry:](https://reader036.vdocument.in/reader036/viewer/2022082319/56649e405503460f94b31bd6/html5/thumbnails/7.jpg)
• Install the class files in a known location, such as web root directory. RMI by default uses web server to for sharing classes.
• Run rmiregistry on the server machine so it can manage remote objects
• Run the server application and have it install an instance of the remote object with a unique name in the registry.
• Implement a client application that connects to the server and looks up the remote object by name in a remote object registry.
• Compile, then Run client
![Page 8: RMI Components java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry:](https://reader036.vdocument.in/reader036/viewer/2022082319/56649e405503460f94b31bd6/html5/thumbnails/8.jpg)
package message;
import java.rmi.Remote;import java.rmi.RemoteException;
public interface MessageServer extends Remote{
static String SERVER_NAME = "Message_Server";
void send(String name, String s) throws RemoteException;}
![Page 9: RMI Components java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry:](https://reader036.vdocument.in/reader036/viewer/2022082319/56649e405503460f94b31bd6/html5/thumbnails/9.jpg)
package server;
import java.rmi.*;import java.rmi.server.*;import java.rmi.registry.*;import message.*;
public class Server extends UnicastRemoteObject implements MessageServer{ public Server() throws RemoteException
{ super(); }
public void send(String name, String s) throws RemoteException{
System.out.println(name + ": " + s); }
public static void main(String[] args) { System.setSecurityManager(new RMISecurityManager());
try { MessageServer srv = new Server();
// Bind server to local registry on default port Naming.rebind(MessageServer.SERVER_NAME, srv);
System.out.println("Server bound and started"); } catch (Exception e) { System.err.println("Server exception: " + e.getMessage()); e.printStackTrace(); } }}
![Page 10: RMI Components java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry:](https://reader036.vdocument.in/reader036/viewer/2022082319/56649e405503460f94b31bd6/html5/thumbnails/10.jpg)
package client;
import java.io.*;import java.rmi.*;import java.rmi.server.*;import message.*;
public class Client{
static String name; // name of this client static MessageServer server; //Connection to the server
public static void main(String args[]){
if(args.length<1){
System.out.println("Usage: java client.Client ClientName");System.exit(-1);
}
name = args[0];
System.setSecurityManager(new RMISecurityManager());
![Page 11: RMI Components java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry:](https://reader036.vdocument.in/reader036/viewer/2022082319/56649e405503460f94b31bd6/html5/thumbnails/11.jpg)
try{
server =(MessageServer)Naming.lookup(MessageServer.SERVER_NAME);
//Now, loop and process input from the user untilthey type 'exit'
boolean notDone = true;String message = null;BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Type 'exit' to end.");
while(notDone){
System.out.print("Enter message:"); message = br.readLine();
if(message.startsWith("exit")) notDone=false;else server.send(name,message);
} } catch (Exception e)
{ System.err.println("Client exception: " + e.getMessage()); e.printStackTrace();
System.exit(-1); } }}
![Page 12: RMI Components java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry:](https://reader036.vdocument.in/reader036/viewer/2022082319/56649e405503460f94b31bd6/html5/thumbnails/12.jpg)
grant{
permission java.net.SocketPermission "*:1024-65535", "connect,accept";permission java.lang.RuntimePermission "setSecurityManager";permission java.lang.RuntimePermission "createSecurityManager";
};
![Page 13: RMI Components java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry:](https://reader036.vdocument.in/reader036/viewer/2022082319/56649e405503460f94b31bd6/html5/thumbnails/13.jpg)
javac message\*.java
javac server\*.javarmic -d . server.Server
javac client\*.java
REM Start local registrystart rmiregistry
REM start java server.Serverstart java -Djava.security.policy=server.policy server.Server
REM give the server a chance to startpause
REM start a few clientsstart java -Djava.security.policy=server.policy client.Client Client1start java -Djava.security.policy=server.policy client.Client Client2start java -Djava.security.policy=server.policy client.Client Client3
![Page 14: RMI Components java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry:](https://reader036.vdocument.in/reader036/viewer/2022082319/56649e405503460f94b31bd6/html5/thumbnails/14.jpg)
package examples.hello;public interface Hello extends java.rmi.Remote {
String sayHello() throws java.rmi.RemoteException;}
![Page 15: RMI Components java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry:](https://reader036.vdocument.in/reader036/viewer/2022082319/56649e405503460f94b31bd6/html5/thumbnails/15.jpg)
package examples.hello;
import java.rmi.*;import java.rmi.server.UnicastRemoteObject;
public class HelloImpl extends UnicastRemoteObject implements Hello {private String name;
public HelloImpl(String s) throws RemoteException {super();name = s;
}
public String sayHello() throws RemoteException {return "Hello World!";
}
public static void main(String args[]){
System.setSecurityManager(new RMISecurityManager());
try {HelloImpl obj = new HelloImpl("HelloServer");Naming.rebind("//myhost/HelloServer",obj);System.out.println("HelloServer bound in registry");
} catch (Exception e) {System.out.println("HelloImpl err: " +e.getMessage());e.printStackTrace();
}}
}
![Page 16: RMI Components java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry:](https://reader036.vdocument.in/reader036/viewer/2022082319/56649e405503460f94b31bd6/html5/thumbnails/16.jpg)
package examples.hello;
import java.awt.*;import java.rmi.*;
public class HelloApplet extends java.applet.Applet {String message = "";public void init() {
try {Hello obj = (Hello)Naming.lookup("//" +
getCodeBase().getHost() + "/HelloServer");message = obj.sayHello();
} catch (Exception e) {System.out.println("HelloApplet exception: " +e.getMessage());e.printStackTrace();
}}public void paint(Graphics g) {
g.drawString(message, 25, 50);}
}