Remote Method Invocation 1/2 “Hello World”
-
Upload
joshua-hernandez -
Category
Documents
-
view
68 -
download
2
description
Transcript of Remote Method Invocation 1/2 “Hello World”
![Page 3: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/3.jpg)
3
What is?• Networking
– moving files and data– run programs on another host
• Remote Method Invocation– a facility that allows Java programs
to call certain methods on a remote server
– another virtual machine– search engine– remote objects and methods work
just like the local ones
• Socket– applications-level protocols to
encode and decode messages cont
![Page 4: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/4.jpg)
4
– needs threaded Server
• RPC( Remote Procedure Call )– external data representation, such as
XDR – does not translate well into
distributed object systems
Cont’d
![Page 5: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/5.jpg)
5
Security
• Just as an applet– a host can limit what the remote
clients can do
– SecurityManager– Public key authentication
• allow different users different levels of access to a remote object
![Page 6: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/6.jpg)
6
Object Serialization
• Object reference– really transferred is a reference to
the object– problem
• the remote machine can’t read what’s in the memory of the local machine
• Two ways around this problem– a special remote reference to the
object• when the local machine passes a
remote object to the remote machine
– a copy of the object• when the local machine passes one of
its own objects to the remote machine
cont
![Page 7: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/7.jpg)
7
• To copy an object– convert the object into a stream of
bytes• more difficult than it appears at first
glance because objects can include other objects as fields
• these bytes can also be written to disk, and read back from disk at a later time
• For security reasons, some limitation on serializable– All Java primitive types and remote
objects can be serialized– non-remote objects can only be
serialized if they implement the java.io.Serializable interface
Cont’d
cont
![Page 8: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/8.jpg)
8
• Not Serializable– Threads, InputSreams,
OutputSreams, Peer classes, JDBC ResultSet, Most of the sun classes
• Interface java.io.Serializable – has no methods or fields and serves
only to identify the semantics of being serializable
Cont’d
![Page 9: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/9.jpg)
9
Under the Hood
• Three different mechanisms to pass arguments to and return resluts– primitive types( int, boolean…)
• passed by value
– reference to remote objects• remote reference
– objects that do not implement the Remote interface
• complete copies
– Objects that do not allow themselves to be serialized
cont
![Page 10: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/10.jpg)
10
• Compatibility with existing Java programs, Transparency to the programmer
Cont’d
Server Program
Skeleton
Remote Reference Layer
Transport Layer
Client Program
Stub
Remote Reference Layer
Transport Layer
The Internet
Logical Path
cont
![Page 11: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/11.jpg)
11
• Stub– a special object that implements the
remote interfaces of the remote object
– calling remote object, in fact calling an equivalent method in the stub
– passes the invocation into the remote reference layer
• Remote Reference Layer– Sometimes refers to multiple virtual
machines on multiple hosts
Cont’d
![Page 12: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/12.jpg)
12
Packages
• java.rmi– include exceptions that will be
visible on the client side
• java.rmi.server– include exceptions that will be
visible on the client side
• java.rmi.registry• java.rmi.dgc
– distributed garbage collection
![Page 13: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/13.jpg)
13
Hello World
• Four source files– The Java remote interface– The Java remote object (server)
which implements the remote interface
– The Java applet – The HTML code
![Page 14: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/14.jpg)
14
Remote Interface
• Why need?– So many problems in network
• Characteristics– must be public– extends java.rmi.Remote– throws
java.rmi.RemoteException– A remote object passed as an
argument or return value must be declared as the remote interface, not the implementation class
cont
![Page 15: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/15.jpg)
15
Cont’d
package examples.hello;public interface Hello extends java.rmi.Remote { String sayHello() throws java.rmi.RemoteException;}
Program Source
![Page 16: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/16.jpg)
16
Implementation Class
• Needs to– implements a interface– define a constructor– implements methods– create and install a
SecurityManager– create one or more instances of
remote object– register a remote object
cont
![Page 17: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/17.jpg)
17
Cont’d
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!"; }
Program Source
cont
![Page 18: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/18.jpg)
18
• implements a interface– java.rmi.server.UnicastRemo
teObject extends java.rmi.server.RemoteServer extends java.rmi.server.RemoteObject
Cont’d
public static void main(String args[]) { // Create and install a security manager 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(); } }}
cont
![Page 19: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/19.jpg)
19
• define a constructor– super( )
• java.rmi.server.UnicastRemoteObject, which "exports" the remote object
– java.rmi.RemoteException
• implements methods– the methods not specified in the
remote interface• can only be invoked within the virtual
machine running the service
• create and install a SecurityManager
Cont’d
cont
![Page 20: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/20.jpg)
20
• create one or more instances of remote objects
• register a remote object– like URL– default port:1099– For security reasons, an application
can bind or unbind only in the registry running on the same host
Cont’d
![Page 21: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/21.jpg)
21
Client Applet
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); }}
Program Source
![Page 22: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/22.jpg)
22
Generate Stubs and Skeletons
• rmic– rmic -d
$HOME/public_html/codebase examples.hello.HelloImpl
– it makes two files•HelloImpl_Stub.class •HelloImpl_Skel.class
![Page 23: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/23.jpg)
23
Start Registry and Server
• Start registry– rmiregistry &– rmiregistry 2001 &– must stop and restart the registry
any time you modify a remote interface, etc.
• Launch the server– java HelloImpl &
![Page 24: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/24.jpg)
24
Talking to registry
Stub
Hello Client
HelloImpl_Skel.class
HelloImpl_Stub.class
Registry
HelloImpl.class
Client
Server
lookup()where’s Hello
Hello is here
Send the stub
Here’s the Stub
sayHello()
“Hello”
![Page 25: Remote Method Invocation 1/2 “Hello World”](https://reader035.fdocuments.net/reader035/viewer/2022062517/568134fe550346895d9c49f9/html5/thumbnails/25.jpg)
25
References
• Java Network Programming– Elliotte Rusty Harold, 1997
O’REILLY
• JDK1.1.3 Documentation– JavaSoft, 1997, JavaSoft– www.javasoft.com/
• Client/Server Programming with JAVA and CORBA– Robert Orfali •Dan Harkey,
1997, WILLEY