Distributed Computing — Chapter 18
Things are easier when all the parts of your application are in one place, however it is not always possible. Starting from having to perform powerful computations on server side, to having to fetch data from a database. In this chapter, we’ll learn how to use Java Remote Method Invocation (RMI) technology, including Servlets, Enterprise Java Beans (EJB) ,and Jini. This includes how the EJB and Jini depends on RMI.
Few important points about distributed computing using Java
- An object on one heap cannot get a normal Java reference to an object on a different heap (running on a different JVM)
- Java Remote Method Invocation (RMI) makes it seem like you’re calling a method on a remote object (i.e. an object in a different JVM), but you aren’t
- When a client calls a method on a remote object, the client is really calling a method on a proxy of the remote object. The proxy is called a ‘stub’.
- A stub is a client helper object that takes care of the low-level networking details (sockets, streams, serialization etc.) by packaging and sending method calls to the server.
- To build a remote service (in other words, an object that a remote client can ultimately call methods on), you must start with a remote Interface.
- A remote interface must extend the java.rmi.Remote interface. and all methods must declare RemoteException.
- Your remote service implements your remote interface.
- Your remote service should extend UnicastRemoteObject. (Technically there are other ways 10 create a remote object, but extending UnicastRemoteObject is the simplest).
- Your remote service class must have a constructor, and the constructor must declare a RemoteException (because the superclass constructor declares one).
- Your remote service must be instantiated, and the object registered with the RMI registry.
- To register a remote service, use the static Naming.rebind(‘Service Name”, serviceInstance);
- The RMI registry must be running on the same machine as the remote service, before you try 10 register a remote object with the RMI registry.
- Almost everything related to RMI can throw a RemoteException (checked by the compiler). This Includes registering or looking up a service in the registry, and all remote method calls from the client to the stub.
- Servlets are Java classes that run entirely on (and/or within) an HTTP(web) server.
- Servlets are useful for running code on the server as a result of client interaction with a web page. For example, if a client submits information in a web page form, the servlet can process the information, add it to a database, and send back a customized, confirmation response page.
- To compile a servlet, you need the servlet packages which are in the servlets.jar file. The servlet classes are not part of the Java standard libraries, so you need to download the servlets.jar from java.sun.com or get them from a servlet capable web server.
- To run a servlet, you must have a web server capable of running servlets, such as the Tomcat server from apache.org.
- Your servlet must be placed in a location that’s specific to your particular web server, so you’ll need to find that out before you try to run your servlets. If you have a web site hosted by an ISP that supports servlets, the ISP will tell you which directory to place your servlets in.
- A typical servlet extends HttpServlet and overrides one or more servlet methods, such as doGet() ordoPos().
- The web server starts the servlet and calls the appropriate method (doGet(), etc.) based on the client’s request.
- The servlet can send back a response by getting aPrintWriter output stream from the response parameter of the doGet() method.
- The servlet can write out to an HTML page to produce the output.