RMI নির্ভেজাল উদাহরণ


RMI হচ্ছে সোজা কথায় আলাদা JVM -এর মাঝে অবজেক্ট ফাংশন কল করার জন্য একটা উপায়। এক মেশিনে হয়ত একটা JVM আছে RMI use করে আমরা আলাদা কোনো মেশিনে থাকা JVM-এর কোনো অবজেক্ট-এর ফাংশন কল করতে পারি।
This is a powerful feature!
আমার কাছে খুব বেশি কাজের মনে হয়নি, কারন হিসেবে বলতে পারি আমি এর ব্যবহার নিয়ে খুব বেশি পড়াশোনা করিনি। হয়ত সত্যিই This is a powerful feature! তবে ব্যপারটা খুব-ই interesting and exiting, এবং মজারও বটে।
তো বঝাই জাচ্ছে RMI নিয়ে আমার জ্ঞন খুবিই সামান্য। তাই আমি এর theory নিয়ে এর চেয়ে বেশি বলছিনা। তবে introductory এখানে দেখা যেতে পারেwww.javacoffeebreak.com
আজ আমি RMI -এর implementation নিয়ে লিখব। কেননা google করে আমার পাওয়া সব help ছিল command দিয়ে এটা সেটা করে এগুনো…
আমরা যারা একটু অলস/NetBeans dependent তাদের জন্য usefull কোনো help খুজে পাইনি।
মূল technology আমার দেয়া উপরের link -থেকে দেখে নেয়া যেতে পারে আমি একটা example code নিচে দিয়ে দিলাম।


Server:

 /**
 *
 * @author Farsim,Department of CSE, Shahjalal University of Science and Technology
 */</code>

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

public interface Hello extends Remote {
 String sayHello() throws RemoteException;
 }

<pre>

/**
*
* @author Farsim,Department of CSE, Shahjalal University of Science and Technology
*/
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;

public class Server extends UnicastRemoteObject
implements Hello {

public Server() throws RemoteException {
//super();
}

public String sayHello() {
return “Hello World”;
}

public static void main(String args[]) throws RemoteException {

// Create and install a security manager
LocateRegistry.createRegistry(13330);
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
Server obj = new Server();
// Bind this object instance to the name “HelloServer”
Naming.rebind(“rmi://localhost:13330/HellowServer”, obj);
System.out.println(“HelloServer bound in registry and it’s ready…”);
} catch (Exception e) {
System.out.println(“HelloImpl err: ” + e.getMessage());
e.printStackTrace();
}
}
}

</pre>

Clent:

<pre>

/**
*
* @author Farsim,Department of CSE, Shahjalal University of Science and Technology
*/

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

public interface Hello extends Remote {
String sayHello() throws RemoteException;
}

</pre>

<pre>

/**
*
* @author Farsim,Department of CSE, Shahjalal University of Science and Technology
*/
import java.rmi.Naming;
import java.rmi.RemoteException;

public class Client {

//String message = “blank”;

// “obj” is the identifier that we’ll use to refer
// to the remote object that implements the “Hello”
// interface

public Client() throws RemoteException{

}
public static void main(String args[]) throws RemoteException {
String message;
Hello obj = null;
//LocateRegistry.createRegistry(13330);
try {
obj = (Hello)Naming.lookup(“rmi://localhost:13330/HellowServer”);
message = obj.sayHello();
System.out.println(“”+message);

} catch (Exception e) {
System.out.println(“HelloApplet exception: ” +
e.getMessage());
e.printStackTrace();
}
}
}

</pre>

Server এবং Client আলাদা project-এর default package – রাখা যেতে পারে for trouble shooting।
Server -এ security manager on করা যেতে পারে।

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s