Java RMI helloworld

Source: Internet
Author: User

First, RMI is integrated in Spring:

Http://blog.csdn.net/partner4java/article/details/7285055

Then, we will write a helloworld separately, which is divided into several steps.
1. Interface Definition. You need to implement remote
2. Implementation
3. Registration enabled
4. Call

For more information, see the JDK documentation.

Package COM. partner4java. demo. RMI; import Java. RMI. remote; import Java. RMI. remoteException;/*** first define an interface <br/> * implement remote ** @ author partner4java **/public interface Hello extends remote {Public String sayhello (string name) throws RemoteException;} package COM. partner4java. demo. RMI; import Java. RMI. alreadyboundexception; import Java. RMI. remoteException; import Java. RMI. registry. locateregistry; import J Ava. RMI. registry. registry; import Java. RMI. server. unicastremoteobject;/*** external services <br/> * There are three main steps: createregistry, exportobject, bind ** @ author partner4java **/public class helloservice implements Hello {Public String sayhello (string name) throws RemoteException {system. out. println ("hello" + name); Return "hello" + name;} public void start () throws RemoteException, alreadyboundexception {// use the provided specific port Guide To receive incoming calls. Hello = (Hello) unicastremoteobject. exportobject (this, 9001); // return the local host's reference to the remote object registry on the specified port. Registry registry = locateregistry. createregiregistry (9000); registry. BIND ("hello", hello);} public static void main (string [] ARGs) throws RemoteException, alreadyboundexception {New helloservice (). start () ;}} package COM. partner4java. demo. RMI; import Java. RMI. notboundexception; import Java. RMI. remoteException; import Java. RMI. registry. locateregistry; import Java. RMI. registry. registry;/*** call ** @ author partner4java **/public class helloclient {public static void main (string [] ARGs) throws RemoteException, notboundexception {registry = locateregistry. getregistry ("127.0.0.1", 9000); Hello = (Hello) registry. lookup ("hello"); system. out. println (hello. sayhello ("world "));}}

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.