Java learning path-RMI Learning
Java Remote Method Invocation (RMI) is an application programming interface used in Java programming language to implement Remote process Invocation. It allows programs running on the client to call objects on the remote server. The remote method call feature enables Java programmers to distribute operations in the network environment. The purpose of RMI is to simplify the use of remote interface objects as much as possible. 1. Create six steps for the RMI program. 1. Define a remote interface. Each method in this interface must declare that it will generate a RemoteException. 2. Define a class to implement this interface. 3. Create a service for releasing the class defined in 2. 4. Create a customer program to call RMI. Ii. Detailed Implementation of the Program 1. First, create an entity class. This class must implement the Serializable interface for information transmission. Copy code 1 import java. io. serializable; 3 public class Student implements Serializable {5 private String name; 7 private int age; 9 public String getName () {11 return name; 13} 15 public void setName (String name) {17 this. name = name; 19} 21 public int getAge () {23 return age; 25} 27 public void setAge (int age) {29 this. age = age; 31} 33} copy Code 2. define an interface that inherits the Remote interface. The method in this interface must declare a RemoteException. Copy code 1 import java. rmi. remote; 3 import java. rmi. remoteException; 5 import java. util. list; 6 public interface StudentService extends Remote {12 List <Student> getList () throws RemoteException; 14} copy code 3. create a class and implement the interface in step 2, but you also need to inherit the UnicastRemoteObject class and display the constructors without parameters. Copy code 1 import java. rmi. remoteException; 3 import java. rmi. server. unicastRemoteObject; 5 import java. util. arrayList; 7 import java. util. list; 11 public class StudentServiceImpl extends UnicastRemoteObject implements13 StudentService {15 public StudentServiceImpl () throws RemoteException {17} 21 public List <Student> getList () throws RemoteException {23 List <Student> list = new ArrayList <Student> (); 25 Student s1 = new Student (); 27 s1.setName ("Zhang San"); 29 s1.setAge (15); 31 Student s2 = new Student (); 33 s2.setName ("Li Si "); 35 s2.setAge (20); 37 list. add (s1); 39 list. add (s2); 41 return list; 43} 45} copy code 4. create a service and start the service copy code 1 import java. rmi. naming; 2 import java. rmi. registry. locateRegistry; 4 public class SetService {6 public static void main (String [] args) {8 try {10 StudentService studentService = new StudentServi CeImpl (); 12 LocateRegistry. createRegistry (5008); // defines the port number 14 Naming. rebind ("rmi: /127.0.0.1: 5008/StudentService", studentService); 16 System. out. println ("service started"); 18} catch (Exception e) {20 e. printStackTrace (); 22} 24} 26} copy code 4. create a customer program to call RMI. Copy code 1 import java. rmi. naming; 3 import java. util. list; 5 public class GetService {9 public static void main (String [] args) {11 try {13 StudentService studentService = (StudentService) Naming. lookup ("rmi: // 127.0.0.1: 5008/StudentService"); 15 List <Student> list = studentService. getList (); 17 for (Student s: list) {19 System. out. println ("name:" + s. getName () + ", age:" + s. getAge (); 21} 23} catch (Exception e) {25 e. printStackTrace (); 27} 29} 33} copy Code 5. console display result ================ console ============ name: Zhang San, age: 15 Name: Li Si, age: 20 ==================================== configure the Rmi service in Spring when combined with Spring, it is much easier to implement Rmi services than above. 1. first, we define the interface. At this time, the defined interface does not need to inherit other interfaces, but is a common interface 1 package service; 3 import java. util. list; 5 public interface StudentService {7 List <Student> getList (); 9} 2. define a class to implement this interface. This class only needs to implement the interface with certain steps. No additional operation is required to copy code 1 package service; 4 import java. util. arrayList; 6 import java. util. list; 9 public class StudentServiceImpl implements StudentService {11 public List <Student> getList () {13 List <Student> list = new ArrayList <Stu Dent> (); 15 Student s1 = new Student (); 17 s1.setName ("Zhang San"); 19 s1.setAge (15); 21 Student s2 = new Student (); 23 s2.setName ("Li Si"); 25 s2.setAge (20); 27 list. add (s1); 29 list. add (s2); 31 return list; 33} 35} copy code 3. in applicationContext. xml configuration information. first, define the service bean <bean id = "studentService" class = "service. studentServiceImpl "> </bean> B. define the export service copy Code <bean class = "org. springframework. remoting. rmi. rmiServiceExporter "p: s Ervice-ref = "studentService" p: serviceInterface = "service. studentService "p: serviceName =" StudentService "p: registryPort =" 5008 "/> You can also add the p: registryHost attribute to host c. in the client's applicationContext. the bean of the service defined in xml (in this example, the bean of the export service and the client is placed in an applicationContext. <bean id = "getStudentService" class = "org. springframework. remoting. rmi. rmiProxyFactoryBean "p: serviceUrl =" rmi: /127.0.0.1: 5008/StudentService "p: ser ViceInterface = "service. StudentService"/> d. There are so many configuration items. Is it more convenient than the above reality! Now let's test the copy code 1 package service; 2 import java. util. list; 3 import org. springframework. context. applicationContext; 4 import org. springframework. context. support. classPathXmlApplicationContext; 5 public class Test {6 public static void main (String [] args) {7 ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext. xml "); 8 StudentService studentService = (StudentService) ctx. getBean ("getStudentService"); 9 List <Student> list = studentService. getList (); 10 for (Student s: list) {11 System. out. println ("name:" + s. getName () + ", age:" + s. getAge (); 12} 13} 14}