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

Chapter 31. Exercises for Chapter 15

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

31.1Exercise15.1:WebServicesandEJB2.1

Inthisexercise,youwilllearnhowtouseJAX-RPC'sclientand

server-sideprogrammingmodelwithEJB2.1.Youwillexposea

statelesssessionbeanasawebservice.Youwillalso

investigatehowtoconnecttoandinvokeonanexistingweb

servicefromwithinEJBcode.Thestatelesssessionbeanthatis

exposedmodelstheTravelAgentEndpointinChapter15ofthe

EJBbook.Thesupportingcodefortherestofthisexerciseis

borrowedfromtheexercisesforChapter11(Workbook8).This

exercisealsointroducesanotherstatelesssessionbeanthat

actsasaJAX-RPCclienttotheTravelAgentEndpointEJB.



31.1.1InitializeYourEnvironment

Performthefollowingsteps:

1. Openacommandpromptorshellterminaland

changetotheex15_1directorycreatedbythe

extractionprocess.

SettheJAVA_HOMEandJBOSS_HOMEenvironmentvariablesto

pointtowhereyourJDKandJBoss4.0areinstalled.Examples:

Windows:

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

C:\workbook\ex15_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\ex15_1>setPATH=..\ant\bin;%PATH%



Unix:

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



31.1.2CleantheDatabase

Youneedtocleanandrefreshthedatabase.Todothis,first

shutdownJBossifyouhaveitrunningandthenruntheant

clean.db.



31.1.3BuildandDeployExamplePrograms

JBossimplementswebservicesintegrationusingtheApache

Axisprojecthttp://ws.apache.org/axis/.Oneofthemore

annoyingthingsaboutwebservicesandEJBiscreatingaWSDL

documentbasedonaServiceEndpointinterface.Toalleviate

thiswork,AxishasanicetoolcalledJava2WSDLthatallowsyou

toautomaticallygenerateaWSDLdocumentbasedonaplain

Javainterface.Ifyouexaminethebuild.xmlfile,youcanseean

anttargetdevotedtoinvokingthisutility.



31.1.3.1build.xml





























The-lswitchtellsJava2WSDLthedefaultservicelocationURL

thatwillbeusedbyaclientconnection.The-uLITERALswitch

tellsAxistogenerateWSDLwithRPC/Literalmessaging.Noone

takesthedefaultRPC/Encodedmessagingseriouslyanymoreas

itdoesn'tinteroperateverywell.The-oswitchjustspecifies

wheretheWSDLfileshouldbegenerated.Theclassnameof

theServiceEndpointInterface(itcanbeanyJavainterface)

mustbespecifiedasanargumentandmustalsobewithinthe

classpath.



InthisexercisetherearetwoEJBjarfiles.Oneistitan.jar,

whichcontainsTravelAgentEndpointandothersupportingEJBs;

theotheristitan-client.jar,whichcontainstheEJBthatwillbe

connectingtoTravelAgentEndpointasaJAX-RPCclient.Bothof

thesejarsrequirethetravelagent.wsdlfiletodotheirthings.

Todothebuild,performthefollowingsteps:

1. GeneratetheWSDLdocuments:

$antwsdl



Compileanddeploytheejbjars:

$antejbjar



Youwillseetitan.jarandtitan-client.jarbuilt,copiedtothe

JBossdeploydirectory,andredeployedbytheapplication

server.

Sowhere'stheJAX-RPCstubgeneration?ThespiritofJBosshas

alwaysbeentoavoidanyprecompilationstep.Ifyouhaverun

anyoftheotherexamplesinthisbook,youwillhaveseenthat

thereisnotanystubgenerationforEJBseither.Atdeployment

time,JBossautomaticallygeneratesdynamicproxiestohandle

allwebservicecommunicationbothwithclientsandservices.



31.1.4ExaminetheServerModel

Toillustratehowtoexposeastatelesssessionbean,the

TravelAgentEJBfromExercise4.2hasbeenextended.Thisfirst

