有状态会话Bean实验指导_第1页
有状态会话Bean实验指导_第2页
有状态会话Bean实验指导_第3页
有状态会话Bean实验指导_第4页
有状态会话Bean实验指导_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、开发ejb2.x的有状态会话bean步龙新建名为countbean的j2ee1.4工程。l new冈1世 i zards:a$ plug-in project0 product configuration + 3 cvs & ejb 3. 0 l_、hibernate d & jbossaop-g jboss-ide+ descriptorsi* lr ejb componentsj2ee 14 project (_* j2ee projects ce j2ee 1.3 project 3+web components(*web servicesrcancel设置工程的源程序h

2、录为src,冃的冃录output为bin。步骤2:在新建向导中,选择 jbosside i ejb components i session bean 创建会话 bean。g* jboss-ide迢kg descriptors l_ ejb componentsg ejb t*xt client qf entity beanmessage drivengg& j2ee projects wtb components3 web rvicesbean*e三 jboss jbpmjboss seaniwizards:otherp plugrin d«v*lopa«nt3

3、simple< backnext >cancel设置session bean的各种参数,点击finish按钮,即可看到生成的ejb类框架代码。& ke< session beansession beancreate session beansource folder: |countbean/srcbrowsep ackage:countbrcwsena®e:superclass:interfaces:coun tbeani java lan;. objecto jeivax. ejb sessionbeanselect the type of the ejb

4、stateless 庁 statefnlselect the access of the ejb2 remote local ' bothwhich method stubs would you like to create?constructors £ro» superclass 0 inherited abstract methodsv >jbcreate 0 metho电< backfini shcancel其屮各参数解释如f:source folder:源程序所在h录;package:源程序的包名; name: session bean 类的类名

5、;superclass 和 interfaces:取默认值即可;select the type of the ejb:选择 ejb 的类型;select the access of the ejb:选择 ejb 的接口类型,会话 bean 通常是 remote 接口 ;which method stubs would you like to create? : ejb 类要实现的方法,应选择 inherited abstract methods和ejbcreate() method牛命周期方法。步骤4:在package explorer中,利用ejb类的右键菜单添加商务方法。j package

6、 expl. . . x =二ji ©u- b? conntbeanw 0 src-田 count-(jj countb«w. j tvt-|j i countbean. java l _countbe,1 seri new砥 ejba ejbc tjbp仏 ejbbqejh bgn name = mcountdisplay-name-naroe for count” description=description for coui jndi-name=rfejb/countrf tvpe=,statef ul,r mote,ropenopen withopen type

7、hier&rchyf3f4elements sessionbeanq, sets* hiijre system librai £opy* bifc j2ee 1.4 librairi< paste* vtb s«rvi cts 1 0 w n .x deletebuild pathsourcerefactorctrlfcctrl+vdeletealt+shift+s alt+shift+t long serial vers i on ui1iconcext(sessionconterated method stubimp or t.u export.j2eea

8、dd cmr relationshipreferencesadd cmp fieldadd select methodctrl+shi£t+u add hooe methodadd create methoddeclarationsoccxirrerices in file;refreshadd busir|ss method 设置business方法的参数。method name: |courttc nev ejb business methodejb business methodcreate a new ejb business methodselect the view ty

9、pe to useremotelocal ' bothcancelfini sh完整的countbean.java代码如下: package count;import java.rmi.remoteexception; import javax.ejb.ejbexception;import javax.ejb.sessionbean; import javax.ejb.sessioncontext;import javax.ejb.createexception;public class countbean implements sessionbean public int val;

10、public void setsessioncontext(sessioncontext ctx)throws ejbexception,remoteexception / todo auto-generated method stub system.oulprintln(hsetsessioncontext()n);public void ejbremove() throws ejbexception, remoteexception / todo auto-generated method stubsystem.oulprintln(ftejbremove()fl);public void

11、 ejbactivate() throws ejbexception, remoteexception / todo auto-generated method stubsystem .outprin tln( *' ej b a c ti vate()h);ipublic void ejbpassivate() throws ejbexception, remoteexception / todo auto-generated method stubsystem.oulprintln(hejbpassivaten);public void ejbcreate(int val) thr

12、ows createexception / todo auto-generated method stub this>val=val;sy stem.out.println( *' ej bcrea te()h);public int count() / todo auto-generated method stub system.oulprintln(ncount()h);return +val;步骤6 :分别建立对应于remote接口和home接口的java接口文件。remote接口代码如下:package count;import javax.ejb.ejbobject;i

13、mport java>rmi>remoteexception;public interface count extends ejbobjectpublic int count() throws remoteexception;home接口的代码如下:package count;import javax.cjb.ejbhomc;import javax.ejb>createexception;import java.rmi.remoteexception;public interface counthome extends ejbhomepublic count create(

14、int val)throws createexceptionremoteexception;结构如下:: package expl. . .»a f=jbe o应-courttbean白srccoun t+ j coutlt. java+ jj coutltbgan.jwa+ jjj count home, java+ 吕i jre system library juki. 5 + 或i j2ee 1.4 libraries (jboss- + 旦,web services 1.0 libraries步骤7:在工程下建立count子目录,作为没有打包的ejb模块。在count子目录下

15、,再新建 count目录。c hew folderfoldercreate a new folder resource.£nter or select the piirent folder: |countb«in/count-icz countbean -&bin 0 countcountb & srccountfolder name: countadvanced >>finishcancel把bin目录中自动编译生成的class文件导入到count子目录下。步骤9:在count子目录下建立meta-inf子目录,并建立ejb-jar.xml和j

16、boss.xml配置文 件。ejb-jar.xml代码如下:<?xml version=nlon encoding=mutf-8n?><ejb-jar version=tt2.r,xmlns=h xmlns:xsi=n/2001/xmlschema-instancen xsi:schemalocation=9vhttp:/java«sunxom/xinl/ns/j2eehttp:/java<sun<com/xml/ns/j2ee/ejb-jar_2_lxsdn><enterprise-beans><

17、session><ejb-name>count</ejb-name><home>count.counthome</home> <remote>counlcount</remote> <ejb-class>count.countbean</ejb-class> <session-type>stateful</session-type> <transaction-type>container</transaction-type> </sessi

18、on></enterprise-beans></ejb-jar>jboss.xml 代码如 f:<?xml version=n1.0n encoding二"utf8"?><jboss><enterprise-beans><session><ejb ame>count</ejb-name><jndi-name>count</jndi-name></session></enterprise-beans></jboss>

19、步骤 10:在项目 countbean 下建立一个 ant 的 xml 文件 packaging-build.xmlo在packaging-build.xml的右键菜单上执行ant命令,打包生成jar文件。run ass 1ant buildalt+shi £t+x, qdebug as探2ant duild.teampackaging-build.xml 代码如下:<?xml version'1.0" encoding=mutf-8n?><project name二"packaging generator0 default=n_pack

20、aging_generation_n> <target name=h_packaging_generation_h depends=hn65540f7><target name=hn65540n description=ttvveblwar,t><jar destfile="count.jarh><zipfileset dir='*count* 7> </jar> </target></project>压缩完成提示信息如下:lproblemsjavadocdeclaration<t

21、erminated> countbean packaging-build, xml ant build c:javajdkl. 5. 0_09binjbuildfile : f : workspace countbean packacrincy-build, xml n65540:flari building jar: f:workspacecountbeancount.jar incf_cf 已 n皂iun_:build successfultotal time: 2 seconds目录结构如下图:=pckae explorer xerarpackagi ngrbui 1 d xal1

22、1启动jboss服务器,显示界面如下:-ry countboon-(d? srcm ® count-因 count j ava h o count0 count 0-j countbean java 耳 g countbcani olvorzi onlltdo val0 count 0& ejbactivateo ejbcreate (string)0 jbfwi"toq. ejbremove ()龟 setsessioncontext (sessioncontext) -血 counthome j ava-o counthonie0 create (ini)jre

23、 systw library jdk1 5j2ee 1.4 libraries (jboss-ide)yab services 1 0 librari«x (jboxxide)countcounttcount, classcountbesrl class血 countmonft. class-heta-ikfx ejb-j&r. x1x jbozx. xnl13:01:49,703 13:01:49,703 13:01:49,718 13:01:49,796 13:01:50,031 13:01:50,781 13:01:50,984 13:01:52,468 13:01:s

24、3,109 13:01:s3,140 13:01:53,203infoinfoinfoinfoinfoinfoinfoinfoinfo infoinfo57,281info57,359info57,609info59,328info59,578info00,890warn01,812info02,640info02,703info02,750info3,687info44,687info44,703infoproblems javadoc declaration'| £| y i e! 03 - cn ojboss jboss 4.0.x c:javajdkl. 5. 0_0

25、9binjavaw. exe (2007t0-27 12:59:02)securedtopic bound to jndi name: topic/securedtopictestdurabletopic bound to jndi name: topic/testdurabletopictestqueue bound to jndi name: queue/testqueueuilserverilservice jbossmq uil service available at : /0.000:8093dlq bound to jndi name: queue/dlqconnectionfa

26、ctorybindingservice bound connectionhanager 'jbossjca:ionfact) tomcatdeployer deploy, ctxpath=/jmx-console, warurl=. . ./deploy/jmx-consolew&i:/httpllbaseprotocol starting coyote http/11 on http-0000-8080channeisocket jk: ajpl3 listening on /0000:8009jkhain jk running id=0 time=0/203 config=

27、nullserver jboss (hx hicrokernel) 4.0.4.ga (build: cvst&g=jboss 4 0 4 ga d&te=2oo6o5151ooo)>r把打包工成的count.jar文件拷贝到c:jboss404gaserverdefaultdeploy路径下(不同机器,安装路径不同),jboss服务器如呆成功部署该count m件,则显示如下所示信息:problems javadoc declaration 旦 console s3 、画冷cl專!jboss jboss 4.0.x c:javajdkl. 5. 0 09binjavaw

28、. exe (2007-10-29 11:57:45)testqueue bound to jndi name: queue/testqueueuilserverilservice jbossmq uil service available at : /:8093dlq bound to jndi name: queue/dlqconnectionfactorybindingservice bound connectionmanager 1jboss.jca:service=coi tomcatdeployer deploy, ctxpath=/jmx-console, waru

29、r1=./deploy/jmx-console.vai maindeployer undeploy 1 file:/c:/jboss-40 4ga/server/default/deploy/count.jai httpllbaseprotocol starting coyote http/1.1 on http--8080channeisocket jk: ajpl3 listening on /:8009jkmain jk running id=0 time=0/391 config=nullserver jboss (mx microkernel) 4.0.4

30、.ga (build: cvstag=jboss_4_0_4_ga date=20c ejbmodule deploying countproxyfactory bound ejb hoine 1 count1 to jndi 1 count1ejbdeployerl deployed: file:/c:/iboss-4.0.4.ga/server/default/deploy/count.jar步翡12:在新建向导中,利用向导jboss-ide i ejb test client建立ejb客户机程序。next >firn shcancel 13设置ejb客户机的各种参数。 ne<

31、®lwizards:+hibernatezk l二 java & jbossaopf-3 jboss-ide+ g* descrip tors- g* ejb components gbsen g entity b«tng message driven bean g session beanj2ee projects d web components g* w*b s«rvic«sh 二< 3 jboss jbpm彳jboss seam 7客户机代码如2package count;import junit.framework.testcas

32、e;import java.util.hashtable;import javax.rmi.portableremoteobject;import javax.naming.context;import javax.naming .initialcontext;public class testcount extends testcase public testcount() super();protected count.counthome home;protected context getinitialcontext() throws exception hashtable props

33、= new hashtable();props.put(context.initial_context_factory,herfaces.namingcontextfactoryh);props.put(context.url_pkg_prefixes,norg.jboss.naming:erfacesn);props.put(context.provider_url, "jnp:/localhost: 1099");context ctx = new initialcontext(props);return ctx;protec

34、ted count.counthome gethome() throws exception context ctx = this.getinitialcontext();object o = ctxjookup(hcountm);count.counthome intf = (count.counthome) portableremoteobject.narrow(o,count.counthome.class); return intf;protected void setup() throws exception this.home = this.gethome();public voi

35、d testcount()trycount count=new count5;int countval=0;system.out>println(,instantiating beansfor(int i=0;i<5;i+)counti=this.home.create(countval);countval=counti<count();system.outprintln(countval);thread.sleep(500);system.outprintln(hcalling count() on beans ");for(int i=0;i<5;i+)countval=countixount();system.out<println(countval);thread.sleep(500);for(int i=0;i<5;i+)counti>remove();catch(exception e)e.printstacktrace();14:运行客户机程序testcount.java0|run asju 1 junit plug-in testalt+shift+x, fdebug asik 2 jvni t testalt+shi£

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论