{"id":45,"date":"2018-07-10T09:23:42","date_gmt":"2018-07-10T09:23:42","guid":{"rendered":"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=45"},"modified":"2019-05-15T06:54:58","modified_gmt":"2019-05-15T06:54:58","slug":"remote-method-invocation","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/chapter\/remote-method-invocation\/","title":{"rendered":"Remote Method Invocation"},"content":{"raw":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/HmF2-faAEq8\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<p style=\"text-align: justify;\">What is <strong>R<\/strong><strong>MI<\/strong>?<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong>Remote Method\u00a0 Invocation<\/strong>.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">That \u00a0name \u00a0gives \u00a0an \u00a0impression, \u00a0that \u00a0it \u00a0is \u00a0about invoking method on some object which is on a remote machine. RMI is actually about how \u00a0to create objects on which methods can be invoked from a remote place. How can be achieve this? We would like to have objects on which methods can be invoked from a remote place. for\u00a0 eg.\u00a0 let us\u00a0 consider that we would\u00a0 like to\u00a0 have an instance of Bank\u00a0 on\u00a0 which we would like to be able to invoke methods from a remote place.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-47\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic1-1.png\" alt=\"\" width=\"856\" height=\"404\" \/>\r\n<p style=\"text-align: justify;\">We have a RemoteObject on a machine on which we want to invoke methods from a remote place. Then this can be done by having an instance of Stub Object and Skeleton Object for this RemoteObject. The Skeleton Object will be on the same machine as the RemoteObject and the Stub Object will on the machine of the invoker. The Stub Object and the Skeleton Object would be connected using a socket, and can communicate over the socket. Now for the remote method invocation, the Stub Object has to have all the methods of the RemoteObject available, implementation of these methods will be different. ie. the methods of RemoteObject are available to the invoker on the Stub Object. When the invoker wants to invoke any method on the RemoteObject, he would simply invoke the method on the Stub Object. The implementation of the method on the Stub Object would communicate with the Skeleton over the socket connection and send the method name and the values of the parameters to the skeleton. The Skeleton on receiving the method name and parameters from the stub would now invoke the method locally on the RemoteObject. The method would return some value, The returned value would now be sent back to the Stub by the Skeleton, and the Stub in turn returns it to the invoker. In case the method on RemoteObject throws an exception the Skeleton would catch the exception object and send it to the Stub. The Stub when it receives an exception instead of return value would also throw the exception object received from the skeleton. The process of sending parameters by Stub to skeleton object is known as Marshaling of parameters. The process of receiving parameters by skeleton object is known as UnMarshaling of parameters. The marshaling and unmarshaling of parameters is done on the Socket connection, which is a stream based connection, and therefore they need to be Serializable. If any of the parameters being marshaled is not Serializable then the marshaling process would throw an exception. So initially the Stub marshalls the parameters and the skeleton unmarshalls the parameters, after invoking the method the skeleton in turn marshals the return value and the stub unmarshalls the return value. So to have remote method invocation, we need three objects. The RemoteObject, the Object which has the actual implementation of the methods, whose invocation is desired by the client. The Stub and the Skeleton classes whose objects help the client to achieve the remote invocation. Also it is necessary that the method invoker uses only Serializable objects as parameters and the implementation of methods returns values which are Serializable.<\/p>\r\n\r\n<div style=\"text-align: justify;\">\r\n\r\n&nbsp;\r\n\r\nLet us consider an example for a banking application:\r\n\r\n&nbsp;\r\n\r\nWe might consider to have an entity called Bank.\r\n\r\n&nbsp;\r\n\r\nWhat is a Bank? Bank is any object which has the methods like\r\n\r\n&nbsp;\r\n\r\npublic long openAccount(AccountType type, String name, long openBal)\r\n\r\npublic Account getAccount(long acno)\r\n\r\npublic void deposit(long acno, long amount)\r\n\r\npublic boolean withdraw(long acno, long amount)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The Bank is identified by the methods it has. ie. the functionality supported by the instance. In case of RMI, the Stub Object has all these methods, and so it is the Bank for the invoker. The Bank is identified by the methods and not by its implementation. We would define Bank as an interface, with the required methods, This would be implemented by the RemoteObject and the Stub Object. The RemoteObject and the Stub have common methods, ie. they implement the same interface. Lets look at the steps for defining a remote object.<\/p>\r\n&nbsp;\r\n<ol>\r\n \t<li style=\"text-align: justify;\">Identify the methods for remote invocation and define an interface which has the\u00a0\u00a0 methods. This interface has to extend from the interface Remote in the java.rmi package. All the methods in this interface must have throws RemoteException over and above the application related throws. this throws RemoteException is required since the implementation of the methods by the Stub Object can fail because of some failure in the protocol between the Stub and the Skeleton over the network. ie. the failure of the\u00a0\u00a0 method could be because of the failure in the communication between the Stub and the Skeleton Objects. so in case of bank example we might have and interface for Bank defined<\/li>\r\n<\/ol>\r\n<\/div>\r\n<div style=\"text-align: justify;\">\r\n<p style=\"text-align: justify;\">as below:<\/p>\r\n&nbsp;\r\n\r\npackage bank; import java.rmi.*;\r\n\r\npublic interface Bank extends Remote {\r\n\r\npublic long openAccount(AccountType type, String name, long openBal) throws RemoteException;\r\n\r\npublic Account getAccount(long acno) throws RemoteException; public void deposit(long acno, long amount) throws RemoteException;\r\n\r\npublic boolean withdraw(long acno, long amount) throws RemoteException;\r\n\r\n....\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><strong>2.<\/strong> Write an implementation class for the above interface. This would be the remote object. This\u00a0 \u00a0class\u00a0 \u00a0has\u00a0 \u00a0to\u00a0 \u00a0inherit\u00a0 \u00a0from\u00a0 \u00a0the\u00a0 \u00a0UnicastRemoteObject\u00a0 \u00a0class\u00a0 \u00a0from\u00a0 \u00a0the\u00a0 \u00a0java.rmi.server package. The UnicastRemoteObject class has a default constructor which throws RemoteException, so the constructor in the class must also have throws RemoteException. We inherit from the UnicastRemoteException class, to inherit the implementations of the methods of \u00a0the \u00a0Object \u00a0class, \u00a0which \u00a0would appropriate \u00a0for \u00a0the RemoteObject.<\/p>\r\n&nbsp;\r\n\r\nSo,for the above example we could have a class as below:\r\n\r\n&nbsp;\r\n\r\npackage bank;\r\n\r\n&nbsp;\r\n\r\nimport java.rmi.*;\r\n\r\nimport java.rmi.server.*;\r\n\r\n&nbsp;\r\n\r\npublic class BankImpl extends UnicastRemoteObject implements Bank { private String name;\r\n\r\nprivate Map&lt;Long, Account&gt; accountMap = new HashMap&lt;&gt;();\r\n\r\n....\r\n\r\npublic BankImpl(String name) throws RemoteException {\r\n\r\n....\r\n\r\n}\r\n\r\npublic long openAccount(AccountType type, String name, long openBal) throws RemoteException;\r\n\r\npublic Account getAccount(long acno) throws RemoteException; public void deposit(long acno, long amount) throws RemoteException;\r\n\r\npublic boolean withdraw(long acno, long amount) throws RemoteException;\r\n\r\n....\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><strong>3.\u00a0<\/strong>Now generate the Stub and Skeleton classes for the RemoteObject class defined in step 2. this is done by using the rmic command available with the jdk. In step 1 and 2 when we compile, we would get two class files as \u00a0Bank.class \u00a0and \u00a0BankImpl.class. \u00a0in \u00a0the \u00a0rmic \u00a0utility use the BankImpl class so command will be<\/p>\r\n&nbsp;\r\n\r\nrmic bank.BankImpl\r\n\r\n<\/div>\r\n<p style=\"text-align: justify;\">This would generate two class files in the bank package as BankImpl_Stub.class and BankImpl_Skel.class.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">So, as far as creating the Bank as a RemoteObject is concerned that is done.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Now we need to use this Remote type in an Application. In this case the application on the server would create an instance of bank.BankImpl and make it available for Remote Method Invocation. The client would have to obtain the stub for this object, for the client to initially obtain the stub object it would require some service where it can connect on the server machine. There is a Naming service available along with the jre for RMI. This is known rmiregistry.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-48\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic2.png\" alt=\"\" width=\"817\" height=\"404\" \/>\r\n<p style=\"text-align: justify;\">At runtime the rmiregistry would have to be started first, then the application on \u00a0the \u00a0server \u00a0side would create the instance of RemoteObject and bind \u00a0it \u00a0with \u00a0the \u00a0Naming \u00a0service, \u00a0ie. \u00a0make \u00a0it available for remote invocation. rmiregistry needs to be started on the same machine as the RemoteObject The client would then have to connect to the Naming service and obtain the stub corresponding to the RemoteObject which has been bound by the \u00a0application \u00a0on \u00a0the \u00a0server \u00a0side. Now interaction wtth the rmiregistry(Naming service) would be required by the server application as well as the client. There is a class called Naming, in the java.rmi package, which has static methods which will allows interaction with the \u00a0Naming \u00a0service. \u00a0The \u00a0following \u00a0are \u00a0the \u00a0methods \u00a0in the Naming class: (all methods in this class are static)<\/p>\r\n&nbsp;\r\n\r\nstatic void bind(String key, Remote remoteObject)\r\n\r\nstatic void rebind(String key, Remote remoteObject)\r\n\r\nstatic void unbind(String key)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The above methods are\u00a0 called by the\u00a0 server applicaton only. ie. they\u00a0 cannot be invoked\u00a0 from remote place.<\/p>\r\n&nbsp;\r\n\r\nstatic Remote\u00a0 lookup(String url)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">This \u00a0method \u00a0is \u00a0invoked \u00a0by \u00a0the \u00a0client \u00a0for \u00a0obtaining \u00a0the \u00a0stub \u00a0object \u00a0for \u00a0the \u00a0given \u00a0remote \u00a0object which has been bound with the rmiregistry. The format for the url will be:<\/p>\r\n&nbsp;\r\n\r\nrmi:\/\/&lt;host&gt;[:&lt;port&gt;]\/&lt;key&gt;\r\n<ol>\r\n \t<li style=\"text-align: justify;\">assuming we \u00a0have \u00a0the \u00a0classes \u00a0bank.Bank \u00a0interface, \u00a0bank.BankImpl \u00a0and \u00a0bank.BankImpl_Stub and bank.BankImpl_Stel. then on the server the application might have code like:<\/li>\r\n<\/ol>\r\npublic static void main(String[] args) { bank.BankImpl bi = new bank.BankImpl(....); Naming.rebind(\"SBI\", bi);\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nThe client application would then look something like:\r\n\r\n&nbsp;\r\n\r\npublic static void main(STring[] args) {\r\n\r\nBank bank = (Bank)Naming.lookup(\"rmi:\/\/127.0.0.1\/SBI\");\r\n\r\n.....\r\n\r\nbank....... \/\/ invoking some method on bank;\r\n\r\n}\r\n<div>\r\n\r\nThere is a method called list in the Naming class, with one parameter of type String.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">This could be invoked by the server side or by the client. to konw the keys which have been bound in the Naming service.<\/p>\r\n&nbsp;\r\n\r\nThe method is:\r\n\r\n&nbsp;\r\n\r\nstatic String[] list(String s);\r\n\r\n&nbsp;\r\n\r\nOn the server side it would be invoked as: String[] names = Naming.list(\"*\");\r\n\r\non the client side it would be invoked as:\r\n\r\n&nbsp;\r\n\r\nString[] urls\u00a0 \u00a0= Naming.list(\"rmi:\/\/127.0.0.1\/*\"); now look at the following code on the client side.\r\n\r\npublic static void main(String[] args) {\r\n\r\n<\/div>\r\nBank bank = (Bank)Naming.lookup(\"rmi:\/\/127.0.0.1\/SBI\"); Account ac = bank.openCurrentAccount(\"name1\", 10000); int acno = ac.getAccountNumber();\r\n\r\nbank.deposit(acno, 10000); ac = bank.getAccount(acno); ac.withdraw(acno, 10000); bank.passbook(acno);\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Here we have created an account then deposited some amount into the account now we use the object of the account created by using the getAccount method. and then we withdraw from this account object. is this withdrawal reflected in the account managed in the bank? when we use getAccount method the account instance returned is a copy of the account on the server, and so the withdrawal is not reflected in the passbook method invocation above. ie. in case of rmi here we find pass by value, even for reference types. In rmi pass by reference is still possible. suppose when we call getAccount method, then instead of a copy of the account object, if we could get the instance of a Stub for the account object on the server. then it would be a pass by reference. ie. if the Account was a Remote type then it would be treated as pass by reference automatically. ie. if we have done the same exercise on Account as what is done for Bank. then Account would be passed by reference. ie. we should have Account interface AccountImpl class as an implementation of the Account interface and then we should have also generated the Stub and the Skeleton classes for the Account.<\/p>\r\n\r\n<pre><img class=\"alignnone size-full wp-image-49\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic3.png\" alt=\"\" width=\"915\" height=\"404\" \/><\/pre>\r\n<p style=\"text-align: justify;\">So for rmi, primitive types are always pass by value, reference types are also pass by value, except for the Remote types. Remote types are pass by reference, Remote types are the ones for which the Implementaion class, the Stub class and the Skeleton classes are generated.<\/p>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Remote Method Invocation<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/HmF2-faAEq8\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<div class=\"textLayer\">\r\n<div><strong>Suggested Reading:<\/strong><\/div>\r\n<div><\/div>\r\n<div>1. Core Java Volume 2 by Cay Horstmann &amp; Gary Cornell, Ninth Edition, Pearson Education.<\/div>\r\n<div>2. Beginning Java Networking by Alexander V Konstantinou and others, Wrox publication.<\/div>\r\n<div>3. Java RMI By William Grosso O'Rielly media<\/div>\r\n<div>4. https:\/\/docs.oracle.com\/javase\/tutorial\/rmi\/index.html<\/div>\r\n<\/div>","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/HmF2-faAEq8\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<p style=\"text-align: justify;\">What is <strong>R<\/strong><strong>MI<\/strong>?<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong>Remote Method\u00a0 Invocation<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">That \u00a0name \u00a0gives \u00a0an \u00a0impression, \u00a0that \u00a0it \u00a0is \u00a0about invoking method on some object which is on a remote machine. RMI is actually about how \u00a0to create objects on which methods can be invoked from a remote place. How can be achieve this? We would like to have objects on which methods can be invoked from a remote place. for\u00a0 eg.\u00a0 let us\u00a0 consider that we would\u00a0 like to\u00a0 have an instance of Bank\u00a0 on\u00a0 which we would like to be able to invoke methods from a remote place.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-47\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic1-1.png\" alt=\"\" width=\"856\" height=\"404\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic1-1.png 856w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic1-1-300x142.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic1-1-768x362.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic1-1-65x31.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic1-1-225x106.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic1-1-350x165.png 350w\" sizes=\"auto, (max-width: 856px) 100vw, 856px\" \/><\/p>\n<p style=\"text-align: justify;\">We have a RemoteObject on a machine on which we want to invoke methods from a remote place. Then this can be done by having an instance of Stub Object and Skeleton Object for this RemoteObject. The Skeleton Object will be on the same machine as the RemoteObject and the Stub Object will on the machine of the invoker. The Stub Object and the Skeleton Object would be connected using a socket, and can communicate over the socket. Now for the remote method invocation, the Stub Object has to have all the methods of the RemoteObject available, implementation of these methods will be different. ie. the methods of RemoteObject are available to the invoker on the Stub Object. When the invoker wants to invoke any method on the RemoteObject, he would simply invoke the method on the Stub Object. The implementation of the method on the Stub Object would communicate with the Skeleton over the socket connection and send the method name and the values of the parameters to the skeleton. The Skeleton on receiving the method name and parameters from the stub would now invoke the method locally on the RemoteObject. The method would return some value, The returned value would now be sent back to the Stub by the Skeleton, and the Stub in turn returns it to the invoker. In case the method on RemoteObject throws an exception the Skeleton would catch the exception object and send it to the Stub. The Stub when it receives an exception instead of return value would also throw the exception object received from the skeleton. The process of sending parameters by Stub to skeleton object is known as Marshaling of parameters. The process of receiving parameters by skeleton object is known as UnMarshaling of parameters. The marshaling and unmarshaling of parameters is done on the Socket connection, which is a stream based connection, and therefore they need to be Serializable. If any of the parameters being marshaled is not Serializable then the marshaling process would throw an exception. So initially the Stub marshalls the parameters and the skeleton unmarshalls the parameters, after invoking the method the skeleton in turn marshals the return value and the stub unmarshalls the return value. So to have remote method invocation, we need three objects. The RemoteObject, the Object which has the actual implementation of the methods, whose invocation is desired by the client. The Stub and the Skeleton classes whose objects help the client to achieve the remote invocation. Also it is necessary that the method invoker uses only Serializable objects as parameters and the implementation of methods returns values which are Serializable.<\/p>\n<div style=\"text-align: justify;\">\n<p>&nbsp;<\/p>\n<p>Let us consider an example for a banking application:<\/p>\n<p>&nbsp;<\/p>\n<p>We might consider to have an entity called Bank.<\/p>\n<p>&nbsp;<\/p>\n<p>What is a Bank? Bank is any object which has the methods like<\/p>\n<p>&nbsp;<\/p>\n<p>public long openAccount(AccountType type, String name, long openBal)<\/p>\n<p>public Account getAccount(long acno)<\/p>\n<p>public void deposit(long acno, long amount)<\/p>\n<p>public boolean withdraw(long acno, long amount)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The Bank is identified by the methods it has. ie. the functionality supported by the instance. In case of RMI, the Stub Object has all these methods, and so it is the Bank for the invoker. The Bank is identified by the methods and not by its implementation. We would define Bank as an interface, with the required methods, This would be implemented by the RemoteObject and the Stub Object. The RemoteObject and the Stub have common methods, ie. they implement the same interface. Lets look at the steps for defining a remote object.<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"text-align: justify;\">Identify the methods for remote invocation and define an interface which has the\u00a0\u00a0 methods. This interface has to extend from the interface Remote in the java.rmi package. All the methods in this interface must have throws RemoteException over and above the application related throws. this throws RemoteException is required since the implementation of the methods by the Stub Object can fail because of some failure in the protocol between the Stub and the Skeleton over the network. ie. the failure of the\u00a0\u00a0 method could be because of the failure in the communication between the Stub and the Skeleton Objects. so in case of bank example we might have and interface for Bank defined<\/li>\n<\/ol>\n<\/div>\n<div style=\"text-align: justify;\">\n<p style=\"text-align: justify;\">as below:<\/p>\n<p>&nbsp;<\/p>\n<p>package bank; import java.rmi.*;<\/p>\n<p>public interface Bank extends Remote {<\/p>\n<p>public long openAccount(AccountType type, String name, long openBal) throws RemoteException;<\/p>\n<p>public Account getAccount(long acno) throws RemoteException; public void deposit(long acno, long amount) throws RemoteException;<\/p>\n<p>public boolean withdraw(long acno, long amount) throws RemoteException;<\/p>\n<p>&#8230;.<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><strong>2.<\/strong> Write an implementation class for the above interface. This would be the remote object. This\u00a0 \u00a0class\u00a0 \u00a0has\u00a0 \u00a0to\u00a0 \u00a0inherit\u00a0 \u00a0from\u00a0 \u00a0the\u00a0 \u00a0UnicastRemoteObject\u00a0 \u00a0class\u00a0 \u00a0from\u00a0 \u00a0the\u00a0 \u00a0java.rmi.server package. The UnicastRemoteObject class has a default constructor which throws RemoteException, so the constructor in the class must also have throws RemoteException. We inherit from the UnicastRemoteException class, to inherit the implementations of the methods of \u00a0the \u00a0Object \u00a0class, \u00a0which \u00a0would appropriate \u00a0for \u00a0the RemoteObject.<\/p>\n<p>&nbsp;<\/p>\n<p>So,for the above example we could have a class as below:<\/p>\n<p>&nbsp;<\/p>\n<p>package bank;<\/p>\n<p>&nbsp;<\/p>\n<p>import java.rmi.*;<\/p>\n<p>import java.rmi.server.*;<\/p>\n<p>&nbsp;<\/p>\n<p>public class BankImpl extends UnicastRemoteObject implements Bank { private String name;<\/p>\n<p>private Map&lt;Long, Account&gt; accountMap = new HashMap&lt;&gt;();<\/p>\n<p>&#8230;.<\/p>\n<p>public BankImpl(String name) throws RemoteException {<\/p>\n<p>&#8230;.<\/p>\n<p>}<\/p>\n<p>public long openAccount(AccountType type, String name, long openBal) throws RemoteException;<\/p>\n<p>public Account getAccount(long acno) throws RemoteException; public void deposit(long acno, long amount) throws RemoteException;<\/p>\n<p>public boolean withdraw(long acno, long amount) throws RemoteException;<\/p>\n<p>&#8230;.<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><strong>3.\u00a0<\/strong>Now generate the Stub and Skeleton classes for the RemoteObject class defined in step 2. this is done by using the rmic command available with the jdk. In step 1 and 2 when we compile, we would get two class files as \u00a0Bank.class \u00a0and \u00a0BankImpl.class. \u00a0in \u00a0the \u00a0rmic \u00a0utility use the BankImpl class so command will be<\/p>\n<p>&nbsp;<\/p>\n<p>rmic bank.BankImpl<\/p>\n<\/div>\n<p style=\"text-align: justify;\">This would generate two class files in the bank package as BankImpl_Stub.class and BankImpl_Skel.class.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">So, as far as creating the Bank as a RemoteObject is concerned that is done.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Now we need to use this Remote type in an Application. In this case the application on the server would create an instance of bank.BankImpl and make it available for Remote Method Invocation. The client would have to obtain the stub for this object, for the client to initially obtain the stub object it would require some service where it can connect on the server machine. There is a Naming service available along with the jre for RMI. This is known rmiregistry.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-48\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic2.png\" alt=\"\" width=\"817\" height=\"404\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic2.png 817w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic2-300x148.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic2-768x380.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic2-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic2-225x111.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic2-350x173.png 350w\" sizes=\"auto, (max-width: 817px) 100vw, 817px\" \/><\/p>\n<p style=\"text-align: justify;\">At runtime the rmiregistry would have to be started first, then the application on \u00a0the \u00a0server \u00a0side would create the instance of RemoteObject and bind \u00a0it \u00a0with \u00a0the \u00a0Naming \u00a0service, \u00a0ie. \u00a0make \u00a0it available for remote invocation. rmiregistry needs to be started on the same machine as the RemoteObject The client would then have to connect to the Naming service and obtain the stub corresponding to the RemoteObject which has been bound by the \u00a0application \u00a0on \u00a0the \u00a0server \u00a0side. Now interaction wtth the rmiregistry(Naming service) would be required by the server application as well as the client. There is a class called Naming, in the java.rmi package, which has static methods which will allows interaction with the \u00a0Naming \u00a0service. \u00a0The \u00a0following \u00a0are \u00a0the \u00a0methods \u00a0in the Naming class: (all methods in this class are static)<\/p>\n<p>&nbsp;<\/p>\n<p>static void bind(String key, Remote remoteObject)<\/p>\n<p>static void rebind(String key, Remote remoteObject)<\/p>\n<p>static void unbind(String key)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The above methods are\u00a0 called by the\u00a0 server applicaton only. ie. they\u00a0 cannot be invoked\u00a0 from remote place.<\/p>\n<p>&nbsp;<\/p>\n<p>static Remote\u00a0 lookup(String url)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">This \u00a0method \u00a0is \u00a0invoked \u00a0by \u00a0the \u00a0client \u00a0for \u00a0obtaining \u00a0the \u00a0stub \u00a0object \u00a0for \u00a0the \u00a0given \u00a0remote \u00a0object which has been bound with the rmiregistry. The format for the url will be:<\/p>\n<p>&nbsp;<\/p>\n<p>rmi:\/\/&lt;host&gt;[:&lt;port&gt;]\/&lt;key&gt;<\/p>\n<ol>\n<li style=\"text-align: justify;\">assuming we \u00a0have \u00a0the \u00a0classes \u00a0bank.Bank \u00a0interface, \u00a0bank.BankImpl \u00a0and \u00a0bank.BankImpl_Stub and bank.BankImpl_Stel. then on the server the application might have code like:<\/li>\n<\/ol>\n<p>public static void main(String[] args) { bank.BankImpl bi = new bank.BankImpl(&#8230;.); Naming.rebind(&#8220;SBI&#8221;, bi);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The client application would then look something like:<\/p>\n<p>&nbsp;<\/p>\n<p>public static void main(STring[] args) {<\/p>\n<p>Bank bank = (Bank)Naming.lookup(&#8220;rmi:\/\/127.0.0.1\/SBI&#8221;);<\/p>\n<p>&#8230;..<\/p>\n<p>bank&#8230;&#8230;. \/\/ invoking some method on bank;<\/p>\n<p>}<\/p>\n<div>\n<p>There is a method called list in the Naming class, with one parameter of type String.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">This could be invoked by the server side or by the client. to konw the keys which have been bound in the Naming service.<\/p>\n<p>&nbsp;<\/p>\n<p>The method is:<\/p>\n<p>&nbsp;<\/p>\n<p>static String[] list(String s);<\/p>\n<p>&nbsp;<\/p>\n<p>On the server side it would be invoked as: String[] names = Naming.list(&#8220;*&#8221;);<\/p>\n<p>on the client side it would be invoked as:<\/p>\n<p>&nbsp;<\/p>\n<p>String[] urls\u00a0 \u00a0= Naming.list(&#8220;rmi:\/\/127.0.0.1\/*&#8221;); now look at the following code on the client side.<\/p>\n<p>public static void main(String[] args) {<\/p>\n<\/div>\n<p>Bank bank = (Bank)Naming.lookup(&#8220;rmi:\/\/127.0.0.1\/SBI&#8221;); Account ac = bank.openCurrentAccount(&#8220;name1&#8221;, 10000); int acno = ac.getAccountNumber();<\/p>\n<p>bank.deposit(acno, 10000); ac = bank.getAccount(acno); ac.withdraw(acno, 10000); bank.passbook(acno);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Here we have created an account then deposited some amount into the account now we use the object of the account created by using the getAccount method. and then we withdraw from this account object. is this withdrawal reflected in the account managed in the bank? when we use getAccount method the account instance returned is a copy of the account on the server, and so the withdrawal is not reflected in the passbook method invocation above. ie. in case of rmi here we find pass by value, even for reference types. In rmi pass by reference is still possible. suppose when we call getAccount method, then instead of a copy of the account object, if we could get the instance of a Stub for the account object on the server. then it would be a pass by reference. ie. if the Account was a Remote type then it would be treated as pass by reference automatically. ie. if we have done the same exercise on Account as what is done for Bank. then Account would be passed by reference. ie. we should have Account interface AccountImpl class as an implementation of the Account interface and then we should have also generated the Stub and the Skeleton classes for the Account.<\/p>\n<pre><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-49\" src=\"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic3.png\" alt=\"\" width=\"915\" height=\"404\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic3.png 915w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic3-300x132.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic3-768x339.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic3-65x29.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic3-225x99.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-content\/uploads\/sites\/21\/2018\/07\/M05Pic3-350x155.png 350w\" sizes=\"auto, (max-width: 915px) 100vw, 915px\" \/><\/p>\n<p style=\"text-align: justify;\">So for rmi, primitive types are always pass by value, reference types are also pass by value, except for the Remote types. Remote types are pass by reference, Remote types are the ones for which the Implementaion class, the Stub class and the Skeleton classes are generated.<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Remote Method Invocation<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/HmF2-faAEq8\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"textLayer\">\n<div><strong>Suggested Reading:<\/strong><\/div>\n<div><\/div>\n<div>1. Core Java Volume 2 by Cay Horstmann &amp; Gary Cornell, Ninth Edition, Pearson Education.<\/div>\n<div>2. Beginning Java Networking by Alexander V Konstantinou and others, Wrox publication.<\/div>\n<div>3. Java RMI By William Grosso O&#8217;Rielly media<\/div>\n<div>4. https:\/\/docs.oracle.com\/javase\/tutorial\/rmi\/index.html<\/div>\n<\/div>\n","protected":false},"author":4,"menu_order":5,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["mr-pravin-jain"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-45","chapter","type-chapter","status-publish","hentry","contributor-mr-pravin-jain"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/45","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":8,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/45\/revisions"}],"predecessor-version":[{"id":390,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/45\/revisions\/390"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/45\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapter-type?post=45"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/contributor?post=45"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/license?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}