Tải bản đầy đủ - 0 (trang)
Chapter 22. Exercises for Chapter 4

Chapter 22. Exercises for Chapter 4

Tải bản đầy đủ - 0trang

22.1Exercise4.1:ASimpleEntityBean

TheCabinEJBdemonstratesbasicCMP2.0capabilityfora

simpleentitybeanmappedtoasingletable.Thefollowing

sectionsoutlinethestepsnecessarytobuild,deploy,and

executetheCabinEJBexample.Pleasenotethatbecause

you'reusingJBoss'sdefaultembeddeddatabase,youdon't

needtoconfigurethedatabaseorcreatetables.Thecodeyou'll

seeheremirrorstheexamplecodeprovidedinChapter4ofthe

EJBbook.



22.1.1StartUpJBoss

StartupJBossasdescribedintheJBossInstallationand

Configurationchapteratthebeginningofthisworkbook.



22.1.2InitializetheDatabase

Thedatabasetableforthisexercisewillautomaticallybe

createdinJBoss'sdefaultdatabase,HypersonicSQL,whenthe

EJBJARisdeployed.



22.1.3BuildandDeploytheExamplePrograms

Performthefollowingsteps:

1. Openacommandpromptorshellterminaland

changetotheex04_1directorycreatedbythe

extractionprocess.

SettheJAVA_HOMEandJBOSS_HOMEenvironmentvariablesto



pointtowhereyourJDKandJBoss4.0areinstalled.Examples:

Windows:

C:\workbook\ex04_1>setJAVA_HOME=C:\jdk1.4.2

C:\workbook\ex04_1>setJBOSS_HOME=C:\jboss-4.0



Unix:

$exportJAVA_HOME=/usr/local/jdk1.4.2

$exportJBOSS_HOME=/usr/local/jboss-4.0



Addanttoyourexecutionpath.Antisthebuildutility

Windows:

C:\workbook\ex04_1>setPATH=..\ant\bin;%PATH%



Unix:

$exportPATH=../ant/bin:$PATH



Performthebuildbytypingant.Antusesbuild.xmltofigure

outwhattocompileandhowtobuildyourJARs.

IfyouneedtolearnmoreabouttheAntutility,visittheAnt

projectattheJakartawebsiteat

http://jakarta.apache.org/ant/index.html.



AntcompilestheJavasourcecode,buildstheEJBJAR,and

deploystheJARsimplybycopyingittoJBoss'sdeploydirectory.

IfyouarewatchingtheJBossconsolewindow,youwillnotice

thatJBossautomaticallydiscoverstheEJBJARonceithasbeen

copiedintothedeploydirectory,andautomaticallydeploysthe

bean.

AnotherparticularlyinterestingthingaboutbuildingEJBJARsis

thatthereisnospecialEJBcompilationstep.Unlikeother

servers,JBossdoesnotgeneratecodeforclientstubs.Instead,

ithasalightweightmechanismthatcreatesclientproxieswhen

theEJBJARisdeployed,acceleratingthedevelopmentand

deploymentcycle.



22.1.4Deconstructingbuild.xml

Thebuild.xmlfileprovidedforeachworkbookexercisegivesthe

Antutilityinformationabouthowtocompileanddeployyour

JavaprogramsandEJBs.Thefollowingbuildtaskscanbe

executedbytypinganttaskname:

Thedefaulttask(justtypingantwithoutataskname)

compilesthecode,buildstheEJBJAR,anddeploystheJAR

intoJBoss.Thedeploymentprocedureisjustasimplecopy

intotheJBossdeploydirectory.

antcompilecompilesalltheJavasourcefiles.

antcleanremovesall.classand.jarfilesfromtheworking

directoryandundeploystheJARfromJBossbydeletingthe

filefromJBoss'sdeploydirectory.

antclean.dbprovidesyouwithacleancopyofthe

HypersonicSQLdatabaseusedthroughouttheexercises.



ThistaskworksonlywithHypersonicSQL.

run.client_xxxrunsaspecificexampleprogram.Each

exerciseinthisbookwillhavearun.clientruleforeach

