개념정리및샘플예제 EJB stateful sample 문서 2016. 01. 14
목차 JEUS EJB Session Bean가이드... 3 1. stateful session bean... 3 1.1 stateful sample 가이드... 3 1.1.1 sample source... 3 1.1.2 결과확인... 6 1.2 http session에서사용하기... 7 1.2.1 sample source... 7 1.2.2 결과확인... 8 2. JEUS Deploy 환경및 tip... 8 2
JEUS EJB Session Bean 가이드 본문서는 sample source 위주의개발가이드이며 EJB 에대한 backgroupd 지식소개및개념정리는생략합니다. 1. stateful session bean 1. 메소드호출과정에서상태정보를유지합니다. 2. Session bean의상태정보는세션종료, 시스템다운, 네트워크장애시에는보존될수없습니다. 3. Client 가 stateful session bean 인스턴스를요청하면, 그 client 는 stateful 인스턴스에할당되고해당 client 의빈상태정보가관리됩니다. 4. 특정메소드가완료된이후에 stateful session bean 인스턴스를제거하려면 @Remove annotation 을사용합니다. 1.1 stateful sample 가이드 stateful 은 request 시에 bean 이존재여부와상관없이무조건 session 을새로생성합니다.(create 할때마다 new sessionid 를생성합니다.) 1.1.1 sample source 비즈니스로직 (CustomerRec.java) package test; import java.io.serializable; public class CustomerRec implements Serializable private String name; private int age; public CustomerRec() public CustomerRec(String name,int age) this.name=name; this.age=age; 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; public boolean equals(object obj) if (obj instanceof CustomerRec) 3
CustomerRec rec=(customerrec)obj; if(name.equals(rec.name)&&age==rec.age) return true; return false; public int hascode() return name.hashcode()^age; public String tostring() return "name:"+name+""+", "+"age:"+age; create 부분 (HelloEJB.java) package test; import java.rmi.remoteexception; import javax.naming.namingexception; import javax.naming.initialcontext; import java.rmi.*; import javax.ejb.*; import java.lang.reflect.*; import java.util.arraylist; public class HelloEJB implements SessionBean private ArrayList arraylist; private String name; private SessionContext sessioncontext; public void ejbcreate(string name) System.out.println(">>>>>>>>>> ejbcreate"); this.name = name; public HelloEJB() arraylist=new ArrayList(); System.out.println(" 생성자 "); public void setsessioncontext(sessioncontext sessioncontext) this.sessioncontext=sessioncontext; System.out.println("setSessionContext()"); public void setcustomer(customerrec rec) arraylist.add(rec); public ArrayList getcustomerlist() return arraylist; 4
public void ejbremove() System.out.println("ejbRemove"); public void ejbactivate() System.out.println("ejvActivate"); public void ejbpassivate() System.out.println("ejbPassivate"); 인터페이스 (Hello.java) package test; import javax.ejb.*; import java.rmi.*; import java.util.arraylist; public interface Hello extends EJBObject public void setcustomer(customerrec rec) throws RemoteException; public ArrayList getcustomerlist() throws RemoteException; 인터페이스 (HelloHome.java) package test; import java.rmi.*; import javax.ejb.*; public interface HelloHome extends EJBHome Hello create(string name) throws RemoteException, CreateException; create 할때마다 session 이바뀌는지를확인하기위한 view(helloejb.jsp) <%@page import="javax.naming.*" %><%@page import="javax.rmi.*" %><%@page import="javax.ejb.*" %> <%@page import="test.hello" %> <%@page import="test.hellohome" %> <%@page import="test.customerrec" %> <%@ page import="java.util.*"%> <% String name = request.getparameter("name"); int age = Integer.parseInt(request.getParameter("age")); try System.out.println("############CREATE NEW EJB BEAN Context ctx = new InitialContext(); Object objref = ctx.lookup("test"); HelloHome home = (HelloHome) PortableRemoteObject.narrow(objref, HelloHome.class); Hello hello = home.create("wonyoung"); System.out.println("############Customer Data Input to Session 5
hello.setcustomer(new CustomerRec(name, age)); System.out.println("############Customer Data get from Session ArrayList arraylist = hello.getcustomerlist(); Iterator rs = arraylist.iterator(); while(rs.hasnext()) String result=rs.next().tostring(); System.out.println(result+"<br>"); out.println(result+"<br>"); //System.out.println(session.getId()); //System.out.println("############EJB Bean input to http-session //session.setattribute("helloejb", hello); catch(exception e) System.out.println("EJB LOG - Exception"); e.printstacktrace(); %> 1.1.2 결과확인 Get 방식으로파라미터전달 ( 호출 ) 새로고츰 (f5) 를이용해서여러번요청을하고, JEUS Log 를확인합니다. -> create 할때마다 sessionid 가변경되는것을확인할수있습니다. [2016.01.14 16:32:56][1][b393] [container1-39] [EJB-4495] [module name : wonyoung#statefulejb, bean name : test, session id : Ja5KHA4YMwZjhlMUetjDEqaOT0ezbD11QtrqiFYXhPEqxM8eOD2m5vOnxPnfFQSM] try to call ejb passivate method of ejb bean ejbpassivate [2016.01.14 16:33:06][1][b393] [container1-47] [EJB-4495] [module name : wonyoung#statefulejb, bean name : test, session id : lxmtab2hreedhuuiehxpamx9faurrl3zqtxstej6fmcvbelgoyxaotoukoywvqfa] try to call ejb passivate method of ejb bean ejbpassivate [2016.01.14 16:33:06][1][b393] [container1-47] [EJB-4495] [module name : wonyoung#statefulejb, bean name : test, session id : viu663fmfd6ye3tje4py1bkhaysnte8fqe9ciwsnr005wcyeorgrwaiu9kfvs1d0] try to call ejb passivate method of ejb bean ejbpassivate 6
1.2 http session 에서사용하기. 위에서처럼 EJB stateful 은무조건 new session 으로 create 되는것은 EJB Spec 에의거한정상작동입니다. 하지만업무 에서는기존재요청시 session 에접근해야하는경우가많이있습니다. 이럴때는 create 된 session bean 을 http session 에담아서사용하면됩니다. 1.2.1 sample source 기존 helloejb.jsp 에서 session 을 setattribete() 하고, 다른 jsp 에서해당 session 을불러오는 sample 입니다. -> 위의 helloejb.jsp 에서다음구문을추가합니다. System.out.println("############EJB Bean input to http-session session.setattribute("helloejb", hello); http session 에담긴 attribute 를 get 하는 sample (getejb.jsp) <%@page import="test.hello" %> <%@page import="test.hellohome" %> <%@page import="test.customerrec" %> <%@ page import="java.util.*"%> <% try System.out.println("############ACCESS already created EJB BEAN Hello hello = (Hello)session.getAttribute("helloejb"); System.out.println("############Customer Data get from Session ArrayList arraylist = hello.getcustomerlist(); Iterator rs = arraylist.iterator(); while(rs.hasnext()) String result=rs.next().tostring(); System.out.println(result+"<br>"); out.println(result+"<br>"); System.out.println(session.getId()); //hello.remove(); // //System.out.println("EJB LOG - result and remove()"); catch(exception e) System.out.println("EJB LOG - Exception"); e.printstacktrace(); %> 7
1.2.2 결과확인 Hellojeb.jsp 를호출하여 setattribute() 하고, getejb.jsp 를호출하여 getattribute() 합니다. -> 이때동일한 value 가호출되는것을알수있습니다. JEUS log 에서 seesionid 를확인합니다. (log buffer 때문에 hellojeb.jsp 에서 1111 로식별하고 getejb.jsp 에서 2222 로식 별하겠습니다.) WYc7CYcHHNwgKtniHrL5SZkbnQrHfluEQs54exQY3tqRN8NiOcFRr1IPYC9WTxtU 2. JEUS Deploy 환경및 tip <application> <name>wonyoung</name> <path>/user/wonyoung/wonyoung</path> <deployment-type>ear</deployment-type> <web-component> 8
<uri>ejbtest</uri> </web-component> <ejb-component> <uri>statefulejb.jar</uri> </ejb-component> <deployment-target> <target> <engine-container-name>infrasvr_container1</engine-container-name> </target> </deployment-target> <classloading>isolated</classloading> <fast-deploy>false</fast-deploy> </application> </jeus-system> stateful 만 sample 을작성하였지만 stateless 은동일소스에서각 jar파일의 META-INF 의 ejb-jar.xml 의 <session><session-type> 만변경해주면됩니다. stateless 는 bean이없을경우새로생성하고존재하면그 bean을그대로이용하기때문에 1.1.2 에서결과테스트를하게되면 arraylist 에 add로 data를쌓게되면계속추가가됩니다. 즉, 새로고침할때마다 list가늘어나서모든세션들이하나의 list에쌓이게됩니다. 9
Copyright 2013 TmaxSoft Co., Ltd. All Rights Reserved. TmaxSoft Co., Ltd. Trademarks Tmax, WebtoB, WebT, JEUS, ProFrame, SysMaster and OpenFrame are registered trademarks of TmaxSoft Co., Ltd. Other products, titles or services may be registered trademarks of their respective companies. Contact Information TmaxSoft can be contacted at the following addresses to arrange for a consulting team to visit your company and discuss your options for legacy modernization. Korea - TmaxSoft Co., Ltd. Corporate Headquarters 272-6 Seohyeon-dong, Bundang-gu, Seongnam-si, South Korea, 463-824 Tel : (+82) 31-8018-1708 Fax : (+82) 31-8018- 1710 Website : http://tmaxsoft.com U.S.A. - TmaxSoft Inc. 560 Sylvan Avenue Englewood Cliffs, NJ 07632, USA Tel : (+1) 201-567-8266 Fax : (+1) 201-567- 7339 Website : http://us.tmaxsoft.com Japan TmaxSoft Japan Co., Ltd. 5F Sanko Bldg, 3-12-16 Mita, Minato-Ku, Tokyo, 108-0073 Japan Tel : (+81) 3-5765-2550 Fax: (+81) 3-5765- 2567 Website : http://jp.tmaxsoft.com China TmaxSoft China Co., Ltd. Room 1101, Building B, Recreo International Center, East Road Wang Jing, Chaoyang District, Beijing, 100102, P.R.C Tel : (+86) 10-5783-9188 Fax: (+86) 10-5783- 9188(#800) Website : http://cn.tmaxsoft.com China(JV) Upright(Beijing) Software Technology Co., Ltd Room 1102, Building B, Recreo International Center, East Road Wang Jing, Chaoyang District, Beijing, 100102, P.R.C Tel : (+86) 10-5783-9188 Fax: (+86) 10-5783- 9188(#800) Website : www.uprightsoft.com TD-JSDB-F0114001 10