Great book written by a typical Geek.
Please follow below instructions if you are trying to run example rmi application provided @sun site in windows.
a) When created your interface,server,client programs please provide server and client policy files as below.
grant{
permission java.net.SocketPermission "localhost:1099", "connect, resolve";
permission java.net.SocketPermission "localhost:1024-", "connect, resolve";
permission java.net.SocketPermission "localhost:1024-", "accept, resolve";
};
b) start rmiregistry
c) Create a jar file which will contain Task and Compute class.please note that my src files are residing in C:\saiworkspace\javaRmi\src folder and clsases in C:\saiworkspace\javaRmi\classes folder.
c) Run Server Application
start java -cp C:\saiworkspace\javaRmi\classes;C:\saiworkspace\javaRmi\compute.jar -Djava.rmi.server.codebase=file:/C:/saiworkspace/javaRmi/compute.jar -Djava.rmi.server.hostname=localhost -Djava.security.policy=server.policy javasun.ComputeEngine
d) Run Client Application
java -cp C:\saiworkspace\javaRmi\classes -Djava.security.policy=client.policy javasun.ComputePi localhost 45
e) Finally you will see output if you are lucky enough as 3.141592653589793238462643383279502884197169399.
Below are the few points i wanted to share from JavaRmi book.
1) The Java Remote Method Invocation (RMI) system allows an object running in one Java virtual machine to invoke methods on an object running in another Java virtual machine. RMI provides for remote communication between programs written in the Java programming language.
2) Distributed object applications need to do the following
a) Locate remote objects
b) Communicate with remote objects
c) Load class definitions for objects that are passed around.
3) Advantages of Dynamic Code Loading
One of the central and unique features of RMI is its ability to download the definition of an object's class if the class is not defined in the receiver's Java virtual machine. All of the types and behavior of an object, previously available only in a single Java virtual machine, can be transmitted to another, possibly remote, Java virtual machine. RMI passes objects by their actual classes, so the behavior of the objects is not changed when they are sent to another Java virtual machine. This capability enables new types and behaviors to be introduced into a remote Java virtual machine, thus dynamically extending the behavior of an application. The compute engine example in this trail uses this capability to introduce new behavior to a distributed program
4) In general, a class that implements a remote interface should at least do the following:
a) Declare the remote interfaces being implemented
b) Define the constructor for each remote object
c) Provide an implementation for each remote method in the remote interfaces
5) An RMI server program needs to create the initial remote objects and export them to the RMI runtime, which makes them available to receive incoming remote invocations. This setup procedure can be either encapsulated in a method of the remote object implementation class itself or included in another class entirely. The setup procedure should do the following:
a) Create and install a security manager
b) Create and export one or more remote objects
c) Register at least one remote object with the RMI registry (or with another naming service, such as a service accessible through the Java Naming and Directory Interface) for bootstrapping purposes
6) Some object types do not meet any of these criteria and thus cannot be passed to or returned from a remote method. Most of these objects, such as threads or file descriptors, encapsulate information that makes sense only within a single address space
7) The rules governing how arguments and return values are passed are as follows:
Remote objects are essentially passed by reference. A remote object reference is a stub, which is a client-side proxy that implements the complete set of remote interfaces that the remote object implements.
Local objects are passed by copy, using object serialization. By default, all fields are copied except fields that are marked static or transient. Default serialization behavior can be overridden on a class-by-class basis. Passing a remote object by reference means that any changes made to the state of the object by remote method invocations are reflected in the original remote object.In the parameters and return values of remote method invocations, objects that are not remote objects are passed by value.Thus, a copy of the object is created in the receiving Java virtual machine. Any changes to the object's state by the receiver are reflected only in the receiver's copy, not in the sender's original instance
8) The java.rmi.registry.Registry remote interface is the API for binding (or registering) and looking up remote objects in the registry
9) A piece of code is said to block if it must wait for a resource to finish its job. For example, using the read( ) method to retrieve data from a file can force the method to halt execution until the target hard drive becomes available.Blocking can sometimes lead to undesirable results. If your code is waiting for a byte that will never come, the program has effectively crashed.
10) Stream navigation methods are methods that enable you to move around in the stream without necessarily reading in data. There are five stream navigation methods:
public int available( ) throws IOException
public long skip(long numberOfBytes) throws IOException
public void mark(int numberOfBytes) throws IOException
public boolean markSupported( ) throws IOException
public void reset( ) throws IOException
Input streams that support marking return true when markSupported( ) is called.
You can use the mark( ) method to mark the current location in the stream. The method's sole parameter, numberOfBytes, is used for expiration—the stream will retire the mark if the reader reads more than numberOfBytes past it.Calling reset() returns the stream to the point where the mark was made
Example:
BufferedInputStream inputStream=new BufferedInputStream(new FileInputStream("d://prasanth//dummy.txt"));
int c=0;
System.out.println(inputStream.markSupported());
inputStream.read();
inputStream.mark(5);
for(int i=0;i<9;i++)
inputStream.read();
inputStream.reset();
while((c=inputStream.read())!=-1) {
System.out.print((char)c);
}
Contents of Dummy.txt: Testing InputStream dummy.txt
Prints:
true
esting InputStream dummy.txt
11) Streams are byte-oriented, whereas readers and writers use characters and strings.
12) The Internet is built out of computers that are connected by wires. Each wire serves as a way to exchange information between the two computers it connects. Information is transferred in small,discrete chunks of data called datagrams.
Or, in the case of wireless networks, things that behave like wires.
Each datagram has a header and a data area. The header describes the datagram: where the datagram originated, what machines have handled the datagram, the type and length of the data being sent, and the intended destination of the the datagram. The data area consists of the actual information that is being sent. In almost all networking protocols, the data area is of limited size.For example, the Internet Protocol (frequently referred to as IP) restricts datagrams to 64 KB.
The Internet Protocol is also an example of what is frequently called a connectionless protocol—each datagram is sent independently, and there is no guarantee that any of the datagrams will actually make it to their destination. In addition, the sender is not notified if a datagram does not make it to the destination. Different datagrams sent to the same destination machine may arrive
out of order and may actually travel along different paths to the destination machine.
13) Distributed applications often require three features that are not provided by a connectionless protocol: programs that send data require confirmation that information has arrived; programs that receive data require the ability to validate (and request retransmission) of a datagram; and finally,programs that receive data require the communication mechanism to preserve the order in which information is sent.To help out, we use the Transmission Control Protocol (TCP). TCP is a communications layer,defined on top of IP, which provides reliable communication.
14) TCP works by extending IP in three ways:
• TCP adds extra header information to IP datagrams. This information allows recipients to tell the order in which datagrams were sent and do some fairly robust error-checking on the data.
• TCP extends IP by providing a way to acknowledge datagram receipt. That is, when data is received, it must be acknowledged. Otherwise, the sender must resend it. This also provides a way for recipients to tell senders that the data was received incorrectly.
• TCP defines buffering strategies. The computer receiving data over the network often has a fixed amount of space (its buffer) to hold data. If the sender sends information too quickly, the recipient may not be able to correctly handle all the information—there might not be enough room in its buffer. The solution to this problem is simple: when using TCP,the sender must wait until the recipient tells the sender how much buffer space is available. Once it does, the sender may transmit only enough information to fill the buffer.It then must wait for the recipient to indicate that more buffer room is available.
15) Among the most severe of those restrictions is this: launch code (the code that binds servers into the registry) has to be running on the same computer as the RMI registry.
16) Why doesn't InputStream implement the Serializable interface?
The answer is that InputStream is an abstract base class whose concrete subclasses often have machine specific state. For example, File-InputStream explicitly refers to a file on a hard drive and probably has a file descriptor as part of its state. Making objects such as FileInputStream serializable makes very little sense,since you can't guarantee that either the file or the file descriptor will be available (or meaningful) when the information is deserialized. Similarly, classes such as Frame or Thread, which encapsulate operating-system resources,are not serializable.
17) Serialization creates deep copies. Deep copies are complete and recursive copies of objects. For example, if object A contains a reference to object B, then a deep copy of A contains a reference to a deep copy of B instead of a reference to B.
18) Since the RMI runtime maintains hashtables of servers and stubs, you actually do need to override equals( ) and hashCode( ) if there is a chance that a server could be compared to a stub.
19) Serialization is a mechanism built into the core Java libraries for writing a graph of objects into a stream of data. This stream of data can then be programmatically manipulated, and a deep copy of the objects can be made by reversing the process. This reversal is often called deserialization.
20) If you can't create a zero-argument constructor in the first nonserializable superclass, you'll have to implement the Externalizable interface instead.
21) The Externalizable Interface
To solve the performance problems associated with making a class Serializable, the serialization mechanism allows you to declare that a class is Externalizable instead. When ObjectOutputStream's writeObject( ) method is called, it performs the following sequence of actions:
1. It tests to see if the object is an instance of Externalizable. If so, it uses
externalization to marshall the object.
2. If the object isn't an instance of Externalizable, it tests to see whether the object is an instance of Serializable. If so, it uses serialization to marshall the object.
3. If neither of these two cases apply, an exception is thrown.
Externalizable is an interface that consists of two methods:
public void readExternal(ObjectInput in);
public void writeExternal(ObjectOutput out);
Externalization stores all the metadata, but writes out only the local instance information.This is true even if the superclass implements Serializable.
22) Namely, in a single-processor machine, how can a program be capable of doing more than one thing at a time? The answer is that either the JVM (via so-called green threads) or the operating system (via native threads) is responsible for making sure that each thread is occasionally active. That is, either the JVM or the OS manages the threads and makes sure that each thread can use a percentage of the processor's time. The process of doing this is often called time-slicing or context-switching;the piece of code that does it is often called the thread-scheduler.Time-slicing is a rather expensive process. The price you pay for doing two things at once is the cost of switching between the associated threads and, occasionally, of copying the local caches to the heap.
23) Synchronization is not part of the method signature. A method can be synchronized in a superclass and not synchronized in a subclass. Similarly, methods declared in an interface cannot be declared synchronized.
24) Declaring a static method as synchronized simply means that it synchronizes on the lock associated with the class object (rather than on the lock associated with a particular instance).This is a very useful way of coordinating behavior between instances of the same class.
25) Every thread has a priority. When there is competition for processing resources,threads with higher priority are generally executed in preference to threads with lower priority. Such preference is not, however, a guarantee that the highest priority thread will always be running, and thread priorities cannot be used to reliably implement mutual exclusion.
26) wait and notify should be placed within synchronized code to ensure that the current code owns the monitor
27) When you create a thread, it gets the same priority as the thread which created it.
28) Xms specifies the starting size of the heap. In other words, it specifies how much memory the JVM consumes when it is started. The amount of memory can be specified in bytes, kilobytes or megabytes of RAM; it must be greater than 1 MB of RAM, and it must be a multiple of 1024 bytes.A trailing M signifies megabytes, and a trailing K signifies kilobytes. Otherwise, the argument is in bytes.Xmx specifies the maximum heap size, rather than the starting heap size, and the maximum heap size has to be greater than the starting heap size.
29) How Classes Are Loaded:
The Java virtual machine loads and validates individual classes through the use of objects known as classloaders. The classloading system works as follows:
a. When the JVM is first launched, a single classloader, often referred to as the bootstrap classloader, is created. This classloader is responsible for loading and creating most classes used within the JVM.
b. An application can programmatically create more classloaders. Every classloader, with the exception of the bootstrap classloader, has a parent classloader. The set of
classloaders forms a tree with the bootstrap classloader at the root.
c. When a class object is needed, the JVM finds the appropriate classloader and asks it for the class object using one of the loadClass( ) methods. The classloader first asks its parent for the class and, if the parent returns null, it then creates the class itself.
30) Because classloaders only check their parents, not their siblings, to see if a class has already been loaded, it's possible to safely load different versions of the same class into a JVM.Because classloaders ask their parents for classes before attempting to load them, the first attempt to load the classes will be from the filesystem (e.g., the bootstrap classloader will first try to load the classes).
About the Author:
William Grosso work in software, usually in software startups as the CTO or the Vice President of Engineering. Currently, William Grosso run engineering at Engage.com, a small startup focused on Social Dating (roughly, the intersection of online dating and social networking).
William Grosso also occasionally consult, usually to evaluate an engineering organization or product, and serve on the advisory boards of several companies (currently Five 9's and Ecofactor).
Before William Grosso joined management, William Grosso was a software architect (Java and open source for the most part). And, before software entirely, William Grosso was a mathematician and did some good, if unpublished, work in hyperbolic group theory.
Hope you enjoy reading this book if not till now.
2 comments:
Thank you,
Bill
Will,
Thanks a lot for your encouragment.
Thanks
Prashant
Post a Comment