Java RMI (Remote Method Invocation) এর মাধ্যমে ক্লায়েন্ট সরাসরি রিমোট সার্ভারে সংরক্ষিত মেথড কল করতে পারে এবং সেখান থেকে রিটার্ন হওয়া ফলাফল সংগ্রহ করতে পারে। নিচে ধাপে ধাপে প্রক্রিয়াটি আলোচনা করা হয়েছে।
Remote Method Call এর ধাপ
- RMI Registry থেকে Remote Object খুঁজে বের করা:
ক্লায়েন্টLocateRegistry.getRegistryএবংlookupমেথডের সাহায্যে রিমোট সার্ভারে অবস্থিত অবজেক্ট অ্যাক্সেস করে। - Remote Method কল করা:
Remote Object এর মেথড সরাসরি ক্লায়েন্ট থেকে কল করা হয়। - ফলাফল সংগ্রহ করা:
Remote Method থেকে প্রাপ্ত রিটার্ন ভ্যালু ক্লায়েন্ট প্রোগ্রামে ব্যবহার করা হয়।
সম্পূর্ণ উদাহরণ: Remote Method Call এবং ফলাফল সংগ্রহ
ধাপ ১: Remote Interface
import java.rmi.Remote;
import java.rmi.RemoteException;
// Remote Interface
public interface Calculator extends Remote {
int add(int a, int b) throws RemoteException;
int subtract(int a, int b) throws RemoteException;
int multiply(int a, int b) throws RemoteException;
double divide(int a, int b) throws RemoteException;
}
ধাপ ২: Remote Object (Implementation Class)
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
// Remote Object Implementation
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 int subtract(int a, int b) throws RemoteException {
return a - b;
}
@Override
public int multiply(int a, int b) throws RemoteException {
return a * b;
}
@Override
public double divide(int a, int b) throws RemoteException {
if (b == 0) {
throw new ArithmeticException("Division by zero is not allowed.");
}
return (double) a / b;
}
}
ধাপ ৩: RMI Server
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
// RMI Server
public class Server {
public static void main(String[] args) {
try {
// Create and export the remote object
Calculator calculator = new CalculatorImpl();
// Bind the remote object to the registry
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("CalculatorService", calculator);
System.out.println("Server is running and CalculatorService is ready...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
ধাপ ৪: RMI Client
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
// RMI Client
public class Client {
public static void main(String[] args) {
try {
// Locate the RMI registry
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
// Lookup the remote object
Calculator calculator = (Calculator) registry.lookup("CalculatorService");
// Call remote methods and collect results
System.out.println("Addition (5 + 3): " + calculator.add(5, 3));
System.out.println("Subtraction (10 - 4): " + calculator.subtract(10, 4));
System.out.println("Multiplication (6 * 7): " + calculator.multiply(6, 7));
System.out.println("Division (20 / 5): " + calculator.divide(20, 5));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Remote Method Call এবং ফলাফল বিশ্লেষণ
Remote Object Registry থেকে প্রাপ্ত:
Registry registry = LocateRegistry.getRegistry("localhost", 1099); Calculator calculator = (Calculator) registry.lookup("CalculatorService");- Remote Method কল এবং রিটার্ন ভ্যালু সংগ্রহ:
উদাহরণ:
int result = calculator.add(5, 3); // Remote Method Call System.out.println("Addition Result: " + result);
- Exception Handling:
RemoteException: নেটওয়ার্ক বা সার্ভার সমস্যা হলে থ্রো হয়।ArithmeticException: মেথডের লজিক অনুযায়ী ভাগফল শূন্য দিয়ে ভাগ করার চেষ্টা করলে থ্রো হয়।
RMI অ্যাপ্লিকেশন রান করার ধাপ
.java ফাইল কম্পাইল করুন:
javac *.javaRMI Server চালু করুন:
java ServerRMI Client চালু করুন:
java Client
ফলাফল উদাহরণ
যদি ক্লায়েন্ট 5 + 3 এর জন্য add মেথড কল করে, সার্ভারের মাধ্যমে ফলাফল হবে 8, এবং ক্লায়েন্টের কনসোলে নিচের আউটপুট দেখা যাবে:
Addition (5 + 3): 8
Subtraction (10 - 4): 6
Multiplication (6 * 7): 42
Division (20 / 5): 4.0
- Remote Method Call: RMI ক্লায়েন্ট সরাসরি রিমোট সার্ভারের মেথড কল করতে পারে।
- ফলাফল সংগ্রহ: রিমোট মেথড থেকে রিটার্ন হওয়া ভ্যালু ক্লায়েন্ট সাইডে ব্যবহার করা যায়।
- Distributed Computing: RMI প্রযুক্তি সহজে ক্লায়েন্ট-সার্ভার যোগাযোগ এবং ডিস্ট্রিবিউটেড সিস্টেম ইমপ্লিমেন্টেশনে সাহায্য করে।
Content added By
Read more