thingtobedonewastodefineaServiceEndpointinterfacethe



webservicewillimplement.Thisinterfaceisdefinedin

src/main/com/titan/travelagent.



31.1.4.1TravelAgentEndpoint.java

packagecom.titan.webservice;



publicinterfaceTravelAgentEndpointextendsjava.rmi.Remote{

StringmakeReservation(intcruiseId,intcabinId,

intcustomerId,doubleprice)

throwsjava.rmi.RemoteException;

}



ThisinterfaceistakendirectlyfromChapter15oftheEJBbook.

Next,youhavetodefineallthedeploymentdescriptors.These

filesresideinsrc/resources/META-INF.



31.1.4.2ejb-jar.xml





AWebServicereservationservice





TravelAgentEjbEndpoint



com.titan.webservice.TravelAgentEndpoint





com.titan.webservice.TravelAgentBean



Stateless

Container



...



ThisXMListakendirectlyfromChapter15oftheEJBbookand

addedtothedefinitionoftheothersupportingEJBs.



31.1.4.3travelagent_mapping.xml


xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"



xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee



http://www.ibm.com/webservices/xsd/j2ee_jaxrpc_m

version="1.1">



com.titan.webservice



http://webservice.titan.com/TravelAgentEndpoint









Theendpointweareexposingfollowstheguidelinesfora

simplemappingfile.ThenamespaceURIelementisalittle

differentfromChapter15oftheEJBbookbecauseitshould

matchthegeneratedWSDLdocument.



31.1.4.4webservices.xml


xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:titan="http://www.titan.com/TravelAgent"



xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee



http://www.ibm.com/webservices/xsd/j2ee_web_se

version="1.1">







TravelAgentService



META-INF/travelagent.wsdl



META-INF/travelagent_mapping.xml







TravelAgentEndpointtitan:TravelAgentEndpoint



com.titan.webservice.TravelAgentEndpoint







TravelAgentEjbEndpoint











Thisisastandardwebservices.xmldescriptor.ItlinkstheWSDL

file,mappingfile,ServiceEndpointinterface,and

TravelAgentEndpointEJBalltogether.Theimportantpartofthis

fileasitpertainstoJbossisthe.JBossbindsalldeployedwebservicesunderthe

/ws4ee/services/URL.Forthis

example,itwouldbeunder

/ws4ee/services/TravelAgentEndpoint.Youcanalsoviewall

endpointsbygoingtothebaseURL/ws4ee/services(Figure311).



Figure31-1.Listingthedeployedservices



31.1.5ExaminetheClientModel

TravelAgentClientEJBisastatelesssessionbeanthatillustrates

howtoinvokeawebservicefromwithinEJBcode.Itsimply

exposesthesameinterfaceasTravelAgentEndpointEJBand

implementsitbydelegatingtotheTravelAgentEndpoint

interface,invokingoverthewireviaaSOAPinvocation.



31.1.5.1TravelAgentClientBean.java

publicStringmakeReservation(intcruiseId,intcabinId,

intcustomerId,doubleprice)

throwsjava.rmi.RemoteException{

try{



javax.naming.ContextjndiContext=newInitialContext();



Objectobj=



jndiContext.lookup("java:comp/env/service/TravelAgen



javax.xml.rpc.Servicesvc=(javax.xml.rpc.Service)obj;

TravelAgentEndpointendpoint=(TravelAgentEndpoint)



svc.getPort(TravelAgentEndpoint.cl



returnendpoint.makeReservation(cruiseId,cabinId,

customerId,price);



}catch(Exceptione){

e.printStackTrace();

thrownewEJBException("failed");

}

}



SincethespiritofJBossistoavoidstubgeneration,the

preferredmethodforclientsistheDynamicProxyAPIasJBoss

willautomaticallysetupallproxiesatdeploytime.

TravelAgentClientBean.makeReservationisanexampleof



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

Chapter 31. Exercises for Chapter 15

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

×