Java RMI (Remote Method Invocation) প্রযুক্তি ক্লায়েন্ট এবং সার্ভারের মধ্যে ডেটা আদান-প্রদানের সুবিধা প্রদান করে। এই ডেটা হতে পারে Primitive Data Types যেমন int, double, boolean, অথবা Complex Data Types যেমন অবজেক্ট, অ্যারে, বা কাস্টম ক্লাস।
Primitive এবং Complex Data Types Passing এর ধরন
1. Primitive Data Types Passing
- Java RMI-তে Primitive Data Types সরাসরি পাস করা যায়।
- উদাহরণ:
int,float,char,boolean, ইত্যাদি। - Primitive Data Types কল করার সময় সরাসরি তাদের মান নেটওয়ার্কের মাধ্যমে পাঠানো হয়।
2. Complex Data Types Passing
- Complex Data Types এর জন্য Serialization এবং Deserialization প্রক্রিয়া ব্যবহৃত হয়।
- ডেটা টাইপ
Serializableইন্টারফেস ইমপ্লিমেন্ট করতে হবে। - উদাহরণ: অবজেক্ট, অ্যারে, লিস্ট, বা কাস্টম ক্লাস।
Primitive Data Types Passing: উদাহরণ
Remote Interface
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Calculator extends Remote {
int add(int a, int b) throws RemoteException;
double multiply(double x, double y) throws RemoteException;
}
Implementation Class
import java.rmi.server.UnicastRemoteObject;
public class CalculatorImpl extends UnicastRemoteObject implements Calculator {
protected CalculatorImpl() throws RemoteException {
super();
}
@Override
public int add(int a, int b) throws RemoteException {
return a + b;
}
@Override
public double multiply(double x, double y) throws RemoteException {
return x * y;
}
}
RMI Client
import java.rmi.Naming;
public class RMIClient {
public static void main(String[] args) {
try {
Calculator calculator = (Calculator) Naming.lookup("rmi://localhost:1099/CalculatorService");
int sum = calculator.add(5, 10);
double product = calculator.multiply(3.5, 2.0);
System.out.println("Sum: " + sum);
System.out.println("Product: " + product);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Complex Data Types Passing: উদাহরণ
1. Complex Object তৈরি
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{name='" + name + "', age=" + age + "}";
}
}
2. Remote Interface
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface StudentService extends Remote {
String getStudentDetails(Student student) throws RemoteException;
}
3. Implementation Class
import java.rmi.server.UnicastRemoteObject;
public class StudentServiceImpl extends UnicastRemoteObject implements StudentService {
protected StudentServiceImpl() throws RemoteException {
super();
}
@Override
public String getStudentDetails(Student student) throws RemoteException {
return "Received: " + student.toString();
}
}
4. RMI Client
import java.rmi.Naming;
public class RMIClient {
public static void main(String[] args) {
try {
StudentService studentService = (StudentService) Naming.lookup("rmi://localhost:1099/StudentService");
Student student = new Student("John Doe", 20);
String response = studentService.getStudentDetails(student);
System.out.println(response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Complex Data Passing এ গুরুত্বপূর্ণ বিষয়
- Serializable ইন্টারফেস:
- Complex Data Types যেমন কাস্টম ক্লাস অথবা অবজেক্ট
Serializableইন্টারফেস ইমপ্লিমেন্ট করতে হবে। উদাহরণ:
public class Student implements Serializable { private String name; private int age; }
- Complex Data Types যেমন কাস্টম ক্লাস অথবা অবজেক্ট
- Serialization এবং Deserialization:
- RMI স্বয়ংক্রিয়ভাবে অবজেক্ট সিরিয়ালাইজ করে এবং নেটওয়ার্কের মাধ্যমে পাঠায়।
- ডাটা রিসিভ করার পর Deserialization হয়।
- Stub এবং Skeleton:
- ক্লায়েন্ট এবং সার্ভারের মধ্যে ডেটা আদান-প্রদান করার জন্য Stub এবং Skeleton ব্যবহার করা হয়।
Primitive এবং Complex Data Types Passing এর তুলনা
| বৈশিষ্ট্য | Primitive Data Types | Complex Data Types |
|---|---|---|
| Serialization প্রয়োজন | প্রয়োজন নেই | প্রয়োজন |
| ডেটা টাইপ | int, float, boolean, ইত্যাদি। | কাস্টম ক্লাস, অবজেক্ট, লিস্ট, অ্যারে। |
| পারফরম্যান্স | দ্রুত এবং কম জটিল | তুলনামূলকভাবে ধীর (Serialization কারণে)। |
| ব্যবহারিক ক্ষেত্র | সাধারণ গাণিতিক কাজ বা স্ট্যাটিক ডেটা পাস করা। | জটিল অবজেক্ট বা ডেটা স্ট্রাকচার পাস করা। |
Java RMI-তে Primitive Data Types সরাসরি এবং দ্রুত পাস করা যায়, যেখানে Complex Data Types পাস করার জন্য Serialization এবং Deserialization প্রয়োজন। Complex Data Types ব্যবহারে Serializable ইন্টারফেস ব্যবহার করা বাধ্যতামূলক। এই পদ্ধতিগুলি RMI প্রযুক্তিকে অত্যন্ত কার্যকর এবং শক্তিশালী করে তোলে।
Content added By
Read more