Summary of RMI usage

Source: Internet
Author: User

Four years ago, it was also the fans of Java, and sun and Java. of course, RMI will not be missed, but it has not been used in any project so far. I did not want to listen to a Java course yesterday, think about how much money I paid and finally I went. The most disgusting thing is that the RMI he talks about has no practical value at all, and some theories are not correct. It is really misleading. academic theories and practices cannot be separated. Teachers can only rely on their own students to enrich themselves. I spent some time last night studying RMI again. Let's take an example as the end of RMI learning.
Writing this article Article Previously, he also visited China Telecom's senior engineer. According to his introduction, RMI has many implementation methods, and now he finds that RMI is not efficient and performance well in large-scale distributed systems. His team is currently developing distributed applications on its own.
With the advent of the Web 2.0 era, the popularization and application of SOA development ideas. Distributed applications will go into another climax.
The RMI development procedure is clear to everyone. Here is an example of how to manually start rmiregistry or specify codebase and web server. we don't have to worry about the details of the underlying TCP/IP. You just need to look at the registration service based on the same thing like soap and JNDI.

1. remote interface
Import java. RMI. Remote;
Import java. RMI. RemoteException;

Public interface irmi extends remote {
Public object invoke (itask task) throws RemoteException;
}
2. remote interface implementation

/**
* @ Author Jack. Wang
*
*/
Import java. RMI. RemoteException;
Import java. RMI. server. unicastremoteobject;

Public class irmiimpl extends unicastremoteobject implements irmi {

Protected irmiimpl () throws RemoteException {
Super ();
}

Public object invoke (itask task) throws RemoteException {
System. Out. println ("Note: This is a remote call ");
Object OBJ = task. dowork ();
System. Out. println ("Return Value for calling the itask. dowork () method:" + obj. tostring ());
// Called by the client. You can play the required music on the server.
Processcaller. callmp3 ();
Return OBJ;
}
}
3. task interface
/**
* @ Author Jack. Wang
*
*/
Import java. Io. serializable;
Public interface itask extends serializable {
Public object dowork ();
}
4. Task implementation class
/**
* @ Author Jack. Wang
*
*/
Public class taskimpl implements itask {

Public object dowork (){
System. Out. println ("CurrentProgramIn remote call ");
Return thread. currentthread (). getname () + ""
+ New date (system. currenttimemillis ());
}
}
5. Call Windows programs in Java
Public class processcaller {

Public static void callmp3 (){
Runtime Ru = runtime. getruntime ();
Try {
// Call the Player file to play the specified MP3
Process p1 = Ru
. Exec ("C :\\ Program Files \ Windows Media Player \ wmplayer D :\\ Jack \ MP3 \ 5. ");
} Catch (exception e ){

}
}
}
6. Server SideCode
/**
* @ Author Jack. Wang
*
*/
Import java. RMI. Naming;
Import java. RMI. Registry. locateregistry;
Import java. RMI. Registry. Registry;

Public class rmiserver {

Public static void registremoteobject () throws exception {
Irmiimpl impl = new irmiimpl ();
Naming. rebind ("RMI: // 210.43.109.25: 1111/mytask", impl );
System. Out. println ("bound success! ");
}

Private Static registry createregistry (){
Registry registry = NULL;
Int Port = 1111;
Try {
Registry = locateregistry. getregistry ("210.43.109.25", Port );
Registry. List ();
System. Out. println ("register the exist server! ");
} Catch (final exception e ){
Try {
Registry = locateregistry. createregistry (port );
System. Out. println ("register the exist server! Port = "+ port );
} Catch (final exception ee ){
Ee. printstacktrace ();
}
}
Return registry;
}

/**
* Register the object to the RMI server.
*/
Public static void BIND (){
Registry registry = NULL;
Registry = createregistry ();
Try {
Irmiimpl impl = new irmiimpl ();
Registry. rebind ("mytask", impl );
System. Out. println ("mytask Server start! ");
} Catch (exception e ){
E. printstacktrace ();
}
}

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Try {
BIND ();
} Catch (exception e ){
E. printstacktrace ();
}
}

}
7. client code

/**
* @ Author Jack. Wang
*
*/
Public class rmiclient {
Public static void getremoteobject () throws exception {
Irmi OBJ = (irmi) Naming. Lookup ("RMI: // 210.43.109.28: 1111/mytask"); // obtain the remotely published Service
Taskimpl task = new taskimpl ();
Object result = obj. Invoke (task); // call the remote service method
System. Out. println (result. tostring ());
}

Public static void main (string [] ARGs ){
Try {
Getremoteobject ();
} Catch (exception e ){
E. printstacktrace ();
}
}
}

Summary:
We recommend that you package the above program into a jar file. You can test it in different machines.
RMI is so simple. If you think he is uncomfortable, encapsulate the socket by yourself. I will be notified at that time. I will also learn.

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.