Download Passing Remote Objects: Example

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CITS3213: CONCURRENT PROGRAMMING
Passing by Remote Reference
RMI: Passing Objects, Concurrency, vs CORBA
• The sender marshals an object by exporting it via RMI so that the
object can receive remote calls.
This topic focusses on additional details of RMI.
• How objects are passed to remote objects in remote calls.
• Then the marshaled representation is the remote reference for the
object: i.e., an identifer for the object including the host name.
• How the concurrency features of Java interact with RMI.
• When the receiver receives the remote reference, it creates a stub/proxy
object that forwards to the original object.
• A comparison of Java RMI and CORBA.
• The receiver can now use the proxy object like any other local
object, and method calls are forwarded to the original object.
Passing objects remotely
• How are arguments to remote methods delivered to the remote
object and how are results returned?
• Thus, the behaviour is the same as ordinary local method calls,
and we have location transparency, except for calls taking longer.
• Arguments are marshalled by the client stub into some bytes that
can be sent down the network.
• To allow an object to be passed by remote reference, its class must
implement an extension of the interface java.rmi.Remote.
• When they arrive they are unmarshalled by the server skeleton to
their original form.
(This is the same interface that remote servers implement.)
• The opposite happens for return results: the server skeleton marshals and the client stub unmarshals.
• This is straightforward for basic types like strings and integers.
• But, how do we marshal objects?
• Java RMI actually provides two ways of passing objects remotely:
by remote reference and by local copy.
1
2
Passing by Local Copy
Passing Remote Objects: Example
• The sender marshals an object by serializing its instance variables
into a series of bytes and sending these along with the class id.
• The receiver unmarshals by creating an instance of the indicated
class, using the bytes to initialize the instance variables.
• The receiver can now use the new object, and it will behave like
the original object.
• However, method calls are not forwarded: the receiver has its own
local copy, not the original object.
• Thus, the behaviour is different from local method calls if instance
variables in the copy are modified.
• But, often a local copy is more efficient, and no problem arises if
the object is not modified.
• To allow an object to be passed by local copy, its class must implement an extension of the interface java.io.Serializable.
import java.rmi.*; // for Remote and RemoteException
import java.rmi.server.UnicastRemoteObject;
// Will be passed by local value (in makeBid and tellMeABid below)
public interface Bid extends java.io.Serializable {
int getAmount();
int getBidderID();
}
// Will be passed by reference (in registerBL below)
public interface BidListener extends Remote {
void tellMeABid(Bid newBid) throws RemoteException;
}
// Runs an auction, informing all listeners of all bids.
public interface Auction extends Remote {
void registerBL(BidListener bl) throws RemoteException;
void makeBid(Bid newBid) throws RemoteException;
}
public class AuctionI extends UnicastRemoteObject implements Auction {
public void registerBL(BidListener bl) throws RemoteException
{
// Add bl to a collection bls }
(This interface is also used for writing objects to files, etc.)
public void makeBid(Bid newBid) throws RemoteException {
// for each bl in collection bls ...
bl.tellMeABid(newBid); // Forwarded by proxy bl
}
// newBid is copied
// main() constructs and registers the server object
}
3
4
RMI and Concurrency
Java RMI vs CORBA
• If we stick to passing by remote reference, then RMI systems will
run just like local systems.
• Java RMI and CORBA both support programming with distributed
objects.
• But, what about the concurrency features of Java?
• Both allow a programmer to treat remote objects like local ones
when making method calls, storing objects in data structures, etc.,
by using stub/proxy objects.
• When threads make RMI calls, they essentially “migrate” to another host.
• In reality a new thread is created on the receiver, and the original
thread on the sender waits for the return to arrive.
• Synchronization often works without needing to change the code
from what it would be without RMI.
• What are the advantages and disadvantages of each? Are there
situations where one should be preferred?
• The differences between them mostly stem from a single fundamental difference: Java RMI is specific to Java, while CORBA is
language independent.
• But, locks are no longer reentrant in every case: if a thread migrates and comes back, it is really a new thread and will wait for
a lock it already has!
• Similarly, wait and notify generally works just like without RMI:
the calls are forwarded to the actual object.
• But, synchronized code blocks won’t work, because they’ll actually
synchronize on the stub/proxy object.
• These issues can be difficult to resolve.
5
6
Java RMI vs CORBA: Language Dependence
Java RMI vs CORBA: Summary
• The main advantage of CORBA is that it allows a system to work
with objects written on many different languages.
• This is particularly important for interfacing to existing systems,
which could be written in any language.
• It is also important for considering possible future access to a system from other languages.
• Java RMI has the advantage of being simpler: interfaces can be
defined directly in Java, as can inheritance, exceptions, etc.
• Java RMI also has the advantage of being able to easily support
features like creating local copies of remote objects and distributed
garbage collection that may not fit well with some languages supported by CORBA.
7
Java RMI
Java only
CORBA
Language independent
Can access foreign language objects
Interfaces specified in Java
Interfaces specified externally in IDL
Pass objects by remote reference
or by value (making local copy)
Pass objects by reference
(by value possible but awkward)
Distributed garbage collection
integrated with local collectors
Remote references manually released
Generally simpler to use
More complicated
8