exampleprogram.



clean.dbcanbeusedonlywhenJBossisnotrunning.



Here'sabreakdownofwhatiscontainedinbuild.xml.





Thedefaultattributedefinesthedefaulttargetthatantwill

runifyoutypeonlyantonthecommandline.Thebasedir

attributetellsAntwhatdirectorytorunthebuildin:

















AllthepropertiesdefinedabovearevariablesthatAntwilluse



throughoutthebuildprocess.YoucanseethattheJBOSS_HOME

environmentvariableispulledfromthesystemenvironment

andotherdirectorypathsdefined:

















Tocompileandruntheexampleapplicationsinthisworkbook,

addalltheJARSin$JBOSS_HOME/clienttotheJavaclasspath.

Alsonoticethatbuild.xmlinsertsthe${basedir}/jndidirectory

intotheclasspath.Ajndi.propertiesfileinthisdirectoryenables

theexampleprogramstofindandconnecttoJBoss'sJNDI

server:

















ThepreparetargetcreatesthedirectorieswheretheJava

compilerwillplacecompiledclasses:




destdir="${build.classes.dir}"

debug="on"

deprecation="on"

optimize="off"

includes="**">









ThecompiletargetcompilesalltheJavafilesunderthe

src/maindirectory.Noticethatitdependsontheprepare

target;preparewillrunbeforethecompiletargetisexecuted:






















todir="${jboss.home}/server/default/deploy"/>





TheejbjartargetcreatestheEJBJARfileanddeploysitto

JBosssimplybycopyingittoJBoss'sdeploydirectory:



























Therun.client_xxxtargetsareusedtoruntheexample

programsinthischapter:









Theclean.dbtargetcleansthedefaultdatabaseusedbyJBoss

fortheexampleprogramsinthisbook.Remember,youcanonly

useitwhenJBossisnotrunning:














Thecleantargetremovescompiledclassesandundeploysthe

EJBJARfromJBossbydeletingtheJARfileinthedeploy

directory.



22.1.5ExaminetheJBoss-SpecificFiles

YoudonotneedanyJBoss-specificfilestowriteasimpleEJB.

ForanentitybeanassimpleastheCabinEJB,JBosscreatesthe

appropriatedatabasetableswithinitsembeddeddatabase

HypersonicSQLbyexaminingtheejb-jar.xmldeployment

descriptor.



Inlaterchapters,youwilllearnhowtomapentitybeanstodifferent

datasourcesandpre-existingdatabasetablesusingJBoss-specificCMP

deploymentdescriptors.



Bydefault,JBossusesthefromthebean'sejbjar.xmldeploymentdescriptorfortheJNDIbindingofthe

bean'shomeinterface.Ifyoudonotlikethisdefault,youcan

overrideitinajboss.xmlfile.Clientsusethisnametolookup

anEJB'shomeinterface.Forthisexample,CabinEJBisbound

toCabinHomeRemote.



22.1.5.1jboss.xml









CabinEJB

CabinHomeRemote









22.1.6ExamineandRuntheClientApplications

Twoexampleprogramsimplementthesampleclientsprovided

intheEJBbook:



Client_1.java

CreatesasingleCabinbean,populateseachofits

attributes,thenqueriesthecreatedbeanwiththeprimary

key.



Client_2.java

Creates99additionalCabinswithavarietyofdifferentdata

thatwillbeusedinsubsequentexercises.



22.1.6.1Client_1.java



packagecom.titan.clients;



importcom.titan.cabin.CabinHomeRemote;

importcom.titan.cabin.CabinRemote;



importjavax.naming.InitialContext;

importjavax.naming.Context;

importjavax.naming.NamingException;

importjavax.rmi.PortableRemoteObject;

importjava.rmi.RemoteException;



publicclassClient_1

{

publicstaticvoidmain(String[]args)

{

try

{

ContextjndiContext=getInitialContext();

Objectref=jndiContext.lookup("CabinHomeRemote");



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 22. Exercises for Chapter 4

Tải bản đầy đủ ngay(0 tr)

×