1 / 30

Remote Method Invocation

Based on RPC. Remote Procedure Call 1980sRPC call function on another computermarshalling data (package arguments, return values) related to function callRPC Disadvantageslimited data typesfunctions must be described in special Interface Definition Language (IDL). RMI. java object registers

dionysus
Download Presentation

Remote Method Invocation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. Chapter 13 Remote Method Invocation

    2. Based on RPC Remote Procedure Call – 1980s RPC – call function on another computer marshalling data (package arguments, return values) related to function call RPC Disadvantages limited data types functions must be described in special Interface Definition Language (IDL)

    3. RMI java object registers as being remotely accessible client obtains remote reference call syntax same as object in same program (no IDL) RMI does marshalling of data (like RPC) Can transfer objects – like Java object serialization

    4. RMI - Disadvantages Only Java CORBA still preferred for interoperability and speed Web Services becoming more popular for cross-platform/cross-language options

    5. RMI process Define remote interface – what methods can the client invoke? Implement the remote object – by convention has same name as interface, ends with Impl Define client app that uses remote interface Compile and execute

    6. RMI Interface java.rmi.Remote tagging interface (implemented in Remote class which our RMI classes extend) no methods can be accessed with appropriate security permission from any JVM connection to remote object class that implements Remote must be exported to make object accessible (call super in constructor) All method arguments/return values must be serializable or primitives

    7. RMI Interface - cont Remote exception Checked exception Use on remote methods in case of communication problem (not necessarily on all methods in class)

    8. rmic rmic is the Java RMI Compiler that generates stubs used to connect remote objects rmic –v1.2 MyServiceImp creates MyServiceImpl_Stub.class A stub is a proxy for a remote object. It implements only the remote interfaces, no local (i.e. helper) interfaces. If the stub doesn’t match the class (that is, if the remote methods have changed), a marshalling error can occur. rmic is run from the command prompt, in the directory that contains the class file (or the beginning path for the class file, e.g., the directory containing com if the path is com.deitel.advjhtp1.rmi.weather

    9. Weather Service Example

    10. Weather Service Example Service side Details Gets information by html scraping: http://iwin.nws.noaa.gov/iwin/us/traveler.html Interface: extends java.rmi.Remote getWeatherInformation is remote method Implementation extends java.rmi.server.UnicastRemoteObject UnicastRemoteObject exports object on anonymous port for point-to-point communication using standard socket connection Notice call to super() in default constructor, which can throw RemoteException. Alternative would be to call static method exportObject explicitly. Implementation has additional updateWeatherConditions method to contact Traveler’s Forecast web page, create beans

    11. Weather Service Example, cont. main method URL to obtain remote reference is rmi://host:post/remotObjectName, in this case rmi://localhost/WeatherService host is computer running rmiregistry port is port number on which registry is running, default for rmi is 1099 localhost is synonymous with 127.0.0.1 static method rebind of class java.rmi.Naming binds WeatherServiceImpl to rmi registry (and replaces other objects with same name, so preferred to bind)

    12. Weather Service Example - cont WeatherBean Stores the weather forecast for one city Data beans, objects with getters and setters. Often use to interact with databases, web apps, etc. If only getters, then read-only WeatherBean has city name, temperature, description, graphic image static initialization block prior to constructor - ensures image names are available as soon as class loads Properties are like hash table.

    13. Weather Service Example – cont. Weather Service Client just extends JFrame (no remote class) finds remote object using Naming.lookup (must have url of remote service) Access WeatherService, not WeatherServiceImpl refers to stub object on client. Stub passes call to rmi system. MUST match version on server! uses remote object to do method call remote access is in a try-block that catches ConnectionException

    14. Weather Service Example - cont WeatherListModel Adapter design pattern. Enables two objects with incompatible interfaces to communicate with each other. Like electrical plugs. List is not compatible with JList, which requires a ListModel. How did they know what methods to write? See ListModel on Java website. WeatherCellRenderer displays weather information inside a JList, using a WeatherItem WeatherItem formats one line of the weather table

    15. Weather Service Example - cont Compile the classes Produce the stub of WeatherServiceImpl rmic –v1.2 com.deitel.advjhtp1.rmi.weather.WeatherServiceImpl Start the registry from a command prompt rmiregistry NOTE: the registry must be able to find the stub. Either the stub directory must be on your path, or you need to run rmiregistry from the root of your application. Run the WeatherServiceImpl Run the WeatherServiceClient

    16. RMI Chat Application UnicastRemoteObject runs continuously java.rmi.activation.Activatable – activate when client invokes request RMI activation daemon (rmid) enables activatable objects Can’t run activatable on campus, we use modified ChatServer that runs continuously.

    17. Chat Application Server Classes

    18. Chat Application Client Classes

    19. Chat application - ServerImpl registers with rmi registry (in main) ChatServerImpl keeps a HashSet of clients register/unregisterClient maintain HashSet postMessage takes ChatMessage bean as parameter bean includes sender, message (getters/setters) bean MUST be serializable calls deliverMessage on clients (iterates HashSet)

    20. DeitelMessenger – client main sets security manager – to enable download of stub creates RMIMessageManager creates ClientGUI displays GUI (waits for events)

    21. RMI Message Manager uses class java.rmi.Naming provides methods for storing and obtaining references to remote objects in the rmi registry Takes string //host:port/name host defaults to localhost port defaults to 1099 Bind/Rebind associates name with remote object Lookup returns an object reference, can use reference to call methods

    22. RMI Message Manager cont. Registry may be shared by all servers on a host or individual process may create its own registry connect(MessageListener) does a lookup on ChatServer Pass this as argument to registerClient on ChatServer. Enables RMIMessageManager to receive callbacks. sets a MessageListener to handle received messages

    23. RMIMessageManager cont. deliverMessage(ChatMessage) calls messageReceived of MessageListener, passing in sender & message from bean sendMessage(sender, message) create bean, call postMessage of ChatServer serverStopping sets ChatServer to NULL, message

    24. ClientGUI Class that incorporates MessageManager, MessageListener and DisconnectListener WindowAdapter – to disconnect if close window connectAction – calls MessageManager connect, sets DisconnectListener most actions just call corresponding functions, display status, etc.

    25. Dynamic Class Download For clients to download stubs dynamically RMI object specifies java.rmi.server.codebase Classes must be available for download from an http server http server must be running on ChatServer machine

    26. Executing Chat First start the server: rmiregistry This must not be run in directory with ChatServerImpl_Stub.class (or with that class in its classpath).  NOTE: If your home computer cannot find rmiregistry, you may need to supply a complete path.  For example, in CTLM we used to run this command as:         "C:\Program Files\Java\jdk1.5.0_04\bin\rmiregistry.exe" Now you need to start the http server running. We'll do this from inside Eclipse: Set up a Run configuration for examples.classServer.ClassFileServer Program Arguments: 2001 ClassServer\classes VM Arguments: none 2001 is port, ClassServer\classes is where you’ve placed the root for it to locate the .class files. May be able to use . (if set up in same project) Now you need to start the Chat Server.  We'll do this from inside Eclipse: Set up a Run configuration for com.deitel.messenger.rmi.server.ChatServerImpl        Program Arguments: none        VM Arguments: -Djava.rmi.server.codebase=http://localhost:2001/ Then start the client: Set up a run configuration for com.deitel.messenger.rmi.client.DeitelMessenger        Program Arguments: none        VM Arguments: -Djava.security.policy=client.policy A common error here is not having client.policy in the correct location. Another possibility is not specifying the VM arguments correctly. 

    27. Attaching from a different computer run ipconfig on the server to find out the IP address Start server normally rmiregistry must be running on client machine (in source root directory) Add ip address of server as Program argument (not VM argument) for DeitelMessenger. Notice if statement that checks for args, routes to host other than localhost if arg included.

    28. RMI Exercise Modify ChatServer and ChatServerImpl to register an ID String with each client. Messages may be sent to a specific client or to “All” Update MessageManager and RMIMessageManager to operate with the new ChatServerImpl Modify ClientGUI to accept To field Hint: I used a Hashtable instead of a set to contain the clients

    29. Info on ChatServerAdministrator (not used on campus) Activatable objects execute in ActivationGroup – security Starts new virtual machine for each Activation Group create new group with needed security policy register group with RMI activation system can customize commands daemon executes on starting, not done for Chat each group gets an incarnation number

    30. ChatServerAdministrator cont. ActivationDesc – config for Activatable remote object name of class codebase with remote object’s class files marshalledObject with initialization info (null for Chat) Call register – take ActivationDesc, return remote object stub. Bind to RMI registry. Locate Registry – does not actually connect to remote host; obtains reference to bootstrap remote object registry.

    31. Activatable cont. stopServer Use Thread to ensure time to unbind from registry before object terminates. Unbind done in ChatServerAdmin.

More Related