SSISO Community

시소당

EJB강좌-2

@CustomerHome.java

//1. home interface --> CM(create)
import javax.ejb.*;
import java.rmi.RemoteException;
public interface CustomerHome extends EJBHome{
 public Customer create() throws RemoteException, CreateException;
}

-------------------------------------------------------------------

@Customer.java

//2. remote interface --> BM선언
import javax.ejb.*;
import java.rmi.RemoteException;
import java.util.ArrayList;

public interface Customer extends EJBObject{
 public void setCustomer(CustomerRec rec) throws RemoteException;
 public ArrayList customerList() throws RemoteException;
}

--------------------------------------------------------------------

@CustomerEJB.java

import javax.ejb.*;
import java.util.ArrayList;
public class CustomerEJB implements SessionBean{
 ArrayList list = new ArrayList();

 public CustomerEJB(){
  System.out.println("CustomerEJB()"); 
 }

 //1. SessionBean interface member overriding
 public void ejbActivate() {
  System.out.println("\t ejbActivate()");
 }
         
 public void ejbPassivate() {
  System.out.println("\t ejbPassivate()");
 }
        
 public void ejbRemove() {
  System.out.println("\t ejbRemove()");
 }
         
 public void setSessionContext(SessionContext ctx) {
  System.out.println("setSessionContext()");
 }

 //2. home create --> ejbCreate
 public void ejbCreate(){
  System.out.println("\t ejbCreate()");
 }

 //3. remote BM overriding
 public void setCustomer(CustomerRec rec){
  list.add(rec);
 }
 
 public ArrayList customerList(){
  return list;
 }
}

------------------------------------------------------------------

@CustomerRec.java

import java.io.Serializable;
public class CustomerRec implements Serializable{
 private String name;
 private int age;
 
 //생성자
 public CustomerRec(){}
 public CustomerRec(String name){
  this.name=name;
 }
 
 public CustomerRec(int age){
  this.age=age;
 }

 public CustomerRec(String name, int age){
  this.name=name;
  this.age=age;
 }
 
 //setXXX() getXXX()
 public void setName(String name){this.name=name;}
 public void setAge(int age){this.age=age;}
 public String getName(){return name;}
 public int getAge(){return age;}
 
 //Object method overriding
 public boolean equals(Object obj){
  if(obj instanceof CustomerRec){
   CustomerRec rec=(CustomerRec)obj;
   if(rec.getName().equals(name) && rec.getAge() == age)
    return true;
  }
  return false;
 } 
 public int hashCode(){
  return name.hashCode() ^ age;
 }
 public String toString(){
  return "name : " + name +" age : " + age;
 }
}

---------------------------------------------------------------------

@CustomerClient.java

import javax.naming.*;
import javax.rmi.*;

public class CustomerClient{
 public static void main(String args[]){
  try{
   Context ctx=new InitialContext(); //JNDI 정보를 가져온다.
   Object obj=ctx.lookup("ejb/Customer");
   CustomerHome home=(CustomerHome)PortableRemoteObject.narrow(obj, CustomerHome.class);
   Customer remote=home.create();
//   remote.setCustomer(new CustomerRec("김홍돈",29));
//   remote.setCustomer(new CustomerRec("문근영",19));
   remote.setCustomer(new CustomerRec("전혜영",10));
   System.out.println(remote.customerList());
  }catch(Exception e){
   e.printStackTrace();
  }
 }
}

978 view

4.0 stars