13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

5.8 Introducing RMI143Example 5.12 The RMI client programpackage net.multitool.RMIDemo;import java.rmi.*;public class Client {public static void main(String[] arglist) {if (System.getSecurityManager() == null) {System.setSecurityManager(new RMISecurityManager());}try {String name = "//penfold/Sessi<strong>on</strong>";// Obtain reference to the remote objectSessi<strong>on</strong> sess = (Sessi<strong>on</strong>) Naming.lookup(name);}}System.out.println("Pointless RMI Client. 47 + 13 = " +sess.add(47,13) + ", right?");} catch (Excepti<strong>on</strong> e) {e.printStackTrace();}5.8.2 The rmic ToolIn order for a remote object to make itself available and in order for a client tobe able to call such an object, each method needs a client and server-side stubto proxy the method call. Arguments to the method call are c<strong>on</strong>verted tostreamable data (this process is called marshaling) by the client stub, and thatdata is sent over the network to the server stub, which must c<strong>on</strong>vert that streaminto object instances <strong>on</strong> the server side (this is called unmarshaling). The serversidestub then calls the actual method implementati<strong>on</strong>. When the method returns,any return values and changes to the state of the arguments must bemarshaled by the server stub and sent back to the client stub where they areunmarshaled and stored in the correct locati<strong>on</strong>s <strong>on</strong> the client.This was the traditi<strong>on</strong>ally painful part of writing multitier clients. Whatrmic does is automate the generati<strong>on</strong> of these stubs, so writing a remote methodis <strong>on</strong>ly slightly more difficult than writing any other method.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!