Ajp U4
Ajp U4
Client-Server Model
The Client-server model is a distributed application structure that partitions
tasks or workloads between the providers of a resource or service, called
servers, and service requesters called clients. In the client-server architecture,
when the client computer sends a request for data to the server through the
internet, the server accepts the requested process and delivers the data
packets requested back to the client. Clients do not share any of their
resources. Examples of the Client-Server Model are Email, World Wide Web,
etc.
In this article, we are going to take a dive into the Client-Server model and
have a look at how the Internet works via, web browsers. This article will help
us have a solid WEB foundation and help us easily work with WEB
technologies.
So, it is the Client requesting something and the Server serving it as long as
it is in the database.
The RMI provides remote communication between the applications using two objects
stub and skeleton.
RMI uses stub and skeleton object for communication with the remote object.
A remote object is an object whose method can be invoked from another JVM. Let's
understand the stub and skeleton objects:
stub
The stub is an object, acts as a gateway for the client side. All the outgoing requests are
routed through it. It resides at the client side and represents the remote object. When
the caller invokes method on the stub object, it does the following tasks:
skeleton
The skeleton is an object, acts as a gateway for the server side object. All the incoming
requests are routed through it. When the skeleton receives the incoming request, it does
the following tasks:
skeletons.
At the server side, the packed parameters are unbundled and then the required method is
invoked. This process is known as unmarshalling.
The RMI application have all these features, so it is called the distributed application.
Java RMI Example
The is given the 6 steps to write the RMI program.
RMI Example
In this example, we have followed all the 6 steps to create and run the rmi application.
The client application need only two files, remote interface and client application. In the
rmi application, both client and server interacts with the remote interface. The client
application invokes methods on the proxy object, RMI sends the request to the remote
JVM. The return value is sent back to the proxy object and then to the client application.
1) create the remote interface
For creating the remote interface, extend the Remote interface and declare the
RemoteException with all the methods of the remote interface. Here, we are creating a
remote interface that extends the Remote interface. There is only one method named
add() and it declares RemoteException.
1. import java.rmi.*;
2. public interface Adder extends Remote{
3. public int add(int x,int y)throws RemoteException;
4. }
Now provide the implementation of the remote interface. For providing the
implementation of the Remote interface, we need to
○ Either extend the UnicastRemoteObject class,
○ or use the exportObject() method of the UnicastRemoteObject class
In case, you extend the UnicastRemoteObject class, you must define a constructor that
declares RemoteException.
1. import java.rmi.*;
2. import java.rmi.server.*;
3. public class AdderRemote extends UnicastRemoteObject implements Adder{
4. AdderRemote()throws RemoteException{
5. super();
6. }
7. public int add(int x,int y){return x+y;}
8. }
3) create the stub and skeleton objects using the rmic tool.
Next step is to create stub and skeleton objects using the rmi compiler. The rmic tool
invokes the RMI compiler and creates stub and skeleton objects.
1. rmic AdderRemote
Now start the registry service by using the rmiregistry tool. If you don't specify the port
number, it uses a default port number. In this example, we are using the port number
5000.
1. rmiregistry 5000
java.net.MalformedURLException,
java.rmi.RemoteException;
java.net.MalformedURLException, name.
java.rmi.RemoteException;
java.net.MalformedURLException;
registry.
In this example, we are binding the remote object by the name sonoo.
1. import java.rmi.*;
2. import java.rmi.registry.*;
3. public class MyServer{
4. public static void main(String args[]){
5. try{
6. Adder stub=new AdderRemote();
7. Naming.rebind("rmi://localhost:5000/sonoo",stub);
8. }catch(Exception e){System.out.println(e);}
9. }
10. }
At the client we are getting the stub object by the lookup() method of the Naming class
and invoking the method on this object. In this example, we are running the server and
client applications, in the same machine so we are using localhost. If you want to
access the remote object from another machine, change the localhost to the host name
(or IP address) where the remote object is located.
1. import java.rmi.*;
2. public class MyClient{
3. public static void main(String args[]){
4. try{
5. Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");
6. System.out.println(stub.add(34,4));
7. }catch(Exception e){}
8. }
9. }
To write an RMI Java application, you would have to follow the steps
given below −
Define the remote interface
Develop the implementation class (remote object)
Develop the server program
Develop the client program
Compile the application
Execute the application
II. Objects.
a) When a primitive data type is passed as a parameter to a remote method, the RMI system passes it
by value.
b) RMI will make a copy of a primitive data type and send it to the remote method. If a method returns
a primitive data type, it is also returned to the calling JVM by value.
d) This allows JVMs running on different platforms to communicate with each other reliably.
a) When an object is passed to a remote method, the semantics change from the case of the single JVM.
b) RMI sends the object itself, not its reference, between JVMs.
c) It is the object that is passed by value, not the reference to the object.
d) Similarly, when a remote method returns an object, a copy of the whole object is returned to the calling
program.
e) Because different JVMs do not share heap memory, RMI must send the referenced object and all objects
it references.
f) RMI uses a technology called Object Serialization to transform an object into a linear format that
can then be sent over the network wire.
h) Serialized objects can be de-serialized in the memory of the remote JVM and made ready for use by a
Java program.
Parameters in RMI
a) A client program can obtain a remote reference to a remote object, through the RMI registry program.
b) When a method returns a local reference to an exported remote object, RMI does not return that object.
c) It substitutes another object (remote proxy for that service), in the return stream.
d) When a reference is returned to the server, it is not converted into a local reference.
e) When the object from the server is returned, to the client the proxy object is substituted.
1. JDBC code is dependent upon the Database software being used i.e.
are inserting a record into Employee table but our query is Database
is very costly.
software.
4. In JDBC, Exception handling is mandatory. Here We can see that we
relationship.
6. In JDBC, there occurs a Boilerplate problem i.e. For each and every
project we have to write the below code. That increases the code
To overcome the above problems we use ORM tool i.e. nothing but Hibernate
framework. By using Hibernate we can avoid all the above problems and we
can enjoy some additional set of functionalities.
Light-weight means:
size.
Non-invasive means:
● The classes of Hibernate application development are loosely
need not implement hibernate API interfaces and need not extend
manually we have to create table and declare the data-type for each
and every column. But Hibernate can do DDL operations for you
we have to manually set a primary key for a table. But Hibernate can
cache memory.
● Hibernate is a ORM tool means it support Object relational
Hibernate Architecture
The Hibernate architecture includes many objects such as persistent object, session
factory, transaction factory, connection factory, session, transaction etc.
This is the high level architecture of Hibernate with mapping file and configuration file.
Hibernate framework uses many objects such as session factory, session, transaction
etc. alongwith existing Java API such as JDBC (Java Database Connectivity), JTA (Java
Transaction API) and JNDI (Java Naming Directory Interface).
SessionFactory:
From cfg object it takes the JDBC information and create a JDBC
Connection.
SessionFactory factory=cfg.buildSessionFactory();
Session:
factory.
● It opens the Connection/Session with Database software through
Hibernate Framework.
Session session=factory.buildSession();
Transaction:
Transaction tx=session.beginTransaction();
tx.commit();
Query:
Criterion objects.