Java远程方法调用,即Java RMI(Java Remote Method Invocation)是Java编程语言里,一种用于实现远程过程调用的应用程序编程接口。它使客户机上运行的程序可以调用远程服务器上的对象。远程方法调用特性使Java编程人员能够在网络环境中分布操作。RMI全部的宗旨就是尽可能简化远程接口对象的使用。
一、创建RMI程序的4个步骤
1、定义一个远程接口的接口,该接口中的每一个方法必须声明它将产生一个RemoteException异常。
2、定义一个实现该接口的类。
3、创建一个服务,用于发布2中定义的类。
4、创建一个客户程序进行RMI调用。
二、程序的详细实现
1.首先我们先创建一个实体类,这个类需要实现Serializable接口,用于信息的传输。
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2.定义一个接口,这个接口需要继承Remote接口,这个接口中的方法必须声明RemoteException异常。
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;
public interface StudentService extends Remote {
List<Student> getList() throws RemoteException;
}
3.创建一个类,并实现步骤2中的接口,但还需要继承UnicastRemoteObject类和显示写出无参的构造函数。
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;
public class StudentServiceImpl extends UnicastRemoteObject implements
StudentService {
public StudentServiceImpl() throws RemoteException {
}
public List<Student> getList() throws RemoteException {
List<Student> list=new ArrayList<Student>();
Student s1=new Student();
s1.setName("张三");
s1.setAge(15);
Student s2=new Student();
s2.setName("李四");
s2.setAge(20);
list.add(s1);
list.add(s2);
return list;
}
}
4.创建服务并启动服务
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
public class SetService {
public static void main(String[] args) {
try {
StudentService studentService=new StudentServiceImpl();
LocateRegistry.createRegistry(5008);//定义端口号
Naming.rebind("rmi://127.0.0.1:5008/StudentService", studentService);
System.out.println("服务已启动");
} catch (Exception e) {
e.printStackTrace();
}
}
}
5. 创建一个客户程序进行RMI调用。
import java.rmi.Naming;
import java.util.List;
public class GetService {
public static void main(String[] args) {
try {
StudentService studentService=(StudentService) Naming.lookup("rmi://127.0.0.1:5008/StudentService");
List<Student> list = studentService.getList();
for (Student s : list) {
System.out.println("姓名:"+s.getName()+",年龄:"+s.getAge());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
6.控制台显示结果
=============控制台============
姓名:张三,年龄:15
姓名:李四,年龄:20
===============================
在Spring中配置Rmi服务
将Rmi和Spring结合起来用的话,比上面实现Rmi服务要方便的多。
1.首先我们定义接口,此时定义的接口不需要继承其他接口,只是一个普通的接口
package service;
import java.util.List;
public interface StudentService {
List<Student> getList();
}
2.定义一个类,实现这个接口,这个类也只需实现步骤一定义的接口,不需要额外的操作
package service;
import java.util.ArrayList;
import java.util.List;
public class StudentServiceImpl implements StudentService {
public List<Student> getList() {
List<Student> list=new ArrayList<Student>();
Student s1=new Student();
s1.setName("张三");
s1.setAge(15);
Student s2=new Student();
s2.setName("李四");
s2.setAge(20);
list.add(s1);
list.add(s2);
return list;
}
}
3.接一下来在applicationContext.xml配置需要的信息
a.首先定义服务bean
<bean id="studentService" class="service.StudentServiceImpl"></bean>
b.定义导出服务
<bean class="org.springframework.remoting.rmi.RmiServiceExporter"
p:service-ref="studentService"
p:serviceInterface="service.StudentService"
p:serviceName="StudentService"
p:registryPort="5008"
/>
也可以增加p:registryHost属性设置主机
c.在客户端的applicationContext.xml中定义得到服务的bean(这里的例子是把导出服务bean和客户端的bean放在一个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.配置的东西就这么多,是不是比上面的现实要方便的多呀!现在我们来测试一下
package service;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService=(StudentService) ctx.getBean("getStudentService");
List<Student> list = studentService.getList();
for (Student s : list) {
System.out.println("姓名:"+s.getName()+",年龄:"+s.getAge());
}
}
}
=============控制台============
姓名:张三,年龄:15
姓名:李四,年龄:20
=============================
上面的mian方法运行可能会报错,应该是spring的jar少了,自己注意添加。
第一次写博客,有不对的地方请多多指出。