{"id":852,"date":"2010-04-08T11:44:23","date_gmt":"2010-04-08T10:44:23","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=852"},"modified":"2010-04-08T11:44:23","modified_gmt":"2010-04-08T10:44:23","slug":"rmi-the-library-factory","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/04\/08\/rmi-the-library-factory\/","title":{"rendered":"RMI &#8211; the Library factory"},"content":{"rendered":"<p>Now we have the <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/04\/08\/rmi-implementation-of-the-library-book\/\">library book<\/a>, we can now build up the RMI library factory that will be the server for the clients to communicate to.<\/p>\n<p>To start with we have to setup the security settings<\/p>\n<pre lang=\"java\">\r\nSystem.setSecurityManager(new RMISecurityManager());\r\n<\/pre>\n<p>this is because the RMI library factory needs to &#8220;listen&#8221; on the server to remote clients to connect and due to the Java <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/guide\/security\/spec\/security-spec.doc1.html\">sandbox<\/a> we have to make a request to the security manager to be allowed to &#8220;listen&#8221; for remote clients, without the permission there will be a error.<\/p>\n<pre lang=\"bash\">\r\nRemote error :Connection refused to host: 127.0.1.1; nested exception is: \r\n        java.net.ConnectException: Connection refused\r\n<\/pre>\n<p>I shall include the <a href=\"http:\/\/java.sun.com\/j2se\/1.3\/docs\/guide\/security\/PolicyFiles.html\">policy file<\/a> when it comes to running the server\/client setup.<\/p>\n<p>now all we need to do is to bind the Book class RMI server and call it the &#8220;RemoteBook&#8221;, you need this to call with the client because you are basically allowing the server to communicate with the client to the Book that has been created (new Book(&#8220;Genuxs book&#8230; ) )<\/p>\n<pre lang=\"java\">\r\nBook remoteBook = new Book(\"Genuxs book to borrow\");\r\nNaming.rebind(\"RemoteBook\",remoteBook);\r\n<\/pre>\n<p>here is the full RMI Factory code<\/p>\n<pre lang=\"java\">\r\nimport java.rmi.*;\r\nimport java.rmi.server.*;\r\nimport java.net.*;\r\n\r\npublic class RMIFactory {\r\n\r\n\tpublic RMIFactory()\r\n\t{\r\n\t\tSystem.setSecurityManager(new RMISecurityManager());\r\n\t}\r\n\t\r\n\tpublic void createRemoteBook()\r\n\t{\r\n\t\ttry {\r\n\t\t\tBook remoteBook = new Book(\"Genuxs book to borrow\");\r\n\t\t\tNaming.rebind(\"RemoteBook\",remoteBook);\r\n\t\t\t\r\n\t\t} \r\n\t\tcatch (RemoteException e)\r\n\t\t{\r\n\t\t\tSystem.out.println(\"Remote error :\"+ e.getMessage());\r\n\t\t}\r\n\t\tcatch (MalformedURLException mue)\r\n\t\t{\r\n\t\t\tSystem.out.println(\"Remote malformed error :\"+mue.getMessage());\r\n\t\t}\r\n\t\t\r\n\t\tSystem.out.println(\"Remote book object has been started\");\r\n\t}\r\n\t\r\n\tpublic static void main(String[] args) {\r\n\t\tRMIFactory RMIF = new RMIFactory();\r\n\t\tRMIF.createRemoteBook();\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>if you save that as RMIFactory.java and next it is the client and running the RMI server and RMI client.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now we have the library book, we can now build up the RMI library factory that will be the server for the clients to communicate to. To start with we have to setup the security settings System.setSecurityManager(new RMISecurityManager()); this is because the RMI library factory needs to &#8220;listen&#8221; on the server to remote clients to &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/04\/08\/rmi-the-library-factory\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">RMI &#8211; the Library factory<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[172],"class_list":["post-852","post","type-post","status-publish","format-standard","hentry","category-java","tag-rmi"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=852"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/852\/revisions"}],"predecessor-version":[{"id":853,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/852\/revisions\/853"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}