RMI usage learning summary

Time:2021-10-8

Four years ago   Java    of   Fans have also been pursuing in full swing   Sun, pursuing   java.   of course   RMI   I won’t let it go, but I haven’t used it in any project so far. I heard one yesterday   java   I didn’t want to listen to the teacher’s class, but I think I handed in so much   Money, finally. The most disgusting thing is what he said   RMI   There is no practical value at all, and some theories are wrong. It’s really misleading. Academic theory and practice can’t be separated. Teachers are like this. They can only rely on the students themselves. Last night, I spent some time studying it again   RMI。 As an example   RMI   The end of learning.
         Before writing this article, I also interviewed the senior engineer of China Telecom. According to his introduction, RMI   There are many ways to implement it, and now he finds   RMI   In large-scale distributed systems, the efficiency and performance are not very good. At present, his team is developing distributed applications by itself.
         along with   web   two   With the advent of the era, SOA   The popularization and application of development ideas. Distributed applications will reach a climax again.
         RMI    Everyone knows the development steps very well. Here is an example without manual startup   rmiRegistry,   Don’t specify either   codebase   And Web   server   Bottom layer   TCP/IP   We don’t care about the details. Just look at him   SOAP、JNDI   Just wait for the same thing based on the registration service   .

one   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 of calling itask. Dowork() method:”  +  obj.toString());
  //  Called by the client, you can play the required music on the server
  ProcessCaller.callMp3();
  return obj;
 }
}
three    Task interface
/**
 * @author Jack.Wang
 * 
 */
import java.io.Serializable;
public interface ITask extends Serializable {
 public Object doWork();
}
four    Task implementation class
/**
 * @author Jack.Wang
 * 
 */
public class TaskImpl implements ITask {

 public Object doWork() {
   System.out.println (“the current program is in remote call”);
  return Thread.currentThread().getName() + ” ”
    + new Date(System.currentTimeMillis());
 }
}
5. calling windows program 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. Don’t come back in autumn – Wang Qiang. Mp3 “);
  } catch (Exception e) { 

  }
 }
}
six   Server   End code  
/**
 * @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 with 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();
  }
 } 

}
seven   Client   End 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 “);  //  Get services published remotely
  TaskImpl task = new TaskImpl();
   Object   result  =  obj.invoke(task);  //  Method of calling remote service
  System.out.println(result.toString());
 } 

 public static void main(String[] args) {
  try {
   getRemoteObject();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

Summary:
         It is recommended to package the above programs into   jar   File, you can test between different machines.
         RMI   It’s that simple. If you think he’s upset, seal it yourself   socket.   Let me know then. I’ll study, too.

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]