Rmi, JDBC, Corba
Rmi, JDBC, Corba
Definition:
Remote Method Invocation (RMI) is a Java API that allows objects to communicate and invoke
methods on objects located in a different Java Virtual Machine (JVM). RMI provides a
framework for building distributed systems in Java, making it easier for different parts of an
application running on different machines to interact as if they were all part of a single system.
RMI is designed to abstract the complexities of network communication and allow Java objects
to interact with each other as if they were local, providing an easy-to-use and powerful tool for
distributed computing in Java applications.
1. Remote Communication:
o RMI enables Java applications to invoke methods remotely over a network. The
communication happens between a client (which calls the method) and a server
(which implements the method).
o The client and server can be on different machines or in different JVMs.
2. Object Serialization:
o Objects in Java are serialized (converted into a byte stream) to be transmitted over
a network.
o RMI automatically serializes the arguments passed to remote methods, ensuring
that objects are safely transferred between different JVMs.
3. Stub and Skeleton:
o RMI uses stubs and skeletons to facilitate communication between the client and
server. These components are automatically generated from the interface
definition, enabling seamless interaction between remote objects.
4. Location Transparency:
o With RMI, the client does not need to know the physical location of the remote
object. The system automatically handles the communication, abstracting the
network details from the client.
5. Automatic Garbage Collection:
o RMI supports automatic garbage collection of remote objects. Once the object is
no longer in use, RMI can remove the object from the memory, ensuring efficient
resource management.
6. Distributed Objects:
o RMI allows the use of distributed objects, meaning objects can exist across
different machines in a network, making it easier to build scalable distributed
applications.
The client is a Java application that invokes methods on remote objects. It acts as the
consumer of the service provided by the remote object.
The client does not interact directly with the remote object but instead communicates
with the stub, which is a proxy representing the remote object.
Key Characteristics:
The client calls the remote methods using the stub object, which behaves as if the method
was local.
The client is unaware of the physical location of the server or the remote object, enabling
location transparency.
Example:
java
2. Server:
The server is a Java application that provides remote services. It implements the methods
defined in the remote interface, which clients can invoke.
The server must make the remote object available to clients through the RMI Registry.
Key Characteristics:
The server contains the actual logic for the remote methods (e.g., the implementation of
business logic).
The server registers its remote objects with the RMI Registry, making them discoverable
by clients.
Example:
java
3. Stub:
The stub is a client-side proxy that acts as the intermediary between the client and the
server.
When the client makes a method call on the stub, it forwards the call to the actual remote
object on the server through the network.
The stub provides a local interface to the remote object, making remote method
invocations look like local calls to the client.
Key Characteristics:
The stub is automatically generated when the remote object interface is compiled, and it
handles the complexities of network communication.
The stub is responsible for marshaling (serializing) the method arguments and sending
them over the network.
bash
rmic CalculatorImpl
4. Skeleton:
The skeleton is a server-side proxy that receives remote method calls from the client’s
stub and forwards them to the actual remote object for execution.
In earlier versions of RMI, the skeleton was needed to dispatch method calls. However,
in newer versions of Java, the skeleton has been deprecated, and its functionality is
embedded within the server’s RMI infrastructure.
Key Characteristics:
The skeleton's role is to unmarshal (deserialize) the parameters sent by the client, invoke
the method on the real object, and then return the result.
The skeleton is no longer needed in modern implementations since the server
automatically handles the method dispatch and unmarshalling.
Example (Deprecated):
java
5. RMI Registry:
The RMI Registry is a centralized service that allows clients to look up remote objects
by name. It acts as a directory that registers remote objects and provides a means for
clients to discover them.
When the server starts, it registers its remote objects in the registry with a unique name.
The client can then search for these objects by their name.
Key Characteristics:
java
1. Client Request:
o The client makes a method call on a stub, as if invoking a local method.
o The stub marshals the method arguments into a byte stream.
2. Network Communication:
o The marshaled request is sent over the network to the RMI Registry.
o The registry locates the remote object on the server and forwards the request to
the server-side skeleton (if using legacy RMI architecture).
3. Server-side Processing:
o The skeleton (or the server's RMI infrastructure in newer versions) receives the
marshaled data, unmarshals the arguments, and forwards the request to the actual
remote object.
o The remote object processes the request, executes the method, and sends the result
back to the skeleton.
4. Response Handling:
o The skeleton marshals the result, sends it back through the network to the client’s
stub.
o The stub unmarshals the response and returns the result to the client as if the
method was executed locally.
4. Advantages of RMI:
Simplified Distributed Communication: Abstracts the complexity of network
programming, providing an easy way for Java applications to interact.
Language Integration: Fully integrated with the Java language, making it seamless for
Java developers to build distributed applications.
Built-in Security: Supports Java’s security manager and allows encrypted
communications with SSL/TLS.
Automatic Garbage Collection: RMI provides automatic garbage collection for remote
objects, freeing developers from manual memory management tasks.
5. Limitations of RMI:
Java Dependency: RMI is Java-specific and does not work across different languages or
platforms. This can limit its use in heterogeneous systems.
Complexity in Large-Scale Systems: RMI may not scale well in very large distributed
systems, particularly if there are numerous distributed objects or complicated data
structures.
Performance Overhead: Due to the serialization and deserialization of objects, RMI can
introduce performance overhead, especially for large objects.
The first step in creating an RMI application is to define a remote interface. This interface
specifies the methods that are available for remote invocation. The methods in the remote
interface must throw RemoteException to handle any network-related issues that might occur
during the communication.
Code Example:
java
import java.rmi.*;
Explanation:
Remote is a marker interface in RMI that indicates the interface is used for remote
communication.
Each method in the remote interface must declare throws RemoteException because
network issues or other communication errors can occur during method invocation.
Next, you need to implement the remote interface. This implementation will define the actual
business logic behind the remote methods. You will need to extend UnicastRemoteObject,
which provides the functionality to export the object for remote access.
Code Example:
java
import java.rmi.*;
import java.rmi.server.*;
Explanation:
The server is responsible for registering the remote object with the RMI Registry so that clients
can discover and access it. You need to create the server class, which registers the remote object
and makes it available for remote invocation.
Code Example:
java
import java.rmi.*;
import java.rmi.registry.*;
} catch (Exception e) {
e.printStackTrace();
}
}
}
Explanation:
The client accesses the remote service by looking up the remote object in the RMI Registry.
Once the object is found, the client can invoke the remote methods just like local method calls.
Code Example:
java
import java.rmi.*;
Explanation:
1. Client Side:
o The client calls a method on the stub object, which acts as a proxy for the remote
object.
o The stub marshals the method parameters into a byte stream, which is then
transmitted over the network.
2. Network Communication:
o The marshaled data is sent across the network to the RMI registry on the server-
side.
o The registry looks up the location of the actual remote object.
3. Server Side:
o The skeleton (in older versions) or the server infrastructure receives the
marshaled data, unmarshals the arguments, and calls the actual method on the
remote object.
o The method executes, and the result is marshaled back into a byte stream and sent
over the network.
4. Client Receives Response:
o The client’s stub receives the marshaled response, unmarshals the result, and
returns it to the client.
JDBC consists of several key components that allow you to interact with databases. These
components work together to perform operations such as connecting to a database, executing
SQL queries, and processing query results.
1. DriverManager:
o Role: DriverManager manages a list of database drivers. It is responsible for
establishing a database connection by selecting an appropriate driver.
o Details: When a connection request is made, DriverManager selects the first
available driver that can handle the requested database URL.
java
DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb",
"username", "password");
2. Connection:
o Role: The Connection object represents a session between the Java application
and the database.
o Details: It provides methods to create Statement and PreparedStatement
objects and to manage transactions. It is responsible for establishing, managing,
and closing the database connection.
java
3. Statement:
o Role: The Statement object is used to execute SQL queries.
o Details: It can be used to execute static SQL queries (like SELECT, INSERT,
UPDATE, and DELETE) that are compiled once and can be executed multiple times.
There are also PreparedStatement and CallableStatement subclasses for
executing parameterized SQL queries and stored procedures.
java
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
4. ResultSet:
o Role: The ResultSet object stores the data retrieved from the database after
executing a SELECT query.
o Details: It provides methods to retrieve data from the result set, such as
getString(), getInt(), etc. It can be navigated to retrieve each row of data.
java
while (rs.next()) {
System.out.println("Name: " + rs.getString("name"));
}
java
import java.sql.*;
java
Class.forName("com.mysql.cj.jdbc.Driver");
java
o Explanation: The URL specifies the database type (jdbc:mysql), the host
(localhost), the port (3306), and the database name (testdb).
o Once connected, you can use the Connection object to manage interactions with
the database.
4. Step 4: Create Statement
o Statement Object: After establishing the connection, create a Statement object
to execute SQL queries.
java
o Explanation: The Statement object is used to execute SQL queries. For static
queries, the Statement object is appropriate, but for queries with user input, you
should use a PreparedStatement to prevent SQL injection.
5. Step 5: Execute Queries
o Execute SQL Commands: You can execute different types of SQL commands
using the Statement or PreparedStatement objects:
executeQuery(): Used for SELECT queries.
executeUpdate(): Used for INSERT, UPDATE, and DELETE queries.
execute(): Used for any SQL command, such as calling stored
procedures.
java
o Explanation: The ResultSet object stores the result of the SELECT query. You
can iterate through it using the next() method and retrieve the data using
methods like getString(), getInt(), etc.
6. Step 6: Close Connection
o Closing Resources: It is important to close the database connection and other
JDBC resources (Statement, ResultSet) to free up resources after use.
java
conn.close();
stmt.close();
rs.close();
o Explanation: Closing the connection helps to avoid resource leaks, which could
cause performance issues over time.
JDBC Types
There are four types of JDBC drivers, each with its advantages and use cases:
+-------------------------+
| Java Application |
+-------------------------+
|
| (JDBC API)
|
+-------------------------+
| JDBC Driver |
+-------------------------+
|
| (Network Protocol)
|
+-------------------------+
| Database |
+-------------------------+
text
idl
interface Calculator {
int add(in int a, in int b);
int subtract(in int a, in int b);
};
In this IDL snippet, an interface Calculator is defined with two methods: add and
subtract.
Example: A client calls stub.add(5, 3), which gets translated into a request to
the ORB to invoke the actual add method on the server object.
Interaction Between Stub and Skeleton: The client communicates with the stub, while
the server communicates with the skeleton, which in turn invokes the actual methods on
the server object.
idl
interface Calculator {
int add(in int a, in int b);
};
cpp
cpp
+-------------------+ +-------------------+
Client -->| Stub |--->ORB-->| Skeleton |---> Server
+-------------------+ +-------------------+
|
(Remote Method Call)
|
(Server-side Object)
Advantages of CORBA
Limitations of CORBA
Comparative Summary: