RMI এর মাধ্যমে Data Passing এর পদ্ধতি

Parameters Passing in RMI - জাভা আরএমআই (Java RMI) - Java Technologies

333

Java RMI (Remote Method Invocation) ক্লায়েন্ট এবং সার্ভারের মধ্যে ডেটা পাস করার একটি পদ্ধতি প্রদান করে। এটি মূলত Serialization এবং Deserialization ব্যবহার করে ডেটা পাঠায়। RMI-এর মাধ্যমে ডেটা পাস করার প্রক্রিয়াটি অত্যন্ত গুরুত্বপূর্ণ, কারণ এটি ডিস্ট্রিবিউটেড অ্যাপ্লিকেশনের কম্পোনেন্টগুলোর মধ্যে কার্যকর যোগাযোগ নিশ্চিত করে।


RMI Data Passing এর মৌলিক ধারণা

  1. Primitive Data Types পাস করা:
    • যেমন: int, double, boolean, ইত্যাদি সরাসরি রিমোট মেথডের প্যারামিটার হিসেবে পাস করা যায়।
  2. Serializable Object পাস করা:
    • Custom ক্লাসের অবজেক্ট পাস করার জন্য, সেই ক্লাসকে java.io.Serializable ইন্টারফেস ইমপ্লিমেন্ট করতে হবে।
  3. Reference পাস করা:
    • RMI এর মাধ্যমে Remote Object এর Reference পাস করা যায়।

RMI Data Passing এর প্রক্রিয়া

1. Primitive Data Passing

RMI এর মাধ্যমে প্রিমিটিভ টাইপ পাস করা সবচেয়ে সহজ। উদাহরণস্বরূপ:

public interface MyRemoteInterface extends Remote {
    int addNumbers(int a, int b) throws RemoteException;
}

Server Implementation:

public class MyRemoteImpl extends UnicastRemoteObject implements MyRemoteInterface {
    public MyRemoteImpl() throws RemoteException {
        super();
    }

    @Override
    public int addNumbers(int a, int b) throws RemoteException {
        return a + b;
    }
}

Client:

public class RMIClient {
    public static void main(String[] args) {
        try {
            MyRemoteInterface remoteObj = (MyRemoteInterface) Naming.lookup("rmi://localhost:1099/MyService");
            int result = remoteObj.addNumbers(5, 10);
            System.out.println("Result from server: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2. Serializable Object Passing

Serializable Object তৈরি করা

যদি ক্লাসের অবজেক্ট RMI এর মাধ্যমে পাঠাতে হয়, তবে ক্লাসটি Serializable ইন্টারফেস ইমপ্লিমেন্ট করতে হবে।

Example: Data Class

import java.io.Serializable;

public class Student implements Serializable {
    private String name;
    private int rollNumber;

    public Student(String name, int rollNumber) {
        this.name = name;
        this.rollNumber = rollNumber;
    }

    public String getName() {
        return name;
    }

    public int getRollNumber() {
        return rollNumber;
    }
}
Remote Interface
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface StudentService extends Remote {
    String getStudentDetails(Student student) throws RemoteException;
}
Server Implementation
import java.rmi.server.UnicastRemoteObject;

public class StudentServiceImpl extends UnicastRemoteObject implements StudentService {
    public StudentServiceImpl() throws RemoteException {
        super();
    }

    @Override
    public String getStudentDetails(Student student) throws RemoteException {
        return "Student Name: " + student.getName() + ", Roll Number: " + student.getRollNumber();
    }
}
Client Implementation
import java.rmi.Naming;

public class RMIClient {
    public static void main(String[] args) {
        try {
            StudentService service = (StudentService) Naming.lookup("rmi://localhost:1099/StudentService");
            
            Student student = new Student("Alice", 101);
            String details = service.getStudentDetails(student);
            
            System.out.println("Response from Server: " + details);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3. Remote Object Reference Passing

RMI এর মাধ্যমে Remote Object এর Reference পাস করার জন্য একটি Remote Interface এবং তার ইমপ্লিমেন্টেশন প্রয়োজন।

Example: Remote Object Passing
public interface CalculatorService extends Remote {
    Calculator getCalculator() throws RemoteException;
}
public interface Calculator extends Remote {
    int multiply(int a, int b) throws RemoteException;
}

Server Implementation:

public class CalculatorImpl extends UnicastRemoteObject implements Calculator {
    public CalculatorImpl() throws RemoteException {
        super();
    }

    @Override
    public int multiply(int a, int b) throws RemoteException {
        return a * b;
    }
}

public class CalculatorServiceImpl extends UnicastRemoteObject implements CalculatorService {
    public CalculatorServiceImpl() throws RemoteException {
        super();
    }

    @Override
    public Calculator getCalculator() throws RemoteException {
        return new CalculatorImpl();
    }
}

Client Implementation:

public class RMIClient {
    public static void main(String[] args) {
        try {
            CalculatorService service = (CalculatorService) Naming.lookup("rmi://localhost:1099/CalculatorService");
            Calculator calculator = service.getCalculator();
            
            int result = calculator.multiply(4, 5);
            System.out.println("Multiplication Result: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

RMI Data Passing এর সুবিধা ও সীমাবদ্ধতা

সুবিধা

  1. Serialization Support:
    জাভার বিল্ট-ইন Serialization পদ্ধতি ডেটা পাঠানো সহজ করে।
  2. Object-Oriented Communication:
    Remote Object এর মাধ্যমে কার্যকর যোগাযোগ নিশ্চিত হয়।
  3. Flexible Data Handling:
    প্রিমিটিভ, Serializable Object এবং Remote Object Reference পাস করা যায়।

সীমাবদ্ধতা

  1. Serialization Overhead:
    বড় অবজেক্ট পাস করার সময় Serialization ও Deserialization ধীর হতে পারে।
  2. Security Concerns:
    RMI এর মাধ্যমে ডেটা পাঠানোর সময় SSL/TLS ব্যবহার করা না হলে নিরাপত্তা ঝুঁকি থাকতে পারে।
  3. Firewall Configuration:
    ডাইনামিক পোর্ট ব্যবহারের কারণে ফায়ারওয়াল সমস্যা হতে পারে।

Java RMI এর মাধ্যমে প্রিমিটিভ টাইপ, Serializable Object, এবং Remote Object Reference পাস করা যায়। এটি ডিস্ট্রিবিউটেড অ্যাপ্লিকেশনে সহজ ও কার্যকর ডেটা পাসিং নিশ্চিত করে। সঠিক Serialization ও Security ব্যবস্থার মাধ্যমে RMI ডেটা পাসিং আরও নির্ভরযোগ্য এবং কার্যকর হয়।

Content added By
Promotion

Are you sure to start over?

Loading...