티스토리 툴바



RMI
Remote Method Invocation
server side computing
분산객체
트랜잭션
EJB규약
 
인터페이스파일 클라이언트에 제공할 인터페이스(클라이언트가 호출가능 메소드 선언)
서버파일
클라이언트파일

서버상의 사원정보 검색 RMI 프로그램 작성

4출력 <- 클라이언트 1사원이름-> 서버 2-> emp.dat
                <----3검색된사원정보

실행순서
javac
rmic
레지등록
서버실행
클라이언트실행


emp.dat
이름  전화  email
민씨  011   asd1@asdf.net
김씨  012   asd2@asdf.net
박씨  013   asd3@asdf.net

==================== RMI 소스 ==============================
------------------ RMIServer.java --------------------------
import java.io.*;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.util.*;

public class RMIServer extends UnicastRemoteObject implements Rem{
    
    /** Creates a new instance of RMIServer */
    public RMIServer() throws RemoteException{}
   
    public String getSawon(String saName) throws RemoteException,FileNotFoundException, IOException{
        BufferedReader br = new BufferedReader(new FileReader("emp.dat"));
        String str="";
        while((str=br.readLine())!=null){
            StringTokenizer st = new StringTokenizer(str,",");
            String name = st.nextToken();
            if(name.equals(saName))return str;
        }
        return "없숨";
    }
   
    public static void main(String[] args){
        try{
            RMIServer server = new RMIServer();
           
            Naming.rebind("rmi:///Rem",server);
            System.out.println("RMI server running");
        }catch(Exception e){e.getStackTrace();}
    }
   
}

------------------ emp.dat ----------------

이름,전화,email
민씨,011,asd1@asdf.net
김씨,012,asd2@asdf.net
박씨,013,asd3@asdf.net
------------------ Rem.java ---------------

import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.*;

public interface Rem extends Remote{
    public String getSawon(String saName) throws RemoteException,FileNotFoundException,IOException;   
}

-------------- Client.java -----------------

import java.rmi.*;
import java.net.*;
import java.io.*;

public class Client {
    public Client() {
    }
   
    public static void main(String[] args) {
        // TODO code application logic here
        try{
            Rem remObject =(Rem)Naming.lookup("rmi://58.121.75.103/Rem");
            System.out.println(remObject.getSawon(args[0]));
           
        }catch(Exception e){e.printStackTrace();}       
    }
   
}

------------- 실행법 ------------------

javac Rem.java

javac RMIServer.java

rmic RMIServer

start rmiregistry

java RMIServer

javac Client.java

java Client

 
================= 끝 ======================


------------------ EmpSearch.java ----------
import java.rmi.*;
public interface EmpSearch extends Remote {
    boolean findByName(String name) throws RemoteException;
    String getName() throws RemoteException;
    String getPhone() throws RemoteException;
    String getEmail() throws RemoteException;
}
------------------ EmpSearchImpl.java -------------------------
import java.io.*;
import java.rmi.*;
import java.net.*;
import java.util.*;

public class EmpSearchImpl implements EmpSearch, Serializable {
    String name, phone, email;
    public EmpSearchImpl() throws RemoteException  {
    }
    public boolean findByName(String name)  throws RemoteException {
        BufferedReader br= null;
        String line = null;
        boolean found = false;
        try{
            br=new BufferedReader(new FileReader("emp.dat"));
            while((line=br.readLine())!=null){
                if(line.indexOf(name)!=-1) {
                   found = true;
                   StringTokenizer token = new StringTokenizer(line);
                   this.name = token.nextToken();
                   this.phone = token.nextToken();
                   this.email = token.nextToken();
                }
            }
            return found;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }
    public String getName() throws RemoteException {
        return name;
    }
    public String getPhone() throws RemoteException {
        return phone;
    }
    public String getEmail() throws RemoteException {
        return email;
    }
    public static void main(String[] args){
        try {
            EmpSearchImpl robj = new EmpSearchImpl();
            Naming.rebind("RServer", robj);
            System.out.println("Server running...........");
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("서버등록 실패");
        }
    }
}
---------------- RMIClient.java --------------------
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class RMIClient {
    public RMIClient() {
    }
    public static void main(String[] args){
        try {
            // Look up remote object
            EmpSearch robj = (EmpSearch) Naming.lookup("rmi://localhost/RServer");

            // Invoke method on remote object
            boolean result = robj.findByName("홍길동");
            if(result){
                System.out.println("이름:"+robj.getName());
                System.out.println("전화:"+robj.getPhone());
                System.out.println("메일:"+robj.getEmail());
            }
        } catch (MalformedURLException e) {
        } catch (java.rmi.UnknownHostException e) {
        } catch (NotBoundException e) {
        } catch (RemoteException e) {
        }
    }
}
==================================================

 

- EJB 빈의 종류

비지니스로직 ... 세션빈(상태유지stateful,stateless)

엔티티빈

메시지 드리븐 빈

'정리없는자료 > JAVA(jsp)' 카테고리의 다른 글

[EJB] transaction  (2) 2007/06/22
[EJB] annotation  (0) 2007/06/20
[EJB] RMI 도스용 태스트  (0) 2007/06/20
[jsp] 파일 업로드 시스템  (0) 2007/06/20
[jsp] 게시판 페이징 기술2  (0) 2007/06/20
[EJB] (Enterprise Java Beans)  (0) 2007/06/15

top
TAG , ,

Trackback Address :: http://deuxist.tistory.com/trackback/281 관련글 쓰기

  1. Tracked from J2공간 2007/11/23 10:09 삭제

    Subject: rmi 사용하기

    # 서버측 1. 원격지의 클래스와의 매개체로 Remote를 상속받은 Interface를 정의한다. public interface 원격매개체 extends Remote{&nbsp;&nbsp;&nbsp; 이런저런 메소드;}2. UnicastRemoteObject를 상속받은 클래스를 정의한다. public class 클래스A extends UnicastRemoteObject implements 원격매개체{}원격지와 로컬 모두를 상속받을 ...

Write a comment