{"id":2030,"date":"2013-01-23T10:38:47","date_gmt":"2013-01-23T08:38:47","guid":{"rendered":"https:\/\/blog.blackbam.at\/?p=2030"},"modified":"2013-01-23T10:38:47","modified_gmt":"2013-01-23T08:38:47","slug":"java-rmi-5-minutes-tutorial","status":"publish","type":"post","link":"https:\/\/blog.blackbam.at\/de\/2013\/01\/23\/java-rmi-5-minutes-tutorial\/","title":{"rendered":"Java RMI five Minutes tutorial"},"content":{"rendered":"<p>This ultra short tutorial is just a summary of how Java RMI (remote method invocation) works. For explanation please check one of the other tutorials which you will find easily easily your favorite search engine.<\/p>\n<h2>1. Write RMI-Server Interface<\/h2>\n<ul>\n<li>extends package java.rmi.Remote<\/li>\n<li>provide all methods to be called remotely<\/li>\n<li>each methods must through a remote exception<\/li>\n<\/ul>\n<pre lang=\"java\">\r\nimport java.rmi.*;\r\n \r\npublic interface ServerInterface extends Remote {\r\n     public void method1() throws RemoteException;\r\n     public int method2() throws RemoteException;\r\n     \/\/ ...\r\n}<\/pre>\n<h2>2. Write Server Class<\/h2>\n<ul>\n<li>implements RMI-Server Interface<\/li>\n<li>extends UnicastRemoteObject (at least for simple RMI)<\/li>\n<li>Constructor throws java.rmi.RemoteException<\/li>\n<li>must be registered at local registry<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<pre lang=\"java\"> \r\nimport java.rmi.*;\r\nimport java.net.MalformedURLExcpetion;\r\nimport java.rmi.registry.*;\r\nimport java.rmi.server.UnicastRemoteObject;\r\n \r\npublic class MyServer extends UnicastRemoteObject implements ServerInterface {\r\n \r\n  MyServer() throws RemoteException {\r\n    super();\r\n  }\r\n \r\n  public static void main(String[] args) {\r\n\r\n    try {\r\n      LocateRegistry.createRegistry(Registry.REGISTRY_PORT);\r\n    } catch (RemoteException ex) {\r\n      System.out.println(ex.getMessage());\r\n    }\r\n    try {\r\n      Naming.rebind(\"MyServer\", new MyServer());\r\n    } catch (MalformedURLException ex) {\r\n      System.out.println(ex.getMessage());\r\n    }\r\n    catch (RemoteException ex) {\r\n      System.out.println(ex.getMessage());\r\n    }\r\n  }\r\n  \/\/ ...\r\n}<\/pre>\n<h2>3. Write a client<\/h2>\n<ul>\n<li>get remote reference<\/li>\n<li>call remote methods<\/li>\n<li>be careful with rmi-specific problems (a more detailed tutorial will explain to you)<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<pre lang=\"java\">\/\/ any class, ...\r\n\/\/ url expects String in url format (e.g. http:\/\/127.0.0.1\/MyServer for local testing)\r\n \r\ntry {\r\n      ServerInterface server = (ServerInterface) Naming.lookup(url);\r\n      server.method1();\r\n      int calculated_by_server = server.method2();\r\n      \/\/ ...\r\n} catch (Exception ex) {\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>API reference:<\/strong><\/p>\n<ul>\n<li>RMI:\u00a0<a href=\"http:\/\/java.sun.com\/javase\/6\/docs\/api\/index.html?java\/rmi\/Remote.html\" rel=\"nofollow\">Remote API<\/a>,\u00a0<a href=\"http:\/\/java.sun.com\/javase\/6\/docs\/api\/index.html?java\/rmi\/server\/UnicastRemoteObject.html\" rel=\"nofollow\">UnicastRemoteObject API<\/a>,\u00a0<a href=\"http:\/\/java.sun.com\/javase\/6\/docs\/api\/index.html?java\/rmi\/registry\/Registry.html\" rel=\"nofollow\">Registry API<\/a>,\u00a0<a href=\"http:\/\/java.sun.com\/javase\/6\/docs\/api\/index.html?java\/rmi\/registry\/LocateRegistry.html\" rel=\"nofollow\">LocateRegistry API<\/a><\/li>\n<li>Properties:\u00a0<a href=\"http:\/\/java.sun.com\/javase\/6\/docs\/api\/index.html?java\/util\/Properties.html\" rel=\"nofollow\">Properties API<\/a><\/li>\n<li>IO:\u00a0<a href=\"http:\/\/java.sun.com\/javase\/6\/docs\/api\/index.html?java\/io\/package-summary.html\" rel=\"nofollow\">IO Package API<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Further reading:<\/strong><br \/>\n<a href=\"http:\/\/docs.oracle.com\/javase\/tutorial\/rmi\/index.html\">http:\/\/docs.oracle.com\/javase\/tutorial\/rmi\/index.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This ultra short tutorial is just a summary of how Java RMI (remote method invocation) works. For explanation please check one of the other tutorials which you will find easily easily your favorite search engine. 1. Write RMI-Server Interface extends package java.rmi.Remote provide all methods to be called remotely each methods must through a remote [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[191,190],"tags":[206,209,208,176],"class_list":["post-2030","post","type-post","status-publish","format-standard","hentry","category-java","category-programming","tag-networking","tag-remote","tag-rmi","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/blog.blackbam.at\/de\/wp-json\/wp\/v2\/posts\/2030","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.blackbam.at\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.blackbam.at\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.blackbam.at\/de\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.blackbam.at\/de\/wp-json\/wp\/v2\/comments?post=2030"}],"version-history":[{"count":0,"href":"https:\/\/blog.blackbam.at\/de\/wp-json\/wp\/v2\/posts\/2030\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.blackbam.at\/de\/wp-json\/wp\/v2\/media?parent=2030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.blackbam.at\/de\/wp-json\/wp\/v2\/categories?post=2030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.blackbam.at\/de\/wp-json\/wp\/v2\/tags?post=2030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}