|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
你通过从书的数量和开发周期及运行速度来证明:net和ruby要比java简单。application
AnOverviewofRMIApplications
RMIapplicationsareoftencomprisedoftwoseparateprograms:aserverandaclient.Atypicalserverapplicationcreatessomeremoteobjects,makesreferencestothemaccessible,andwaitsforclientstoinvokemethodsontheseremoteobjects.Atypicalclientapplicationgetsaremotereferencetooneormoreremoteobjectsintheserverandtheninvokesmethodsonthem.RMIprovidesthemechanismbywhichtheserverandtheclientcommunicateandpassinformationbackandforth.Suchanapplicationissometimesreferredtoasadistributedobjectapplication.
Distributedobjectapplicationsneedto
Locateremoteobjects:Applicationscanuseoneoftwomechanismstoobtainreferencestoremoteobjects.AnapplicationcanregisteritsremoteobjectswithRMIssimplenamingfacility,thermiregistry,ortheapplicationcanpassandreturnremoteobjectreferencesaspartofitsnormaloperation.
Communicatewithremoteobjects:DetailsofcommunicationbetweenremoteobjectsarehandledbyRMI;totheprogrammer,remotecommunicationlookslikeastandardJavamethodinvocation.
Loadclassbytecodesforobjectsthatarepassedaround:BecauseRMIallowsacallertopassobjectstoremoteobjects,RMIprovidesthenecessarymechanismsforloadinganobjectscode,aswellasfortransmittingitsdata.
ThefollowingillustrationdepictsanRMIdistributedapplicationthatusestheregistrytoobtainareferencetoaremoteobject.Theservercallstheregistrytoassociate(orbind)anamewitharemoteobject.Theclientlooksuptheremoteobjectbyitsnameintheserversregistryandtheninvokesamethodonit.TheillustrationalsoshowsthattheRMIsystemusesanexistingWebservertoloadclassbytecodes,fromservertoclientandfromclienttoserver,forobjectswhenneeded.
Thislessoncontainsthefollowingsections:
AdvantagesofDynamicCodeLoading
RemoteInterfaces,Objects,andMethods
CreatingDistributedApplicationsUsingRMI
BuildingaGenericComputeEngine
AdvantagesofDynamicCodeLoading
AdvantagesofDynamicCodeLoading
OneofthecentralanduniquefeaturesofRMIisitsabilitytodownloadthebytecodes(orsimplycode)ofanobjectsclassiftheclassisnotdefinedinthereceiversvirtualmachine.Thetypesandthebehaviorofanobject,previouslyavailableonlyinasinglevirtualmachine,canbetransmittedtoanother,possiblyremote,virtualmachine.RMIpassesobjectsbytheirtruetype,sothebehaviorofthoseobjectsisnotchangedwhentheyaresenttoanothervirtualmachine.Thisallowsnewtypestobeintroducedintoaremotevirtualmachine,thusextendingthebehaviorofanapplicationdynamically.ThecomputeengineexampleinthischapterusesRMIscapabilitytointroducenewbehaviortoadistributedprogram.
RemoteInterfaces,Objects,andMethods
Likeanyotherapplication,adistributedapplicationbuiltusingJavaRMIismadeupofinterfacesandclasses.Theinterfacesdefinemethods,andtheclassesimplementthemethodsdefinedintheinterfacesand,perhaps,defineadditionalmethodsaswell.Inadistributedapplicationsomeoftheimplementationsareassumedtoresideindifferentvirtualmachines.Objectsthathavemethodsthatcanbecalledacrossvirtualmachinesareremoteobjects.
Anobjectbecomesremotebyimplementingaremoteinterface,whichhasthefollowingcharacteristics.
Aremoteinterfaceextendstheinterfacejava.rmi.Remote.
Eachmethodoftheinterfacedeclaresjava.rmi.RemoteExceptioninitsthrowsclause,inadditiontoanyapplication-specificexceptions.
RMItreatsaremoteobjectdifferentlyfromanonremoteobjectwhentheobjectispassedfromonevirtualmachinetoanother.Ratherthanmakingacopyoftheimplementationobjectinthereceivingvirtualmachine,RMIpassesaremotestubforaremoteobject.Thestubactsasthelocalrepresentative,orproxy,fortheremoteobjectandbasicallyis,tothecaller,theremotereference.Thecallerinvokesamethodonthelocalstub,whichisresponsibleforcarryingoutthemethodcallontheremoteobject.
Astubforaremoteobjectimplementsthesamesetofremoteinterfacesthattheremoteobjectimplements.Thisallowsastubtobecasttoanyoftheinterfacesthattheremoteobjectimplements.However,thisalsomeansthatonlythosemethodsdefinedinaremoteinterfaceareavailabletobecalledinthereceivingvirtualmachine.
CreatingDistributedApplicationsUsingRMI
WhenyouuseRMItodevelopadistributedapplication,youfollowthesegeneralsteps.
Designandimplementthecomponentsofyourdistributedapplication.
Compilesourcesandgeneratestubs.
Makeclassesnetworkaccessible.
Starttheapplication.
DesignandImplementtheApplicationComponents
First,decideonyourapplicationarchitectureanddeterminewhichcomponentsarelocalobjectsandwhichonesshouldberemotelyaccessible.Thisstepincludes:
Definingtheremoteinterfaces:Aremoteinterfacespecifiesthemethodsthatcanbeinvokedremotelybyaclient.Clientsprogramtoremoteinterfaces,nottotheimplementationclassesofthoseinterfaces.Partofthedesignofsuchinterfacesisthedeterminationofanylocalobjectsthatwillbeusedasparametersandreturnvaluesforthesemethods;ifanyoftheseinterfacesorclassesdonotyetexist,youneedtodefinethemaswell.
Implementingtheremoteobjects:Remoteobjectsmustimplementoneormoreremoteinterfaces.Theremoteobjectclassmayincludeimplementationsofotherinterfaces(eitherlocalorremote)andothermethods(whichareavailableonlylocally).Ifanylocalclassesaretobeusedasparametersorreturnvaluestoanyofthesemethods,theymustbeimplementedaswell.
Implementingtheclients:Clientsthatuseremoteobjectscanbeimplementedatanytimeaftertheremoteinterfacesaredefined,includingaftertheremoteobjectshavebeendeployed.
CompileSourcesandGenerateStubs
Thisisatwo-stepprocess.Inthefirststepyouusethejavaccompilertocompilethesourcefiles,whichcontaintheimplementationoftheremoteinterfacesandimplementations,theserverclasses,andtheclientclasses.Inthesecondstepyouusethermiccompilertocreatestubsfortheremoteobjects.RMIusesaremoteobjectsstubclassasaproxyinclientssothatclientscancommunicatewithaparticularremoteobject.
MakeClassesNetworkAccessible
Inthisstepyoumakeeverything--theclassfilesassociatedwiththeremoteinterfaces,stubs,andotherclassesthatneedtobedownloadedtoclients--accessibleviaaWebserver.
StarttheApplication
StartingtheapplicationincludesrunningtheRMIremoteobjectregistry,theserver,andtheclient.
Therestofthislessonwalksthroughthestepstocreateacomputeengine.
BuildingaGenericComputeEngine
Thistrailfocusesonasimpleyetpowerfuldistributedapplicationcalledacomputeengine.Thecomputeengine,aremoteobjectintheserver,takestasksfromclients,runsthem,andreturnsanyresults.Thetasksarerunonthemachinewheretheserverisrunning.Thissortofdistributedapplicationcouldallowanumberofclientmachinestomakeuseofaparticularlypowerfulmachineoronethathasspecializedhardware.
Thenovelaspectofthecomputeengineisthatthetasksitrunsdonotneedtobedefinedwhenthecomputeengineiswritten.Newkindsoftaskscanbecreatedatanytimeandthengiventothecomputeenginetoberun.Allthatisrequiredofataskisthatitsclassimplementaparticularinterface.Suchataskcanbesubmittedtothecomputeengineandrun,eveniftheclassthatdefinesthattaskwaswrittenlongafterthecomputeenginewaswrittenandstarted.ThecodeneededtoaccomplishthetaskcanbedownloadedbytheRMIsystemtothecomputeengine,andthentheenginerunsthetask,usingtheresourcesonthemachineonwhichthecomputeengineisrunning.
TheabilitytoperformarbitrarytasksisenabledbythedynamicnatureoftheJavaplatform,whichisextendedtothenetworkbyRMI.RMIdynamicallyloadsthetaskcodeintothecomputeenginesJavavirtualmachineandrunsthetaskwithoutpriorknowledgeoftheclassthatimplementsthetask.Anapplicationlikethis,whichhastheabilitytodownloadcodedynamically,isoftencalledabehavior-basedapplication.Suchapplicationsusuallyrequirefullagent-enabledinfrastructures.WithRMIsuchapplicationsarepartofthebasicmechanismsfordistributedcomputingontheJavaplatform
不得不提一下的是:.net是看到java红,而开发出来的工具。 |
|