Java learning path-RMI learning, java path-rmi

Source: Internet
Author: User

Java learning path-RMI learning, java path-rmi

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. Four steps to create an 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 object class. This class must implement the Serializable interface for information transmission.

 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 }

2. Define an interface that inherits the Remote interface. The method in this interface must declare a RemoteException.

 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 }

3. Create a class and implement the interface in step 2, but inherit the UnicastRemoteObject class and write the constructors without parameters.

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 ("James "); 29 s1.setAge (15); 31 Student s2 = new Student (); 33 s2.setName (""); 35 s2.setAge (20); 37 list. add (s1); 39 list. add (s2); 41 return list; 43} 45}

4. Create and start the service

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 StudentServiceImpl (); 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}

5. Create a customer program for RMI calling.

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}

6. console display results

================= Console =================

Name: Zhang San, age: 15

Name: Li Si, age: 20

====================================

Configure the Rmi service in Spring

Combining Rmi with Spring is much more convenient than implementing the Rmi service.

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 and does not require additional operations.

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 <Student> (); 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}

3. Configure the required information in applicationContext. xml.

A. first define the service bean

<bean id="studentService" class="service.StudentServiceImpl"></bean>

B. Define the export service

      <bean class="org.springframework.remoting.rmi.RmiServiceExporter"        p:service-ref="studentService"        p:serviceInterface="service.StudentService"        p:serviceName="StudentService"        p:registryPort="5008"      />

You can also add the p: registryHost attribute to set hosts.

C. Define the service bean in applicationContext. xml of the client (in this example, the exported service bean and the client bean are placed in an applicationContext. xml)

 <bean id="getStudentService"  class="org.springframework.remoting.rmi.RmiProxyFactoryBean"  p:serviceUrl="rmi://127.0.0.1:5008/StudentService"  p:serviceInterface="service.StudentService" />

D. There are so many configuration items, isn't it more convenient than the above reality! Now let's test it.

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}

 

================= Console =================
Name: Zhang San, age: 15
Name: Li Si, age: 20
==================================
The preceding mian method may report an error. It should be because the jar of spring is missing. Add it as needed.

I wrote a blog for the first time. Please point out some mistakes.

 


JAVA learning path

Linux: the operating system (the same as winXP) has no essential relationship with java, but in the company, it is generally required to use common linux commands.
XML: Data Transmission Format. Learn more about Java Web.
Strut: One of the open-source Java Web frameworks, one of the essential technologies of Java Web

Spring: One of the open-source frameworks of javaweb, one of the essential technologies of javaweb
Hibernate: One of the open-source frameworks of javaweb, one of the essential technologies of javaweb

The above three constitute the most common enterprise-level development framework technology ssh, (each technology name starts with a letter)

SERVLET: One of the basic Java Web technologies, required. This is the foundation of ssh and must be learned well.
Jsp: javaweb display layer, required!
We recommend that you first learn java BASICS (such as interfaces, inheritance, and control statements) and then javaweb, otherwise, Java Web will learn servlet, jsp, and javabean before learning framework technology. In framework technology, I first learned struct2 spring

Java Web can be said to be used as a website, while javase is one of the major functions of java3 (j2s, J2ee, and javase ).
J2ee is enterprise-level development,
Java EE is standard development. j2ee and javase (also called j2se) are basically used for developing desktop applications and websites. However, the former has better functions than the latter.

The path to java learning is lost.

Framework really does not need to spend time to study, especially for recent graduates. The key is the object-oriented design ideas, design patterns, etc. Do you have to try to write some programs or projects by yourself? If you don't have it, do it. It's useless to talk about it on paper. You will find problems, solve problems, improve yourself, and add colors to your resume (this is also a project experience ), there is also the need to standardize the code, and the program is the first to achieve the goal, but maintainability and scalability are also the top priority, because in actual development, the Code only accounts for 20%, and the continuous maintenance after delivery to the customer accounts for 80%, if your code is ugly and has no hierarchy or other concepts, it will be very painful for you to really master the object-oriented architecture. After the design mode, the SSH stream will be as easy as it is, for example, the responsibility chain model in struts1.3 and Spring AOP. Otherwise, you will not be able to get out of the new framework. The foundation is fundamental, and the thinking is the key.

Related Article

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.