Академический Документы
Профессиональный Документы
Культура Документы
References
1.
Cay S. Horstmann
Horstmann,, Core Java, Vol. 2: Advanced Features, 8th Edition,
2.
3.
Example
Assume code running in the local machine holds a remote reference to
an object object_1 on a remote machine
response = object_1.method();
object_1
Local Machine
method ( ) {
..
return (response);
}
Remote Machine
2. Remote reference
These are object references
Refer to remote objects, typically on a different computer
3. Remote interfaces
Normal Java Interfaces that specify API of a remote interface
They should extend java.rmi.Remote interface
The remote interface must known to local and remote code
java.rmi.Remote
Extends
Remote Interface
Is normal
Java interface,
java.rmi.Remote {
import java.rmi.*;
public interface SumServerIntf extends Remote {
int sum(int m, int n) throws RemoteException;
}
Figure : Remote interface
java.rmi.Remote
java.rmi.Remote
marker interface
It declares no methods or fields
java.rmi.RemoteException
RMI makes remote invocations look syntactically like local invocation.
In practice, though, it cannot defend from problems unique to distributed
computing unexpected failure of the network or remote machine.
Remote Objects
A remote object is one whose methods can be invoked from another JVM
Remote
Object
method
JVM
JVM
Remote
Interface
Class
implementing
Remote Interface
Instantiate
Remote
Object
UnicastRemoteObject
Extends
Remote
Interface
Class
Instantiate
Remote
Object
import java.rmi.*;
import java.rmi.server.*;
public class SumServerImpl extends UnicastRemoteObject implements SumServerIntf {
public SumServerImpl() throws RemoteException {
}
public int sum(int m, int n) throws RemoteException {
return m + n;
}
}
Remarks
If there is no explicit constructor invocation in the body of a subclass
constructor, it implicitly invokes super()
Hence the vital constructor of UnicastRemoteObject is called.
Exporting an Object
There are two choices when creating a remote object:
UnicastRemoteObject Class
Export
Remote Objects
Implicit export
Extend UnicastRemoteObject
Remote object can be exported
Explicit export
Call exportObject
exportObject():
():
explicitly export
UnicastRemoteObject:: methods
UnicastRemoteObject
1. exportObject
2. unexportObject
1. exportObject
2. exportObject
3. exportObject
4. unexportObject
This method makes the remote object unavailable for incoming calls.
If the force parameter is true, the object is forcibly unexported even if
there are pending calls to the remote object.
Naming class
Interact with the remote object registry: provides several
methods .
1.
bind
2.
rebind
3.
unbind
4.
lookup
5.
list
1. bind
Example:
bind ( SUM
SUM--SERVER
SERVER,
, RemoteObject );
2. rebind
Example:
rebind ( SUM
SUM--SERVER
SERVER,
, RemoteObject );
3. unbind
4. list
5. lookup
Returns a stub for the remote object associated with the specified name.
This method takes a string containing the URL to a remote object reference
on the server.
Example:
lookup ( rmi://localhost:1099
rmi://localhost:1099//SUM
SUM--SERVER
SERVER );
Distributed Objects
Simple idea objects existing on one machine (server) may be accessed
from another machine through regular method call.
Client Machine
Server Machine
Object
Server
Client
Same
interface as
object
State
Method
Proxy
Skeleton
invokes
same
method at
object
Skeleton
Interface
Server OS
Client OS
Client invokes
a method
RMI Server
3.
Call
Stub
Skeleton
RMI Client
Client Machine
4.
Return
Registry
2.
lookup
1.
bind
Stub
RMI
Client
2.
lookup
2.
RMI
Server
4.
Return
Skeleton
3.
Call
1. bind
Registry
1.
4. serializing the result back to the stub
SumServerIntf.java
import java.rmi.*;
public interface SumServerIntf extends Remote {
int sum(int m, int n) throws RemoteException;
}
SumServerImpl.java
import java.rmi.*;
import java.rmi.server.*;
public class SumServerImpl extends UnicastRemoteObject implements SumServerIntf {
public SumServerImpl() throws RemoteException {
}
public int sum(int m, int n) throws RemoteException {
return m + n;
}
}
A Server Program
The server must bind its name to the registry.
Use java.rmi.Naming class to bind the server name to registry. In this example
the name call SUM
SUM--SERVER
SERVER..
SumServer.java
import java.net.*;
import java.rmi.*;
public class SumServer {
public static void main(String args[]) {
try {
SumServerImpl sumServerImpl = new SumServerImpl();
Naming.rebind(SUM-SERVER", sumServerImpl);
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}
rmi://host:port/name
rmi
://host:port/name
Example: rmi://127.0.0.1:1099/SUM-SERVER
SumClient.java
import java.rmi.*;
public class SumClient {
public static void main(String args[]) {
try {
String sumServerURL = "rmi://" + args[0] + "/SUM-SERVER";
SumServerIntf sumServerIntf =
(SumServerIntf)Naming.lookup(sumServerURL);
int m = 5;
int n = 6;
System.out.println("The sum is: " + sumServerIntf.sum(m, n));
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}
import java.rmi.*;
public class SumClient {
public static void main(String args[]) {
try {
String sumServerURL = "rmi://" + args[0] + "/SUM-SERVER";
SumServerIntf sumServerIntf =
(SumServerIntf)Naming.lookup(sumServerURL);
System.out.println("The first number is: " + args[1]);
int m = Integer.valueOf(args[1]);
System.out.println("The second number is: " + args[2]);
int n = Integer.valueOf(args[2]);
System.out.println("The sum is: " + sumServerIntf.sum(m, n));
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}
javac SumServerIntf.java
javac SumServerImpl.java
javac SumServer.java
javac SumClient.java
rmic SumServerImpl
start rmiregistry
Steps 7 & 8: Start the remote server objects & Run the client
java SumServer
RMI
Applications
RMI
Server
A server creates remote objects.
Registers these objects with rmiregistry
RMI
Client
The client gets a handle by
looking up the remote object by
its name in the server(s) registry
and invokes methods on it
Client Machine
Server Machine
Send marshaled
data
Return
Marshaled result
or exception
RMI
Client
Return value
RMI Server
Return value
Call server
method locally
Skeleton
Stub
Call stub
method
RMI Registry
Client
Remote
Object
Server
Application
1. bind
bind:: The server is responsible for registering remote objects with the
registry service.
RMI Registry
Client
Remote
Object
Server
Application
RMI Registry
Client
Remote
Object
Server
Application
RMI Registry
Client
Remote
Object
Server
Application
RMI Registry
Client
Remote
Object
Server
Application
Java RMI
Creating and running registry service
By
CHITRAKANT BANCHHOR
Registry Service
There are two ways to start a registry:
Provided operations:
1.
2.
A user can register and view the names of already registered users.
Package: java.rmi.registry
Provides classes, interface s and implementations for elements of the RMI
object naming registry.
java.rmi.registry
Locate Registry
java.rmi
Remote
Final class
Interface
Registry
lookup,
binding,
rebinding,
listing the contents of a registry.
Remote )R
1.
bind
2.
rebind
3.
unbind
4.
lookup
5.
list
1. bind
public void bind ( String name, Remote obj )
2. rebind
public void rebind ( String name, Remote obj )
Purpose
Purpose:: rebinding of specified "name" to a remote object.
It replaces an object that is already bound with the registry with the new
object specified with obj.
3. unbind
public void unbind ( String name )
4. lookup
public Remote lookup ( String name )
Example:
lookup ( SUMSUM-SERVER )
5. list
public String list ( )
Class : LocateRegistry
The LocateRegistry class can be used to find or create a new registry
registry..
1. createRegistry
2. getRegistry
1. createRegistry
public static Registry createRegistry ( int port )
Purpose: to create and export a registry on the host that accepts requests
on the specified port
port..
2. getRegistry
public static Registry getRegistry ( )
Returns:
3. getRegistry
public static Registry getRegistry ( int port )
4. getRegistry
public static Registry getRegistry ( String host )
5. getRegistry
public static Registry getRegistry ( String host, int port )
6. getRegistry
public static Registry getRegistry ( String host, int port, RMIClientSocketFactory csf )
This method returns a locally created remote reference to the registry on the
specified host and port. Communication with the registry will use the
supplied RMIClientSocketFactory csf
csf..
Methods
1. registerUser
2. getRegisteredUsersList
Methods
1. registerUser
2. getRegisteredUsersList
Methods
registerUser
2. getRegisteredUsersList
The getRegisteredUsersList
getRegisteredUsersList()
() method returns a list of already registered
users.
(RegistryServerInt.java)
create an interface that extends the Remote interface.
RegistryServerImpl.java
regFile.txt
Rakesh
Rajesh
Ramesh
regFile.txt
Rakesh
Rajesh
Ramesh
1.
Server creates an instance of the remote object, which provides the service.
2.
userRegistry.rebind(REGISTRY-SERVER, regServerObject);
RegistryServer.java
Locate registry
2.
3.
RegistryClient.java
Java RMI
Servlets and RMI
CHITRAKANT BANCHHOR
Servlets
Servlets are Java classes that are loaded and executed by a web server.
How a servlet invokes remote methods.
SumServerIntf.java
SumServer Implementation
The SumServerImpl class extends UnicastRemoteObject in order to be
exported and implements the SumServerIntf
SumServerIntf..
SumServerImpl.java
SumServer class
The main() method in this class creates an instance of the SumServerImpl
(remote object).
Then binds SumServerImpl with the registry service.
SumServer.java
Servlet Client
First, servlets init() method is called and then it gets a reference to the
remote object.
Servlet Client
index.html
web.xml
SumServerClient.java
Execution
Create stub using rmic
References
George Reese, Database Programming with JDBC and JAVA, O'Reilly.
W. Clay Richardson et al, Professional Java, Wrox
What is JDBC?
Actually an Application Programming Interface (API)
117
JDBC components
1.
2.
3.
4.
The application
The driver manager
The driver
The data source
The application
Performs:
The driver
processes JDBC methods invocations,
sends SQL statements to a specific data source,
returns results back to the application.
Java application
1. Application
JDBC API
JDBC-ODBC Bridge
JDBC Driver
API
2. Driver Manager
Vendor specific
JDBC Driver
3. Driver
4. Data Source
Vendor specific
ODBC Driver
122
123
Execute a query
It is necessary to load the JDBC drivers before attempting to connect to the database.
The JDBC drivers automatically register themselves with the JDBC system when
loaded.
try {
Class. forName (
(sun.jdbc.odbc.JdbcOdbcDriver
sun.jdbc.odbc.JdbcOdbcDriver);
);
}
catch (ClassNotFoundException
(ClassNotFoundException cnfe
cnfe){
){
System.out.println(Error
System.out.println
(Error loading driver + cnfe
cnfe);
);
}
125
128
Execute a query
129
Example:
while( rs.next
while
rs.next() ) {
System.out.print(rs.getInt
rs.getInt(1
(1)));
System.out.print(rs.getString
rs.getString(
(SName
SName)
));
}
130
con.close();
con.close
Example - 1
132
Example -2
133
134
Example - 3
135
Assignment
RMI and JDBC Example
SelectServerIntf.java
SelectServerImpl.java
SelectServer.java
SelectClient.java
Example - 1
Example - 2
Example - 3
Thank You!
Start server
Program Modules
SelectRMIServer
SelectServerIntf.java
SelectServerImpl.java
SelectServer.java
SelectServletClient
Index.html
web.xml
ServletClient.java
RMI Layers
RMI Layers
Layers perform specific functions like establishing the connection, marshaling
of parameters, etc.
1.
2.
3.
4.
Application Layer
Proxy Layer
Remote Reference
Transport Layer
Application Layer
(Client)
Application Layer
(Server)
Proxy Layer
(stub)
Proxy Layer
(Skeleton)
Remote Reference
Layer
Virtual
Connection
Remote Reference
Layer
TCP
Transport Layer
Transport Layer
1. Application layer
The application layer is the actual implementation of the client and server
application.
2. Proxy layer
Application Layer
(Client)
Application Layer
(Server)
Proxy Layer
(stub)
Proxy Layer
(Skeleton)
Remote Reference
Layer
Virtual
Connection
Remote Reference
Layer
TCP
Transport Layer
Transport Layer
Stubs
A stub acts as a client(s) local representative for remote object.
The client invokes a method on the local stub. The stub is responsible for calling the
method on the remote object.
Skeleton
The skeleton acts as a server side proxy of the remote objects.
Application Layer
(Client)
Application Layer
(Server)
Proxy Layer
(stub)
Proxy Layer
(Skeleton)
Remote Reference
Layer
Virtual
Connection
Remote Reference
Layer
TCP
Transport Layer
Transport Layer
Application Layer
(Client)
Application Layer
(Server)
Proxy Layer
(stub)
Proxy Layer
(Skeleton)
Remote Reference
Layer
Virtual
Connection
Remote Reference
Layer
TCP
Transport Layer
Transport Layer
Application Layer
(Client)
Application Layer
(Server)
Proxy Layer
(stub)
Proxy Layer
(Skeleton)
Remote Reference
Layer
Virtual
Connection
Remote Reference
Layer
TCP
Transport Layer
Transport Layer
Transport Layer
Application Layer
(Client)
Application Layer
(Server)
Proxy Layer
(stub)
Proxy Layer
(Skeleton)
Remote Reference
Layer
Virtual
Connection
Remote Reference
Layer
TCP
Transport Layer
Transport Layer
Thank You!