উদাহরণ সহ Parameters Passing এবং Return Value Management

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

344

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


RMI-তে Parameters Passing এবং Return Value Management

  1. Parameters Passing:
    • RMI মেথডে পাস করা প্যারামিটার অবশ্যই Serializable বা Primitive Type হতে হবে।
    • প্যারামিটার ক্লায়েন্ট থেকে সার্ভারে Serialize করে পাঠানো হয় এবং সার্ভারে Deserialize করা হয়।
  2. Return Value Management:
    • RMI মেথডের রিটার্ন ভ্যালুও Serializable হতে হবে।
    • রিটার্ন ভ্যালু সার্ভার থেকে ক্লায়েন্টে Serialize করে পাঠানো হয় এবং ক্লায়েন্টে Deserialize করা হয়।

RMI উদাহরণ: Parameters Passing এবং Return Value

Step 1: Remote Interface

import java.rmi.Remote;
import java.rmi.RemoteException;

// Remote Interface
public interface MathService extends Remote {
    // Add two numbers
    int add(int a, int b) throws RemoteException;

    // Find maximum of two numbers
    int max(int a, int b) throws RemoteException;

    // Concatenate two strings
    String concatenate(String str1, String str2) throws RemoteException;
}

Step 2: Remote Object Implementation

import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;

// Remote Object Implementation
public class MathServiceImpl extends UnicastRemoteObject implements MathService {
    protected MathServiceImpl() throws RemoteException {
        super();
    }

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

    @Override
    public int max(int a, int b) throws RemoteException {
        return Math.max(a, b);
    }

    @Override
    public String concatenate(String str1, String str2) throws RemoteException {
        return str1 + str2;
    }
}

Step 3: 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
            MathService mathService = new MathServiceImpl();

            // Bind the remote object to the registry
            Registry registry = LocateRegistry.createRegistry(1099);
            registry.rebind("MathService", mathService);

            System.out.println("Server is running and MathService is ready...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Step 4: 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
            MathService mathService = (MathService) registry.lookup("MathService");

            // Call remote methods and collect results
            int sum = mathService.add(10, 20);
            int maximum = mathService.max(30, 25);
            String concatenated = mathService.concatenate("Hello, ", "World!");

            // Print results
            System.out.println("Addition (10 + 20): " + sum);
            System.out.println("Maximum (30, 25): " + maximum);
            System.out.println("Concatenation: " + concatenated);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

কোডের কাজের ব্যাখ্যা

  1. Parameters Passing:
    • add(int a, int b) মেথডে দুটি সংখ্যা পাস করা হয়।
    • max(int a, int b) মেথডে দুটি সংখ্যা পাস করা হয়।
    • concatenate(String str1, String str2) মেথডে দুটি স্ট্রিং পাস করা হয়।
  2. Return Value Management:
    • add মেথড থেকে একটি সংখ্যা রিটার্ন হয়।
    • max মেথড থেকে সর্বাধিক সংখ্যা রিটার্ন হয়।
    • concatenate মেথড থেকে দুটি স্ট্রিং একত্রিত করে রিটার্ন হয়।
  3. Serialization:
    • RMI নিজেই প্যারামিটার এবং রিটার্ন ভ্যালু Serialize এবং Deserialize করে।

রান করার ধাপ

  1. সার্ভার এবং ক্লায়েন্ট কোড কম্পাইল করুন:

    javac *.java
    
  2. RMI Server চালু করুন:

    java Server
    
  3. RMI Client চালু করুন:

    java Client
    

উদাহরণ আউটপুট

Server is running and MathService is ready...
Addition (10 + 20): 30
Maximum (30, 25): 30
Concatenation: Hello, World!

RMI Parameters Passing এবং Return Value-এর সুবিধা

  1. Distributed Computing:
    • ক্লায়েন্ট এবং সার্ভার একে অপরের সাথে সহজে ডেটা আদান-প্রদান করতে পারে।
  2. Serialization Automatisation:
    • প্যারামিটার এবং রিটার্ন ভ্যালু প্রক্রিয়াকরণ RMI নিজেই হ্যান্ডেল করে।
  3. ডায়নামিক কমিউনিকেশন:
    • ক্লায়েন্ট এবং সার্ভার আলাদা মেশিনে থাকলেও একই ইন্টারফেস ব্যবহার করতে পারে।

Java RMI-তে Parameters Passing এবং Return Value Management সহজ, স্বচ্ছ, এবং টাইপ সেফ। Serializable প্যারামিটার এবং রিটার্ন ভ্যালু ব্যবহার করে ক্লায়েন্ট এবং সার্ভার নির্বিঘ্নে যোগাযোগ করতে পারে। উদাহরণটি ডিস্ট্রিবিউটেড অ্যাপ্লিকেশন তৈরির জন্য একটি শক্তিশালী ভিত্তি দেয়।

Content added By
Promotion

Are you sure to start over?

Loading...