RMI Network programming: How to build a distributed Java RMI based on JDK1.5

Source: Internet
Author: User

1. Create a server-side project in Eclipse. Then, create an interface that is the method definition that you want to open to the client side. It is called: Usermanagerinterface, and must inherit the remote interface.

1  Packagedataserver.rmi.stub;2  3 ImportJava.rmi.Remote;4 Importjava.rmi.RemoteException;5  6 ImportDataserver.rmi.bean.Account;7  8  Public InterfaceUsermanagerinterfaceextendsremote{9      PublicString GetUserName ()throwsremoteexception;Ten      PublicAccount Getadminaccount ()throwsremoteexception; One}

2. To prove how easy it is to "object-oriented" or "seamlessly pass Java object" in RMI, we need to define an account class that is a bean that must implement the implements serializable serialization interface. This is a serializable object that can be transferred between client and server.

 PackageDataserver.rmi.bean; Importjava.io.Serializable;  Public classAccountImplementsserializable,cloneable{/**      *       */     Private Static Final LongSerialversionuid = -1858518369668584532l; PrivateString username; PrivateString password;  PublicString GetUserName () {returnusername; }      Public voidSetusername (String username) { This. Username =username; }      PublicString GetPassword () {returnpassword; }      Public voidSetPassword (String password) { This. Password =password; }       } 

3. At this point, you need to implement an interface that you have already opened:

 PackageDataserver.rmi; Importjava.rmi.RemoteException; ImportDataserver.rmi.bean.Account;ImportDataserver.rmi.stub.UserManagerInterface;  Public classUsermanagerimplImplementsUsermanagerinterface { PublicUsermanagerimpl ()throwsRemoteException {//super (); //TODO auto-generated Constructor stub//Unicastremoteobject.exportobject (this);    }      /**      *       */     Private Static Final LongSerialversionuid = -3111492742628447261l;  PublicString GetUserName ()throwsRemoteException {//TODO auto-generated Method Stub        return"Tommy Lee."; }       PublicAccount Getadminaccount ()throwsRemoteException {//TODO auto-generated Method StubAccount account=NewAccount (); Account.setusername ("Admin"); Account.setpassword ("Admin"); returnAccount ; }  } 

4. Define a main program entry to register the RMI interface you have implemented, including open ports. It's actually very simple:

Name our interface named "Usermanager" for easy client invocation

1  PackageDataserver.entry;2  3 Importjava.rmi.AlreadyBoundException;4 Importjava.rmi.RemoteException;5 ImportJava.rmi.registry.LocateRegistry;6 ImportJava.rmi.registry.Registry;7 ImportJava.rmi.server.UnicastRemoteObject;8  9 ImportDataserver.rmi.UserManagerImpl;Ten ImportDataserver.rmi.stub.UserManagerInterface; One   A  Public classEntry { -   -      Public Static voidMain (String []args)throwsalreadyboundexception, remoteexception{ the       -   -Usermanagerimpl usermanager=NewUsermanagerimpl (); -Usermanagerinterface usermanageri= (usermanagerinterface) unicastremoteobject.exportobject (userManager,0);  +             //Bind The remote object ' s stub in the registry -Registry Registry = Locateregistry.createregistry (2001);  +Registry.rebind ("Usermanager", Usermanageri);  ASYSTEM.OUT.PRINTLN ("Server is ready");  at     }  -}
View Code

5. The server-side code is all written, but the Bean Class (account) and the interface Class (Usermangerinterface) are packaged into jars so that they can be imported into the client-side project below.

Project-"Right-click"-"Export-" jar--"Select Bean and interface--" named rmiserverinterface.jar--"Finish

6. Start creating a client-side program. Create a new project. After the creation is complete, import the jar package into the client project.

7. After importing our interface jar, you can start writing a client-side main program and call the server-side method.

1  PackageWeiblog.rmi;2 Importjava.rmi.NotBoundException;3 Importjava.rmi.RemoteException;4 ImportJava.rmi.registry.LocateRegistry;5 ImportJava.rmi.registry.Registry;6  7 ImportDataserver.rmi.stub.UserManagerInterface;8  9  Public classEntry2 {Ten   One      Public Static voidMain (String []args) { A           -         Try {  -Registry Registry = locateregistry.getregistry ("localhost", 2001);  theUsermanagerinterface Usermanager = (usermanagerinterface) registry.lookup ("Usermanager");  -System.out.println ("" +Usermanager.getadminaccount (). GetUserName () -+Usermanager.getadminaccount (). GetPassword ());  -}Catch(RemoteException e) { +             //TODO auto-generated Catch block - e.printstacktrace (); +}Catch(notboundexception e) { A             //TODO auto-generated Catch block at e.printstacktrace (); -         }  -           -     }  -}
View Code

8. Start the server-side main program, and then start the client-side main program.

Server Console Printing: server is ready

Client Console printing: Adminadmin

Refer to: http://www.cnblogs.com/end/archive/2012/11/16/2772812.html

RMI Network programming: How to build a distributed Java RMI based on JDK1.5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.