Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


RMI programming, The Lifecycle of server
 
1.RMI ProgrammingÀýÂ÷
2.The Lifecycle of RMI Server ( 2003/02/28 ) 73
Written by specular - ÀüÈ«¼º
1 of 1
 

¡Ø RMI Programming, The Lifecycle of an RMI Server 

 
RMI(Remote Method Invocation)´Â Distributed Object(ºÐ»ê°´Ã¼)¸¦ Áö¿øÇÏ´Â Java Tech.ÀÔ´Ï´Ù. 
Áï ´Ù¸¥ Virtual Machine¿¡ »ý¼ºµÇ¾î ÀÖ´Â ºÐ»ê°´Ã¼ÀÇ method¸¦ È£ÃâÇÒ¼ö ÀÖ´Â ±â´ÉÀ» 
Á¦°øÇØ ÁÖ´Â °ÍÀÌÁÒ. RMI applicationÀº Server, Client, Naming serviceÀÇ ¼¼°¡Áö ºÎºÐÀ¸·Î 
±¸¼ºÀÌ µË´Ï´Ù.
RMI Server´Â ºÐ»ê°´Ã¼¸¦ »ý¼ºÇØ Naming Service¿¡ ºÐ»ê °´Ã¼¸¦ µî·ÏÇÏ°í, ClientÀÇ 
remote method call Request¸¦ ó¸®ÇÏ°í, RMI Client´Â Naming Service·Î ºÎÅÍ ºÐ»ê°´Ã¼ÀÇ
remote reference¸¦ ¾ò¾î remote reference¸¦ ÀÌ¿ëÇؼ­ server¿¡ »ý¼ºµÇ¾î ÀÖ´Â ºÐ»ê°´Ã¼ÀÇ 
method callÀÇ request¸¦ ÇÕ´Ï´Ù. ±×¸®°í, Naming service´Â ºÐ»ê°´Ã¼¿¡ ´ëÇÑ remote referenceÀÇ
Á¤º¸¸¦ serviceÇϴµ¥, JDKÀÇ rmiregistry·Î Á¦°øµË´Ï´Ù. 

¡ß RMI Programming 
1. remote interface °³¹ß : ºÐ»ê °´Ã¼¿¡ ´ëÇÑ client interface¸¦ Á¤ÀÇ

  import java.rmi.Remote;
  //¹Ýµå½Ã Remote interface typeÁ¤ÀÇ 
  public interface Echo extends Remote 
  {
	//¹Ýµå½Ã RemoteExceptionÀ» throwsÇØ¾ß ÇÑ´Ù.
	public String sayEcho(String name) throws RemoteException;
  }
2. Servant °³¹ß : ºÐ»ê°´Ã¼¸¦ Á¤ÀÇ

  import java.rmi.*;
  // remote interface ±¸Çö
  public class EchoImpl implements Echo 
  {
  	public String sayEcho(String name) 
  	{
  		System.out.println("EchoImpl.sayEcho() method");
  		return "Hi... "+name+"\n";
  	}
  }
3. Server Application°³¹ß : Servant»ý¼º, Naming service¿¡ µî·Ï 
import java.io.*;
import java.rmi.*;
import java.rmi.registry.*;

public class Server 
{
public static void main(String args[]) 
{
  try {

	//1.create an object
	EchoServer serv = new EchoServer();

	//2.export the object 
	Echo remoteObj = (Echo)UnicastRemoteObject.exportObject(serv);

	//3.get a stub object of naming service daemon 
	Registry r = LocateRegistry.getRegistry("localhost",1099);      
	//  register the servant to naming service
	r.rebind("ECHO", remoteObj);

	InputStreamReader isr = new InputStreamReader(System.in);
	BufferedReader rdr = new BufferedReader(isr);

	//4.Serve clients
	while (true) {
		System.out.println("Type EXIT to shutdown the server.");
		if ("EXIT".equals(rdr.readLine())) {
			break;
		}
	}

	//5.unregister object
	r.unbind("ECHO");

	//6.unexport object
	UnicastRemoteObject.unexportObject(serv, true);
  } catch (Exception e) {
	e.printStackTrace();
  }  
}
}
Step 1 : remote·Î serviceµÇ¾îÁú Distributed object¸¦ »ý¼º(Servant »ý¼º) 
Step 2 : servant¸¦ exportÇÑ´Ù. RMI system¿¡ Servant object°¡ remote VM¿¡ 
availableÇÏ´Ù¶ó´Â°ÍÀ» informÇÑ´Ù. exportObject()ÀÇ °á°ú´Â ºÐ»ê°´Ã¼ÀÇ Stub°´Ã¼Àε¥,
ÀÌ StubÀº ºÐ»ê°´Ã¼¿¡ ´ëÇÑ remote reference, ºÐ»ê°´Ã¼¿¡ ´ëÇÑ Proxy objectÀÌ´Ù. 
UnicastRemoteObject·Î ºÎÅÍ »ó¼ÓÀ» ¹Þ¾Æ servant¸¦ °³¹ßÇÒ °æ¿ì ÀÌ´Ü°è´Â 
UnicastRemoteObject Contstructor ¿¡¼­ ³»ºÎÀûÀ¸·Î ½ÇÇàµÈ´Ù. 
Step 3 : RMI Naming service daemonÀÎ rmiregistry¿¡ Á¢±ÙÇϱâ À§ÇÑ interface¸¦ ¾ò¾î³½´Ù. 
±×¸®°í, ºÐ»ê°´Ã¼ÀÇ Stub object¸¦ rmiregistry¿¡ µî·ÏÇÑ´Ù. ¹°·Ð ÀÌ ÀÛ¾÷Àº java.rmi.NamingÀ̶ó´Â
class¸¦ ÀÌ¿ëÇصµ µÈ´Ù. 
Step 4 : Server applicationÀÇ Á¾·á¸¦ keyboard·Î ºÎÅÍ "EXIT"¸¦ ÀÔ·ÂÇÒ¶§±îÁö wait½ÃŲ´Ù. 
Step 5 : Server applicationÀÇ Á¾·áÀü rmiregistry¿¡ µî·ÏµÈ servantÀÇ remote referenceÀÎ 
Stub object¸¦ deregisterÇÑ´Ù. 
Step 6 : RMI system¿¡¼­ ºÐ»ê°´Ã¼¸¦ unexportÇÑ´Ù. 
Step 7 : ºÐ»ê°´Ã¼¸¦ Garbage collectionÇÑ´Ù. 

4. Client Applicaton °³¹ß 

import java.rmi.*;

public class Client 
{
public static void main(String args[]) 
{
  try {
	System.out.println("Connecting to echo server...");
	Echo e = (Echo) Naming.lookup("ECHO");
	String result = e.echo("Hello");
	System.out.println("Echo returned " + result);
  } catch (Exception e) {
	e.printStackTrace();
  }
}
}

5. Compile

	prompt>javac *.java


6. Stub, Skeleton class »ý¼º

	prompt>rmic EchoImpl
 
 ¡ß ½ÇÇà 

	1.prompt>rmiregistry
	2.prompt>java Server
	3.prompt>java Client
 
2001.05.12 written by Jeon HongSeong
 
1
References
 
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD