{"id":854,"date":"2010-09-02T20:49:33","date_gmt":"2010-09-02T23:49:33","guid":{"rendered":"http:\/\/wordpress.matbra.com\/?p=854"},"modified":"2010-09-03T11:17:12","modified_gmt":"2010-09-03T14:17:12","slug":"java-rmi-introducao-e-exemplo","status":"publish","type":"post","link":"https:\/\/wordpress.matbra.com\/en\/2010\/09\/02\/java-rmi-introducao-e-exemplo\/","title":{"rendered":"Java RMI &#8211; Intro and Example"},"content":{"rendered":"<p>Good Night,<\/p>\n<p>Java RMI (Remote Method Invocation), is a mechanism that let the user, create distributed applications using Java. It takes care of Sockets, protocols and others. But how it works? All the magic is done by RMI. It have a server named RMIREGISTRY, where your server connect and says &#8220;I&#8217;m here&#8221;. After this Clients can connect to RMIREGISTRY and find the server that it wants. <\/p>\n<p>I will show you an example of a Car Advertisement server, where Clients can add and search for Vehicle.<\/p>\n<p><!--more--><\/p>\n<p>Server Interface<br \/>\n[code lang=&#8221;java&#8221;]<br \/>\nimport java.rmi.Remote;<br \/>\nimport java.rmi.RemoteException;<br \/>\nimport java.util.List;<\/p>\n<p>public interface MethosInterfaces extends Remote {<\/p>\n<p>   public List<Vehicle> search(String field, String value) throws RemoteException;<br \/>\n   public void add(Vehicle v) throws RemoteException;<br \/>\n}<br \/>\n[\/code] <\/p>\n<p>The methods in this interface will be implemented by our server, this interface extends Remote, it is a necessary class to make RMI works. All the methods must handle exceptions.<\/p>\n<p>Server<br \/>\n[code lang=&#8221;java&#8221;]<br \/>\npublic class ServerAn implements MethosInterfaces {<br \/>\n[\/code]<\/p>\n<p>Our Server that extends our Interface and implement the methods. <\/p>\n<p>To create a new Server I used this:<br \/>\n[code lang=&#8221;java&#8221;]<br \/>\npublic static void main(String[] args) {<br \/>\n\t\ttry {<br \/>\n\t\t\tServerAn server = new ServerAn();<br \/>\n\t\t\tMethosInterfaces stub = (MethosInterfaces) UnicastRemoteObject.exportObject(server, 0);<br \/>\n\t\t\tRegistry registry = LocateRegistry.getRegistry();<br \/>\n\t        registry.bind(&#8220;Server&#8221;, stub);<br \/>\n\t\t\tSystem.out.println(&#8220;Servider running&#8221;);<br \/>\n\t\t} catch (Exception ex) {<br \/>\n\t\t\tex.printStackTrace();<br \/>\n\t\t}<br \/>\n\t}<br \/>\n[\/code]<\/p>\n<p>We created a new ServerAN, export our server, and registry it with the bind. <\/p>\n<p>Client Sender:<br \/>\n[code lang=&#8221;java&#8221;]<br \/>\npublic class ClientSender {<\/p>\n<p>\tpublic static void main(String[] args) {<br \/>\n\t\ttry {<br \/>\n\t\t\tRegistry registry = LocateRegistry.getRegistry(&#8220;localhost&#8221;);<br \/>\n\t\t\tMethosInterfaces stub = (MethosInterfaces) registry.lookup(&#8220;Server&#8221;);<br \/>\n\t\t\tSystem.out.println(&#8220;Sending new Vehicle to server&#8221;);<br \/>\n\t\t\tstub.add(new Vehicle(&#8220;Matheus2&#8221;, 5000, &#8220;Caminh\u00e3o&#8221;, &#8220;&#8221;, 2008));<br \/>\n\t\t} catch (Exception ex) {<br \/>\n\t\t\tex.printStackTrace();<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>Basically, we search by RMIREGISTRY in &#8220;localhost&#8221; and search by our server named &#8220;Server&#8221;, with the reference to it we add a new Vehicle. To search is almost the same thing but we will receive a List<Vehicle>. If you want to receive a custom Object, you must implement Serializable. <\/p>\n<p><a href='http:\/\/wordpress.matbra.com\/wp-content\/uploads\/JavaRMI-Example.tar.gz'>Java RMI &#8211; Car Advertisement (Example)<\/a><\/p>\n<p>Remember: rmiregistry search in your JAVA PATH and in the dir that you run it for the interfaces. So in this simple example, access the source folder, compile it, and then execute the RMIREGISTRY in the same dir. After this the server and then the clients. (All in the same this, you can use more than one prompt) <\/p>\n<p>Matheus<\/p>\n<p>PS: Java ME don&#8217;t come with RMI by default.<\/p>\n<p>References:<br \/>\n<a href=\"http:\/\/infolab.stanford.edu\/CHAIMS\/Doc\/Details\/Protocols\/rmi\/rmi_description.html\" target=\"_blank\">http:\/\/infolab.stanford.edu\/CHAIMS\/Doc\/Details\/Protocols\/rmi\/rmi_description.html<\/a><br \/>\n<a href=\"http:\/\/www.oracle.com\/technetwork\/java\/javase\/tech\/index-jsp-136424.html\" target=\"_blank\">http:\/\/www.oracle.com\/technetwork\/java\/javase\/tech\/index-jsp-136424.html<\/a><br \/>\n<\/p>","protected":false},"excerpt":{"rendered":"<p>Good Night, Java RMI (Remote Method Invocation), is a mechanism that let the user, create distributed applications using Java. It takes care of Sockets, protocols<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/wordpress.matbra.com\/en\/2010\/09\/02\/java-rmi-introducao-e-exemplo\/\">Continue reading<span class=\"screen-reader-text\">Java RMI &#8211; Intro and Example<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[47],"tags":[],"class_list":["post-854","post","type-post","status-publish","format-standard","hentry","category-java","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/psjGE-dM","_links":{"self":[{"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/posts\/854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/comments?post=854"}],"version-history":[{"count":3,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/posts\/854\/revisions"}],"predecessor-version":[{"id":859,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/posts\/854\/revisions\/859"}],"wp:attachment":[{"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/media?parent=854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/categories?post=854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/tags?post=854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}