传统的实现RMI,需要
1.服务接口必须从Remote派生,每个方法抛出RemoteException
2.实现类必须从UnicastRemoteObject派生
3.所有方法的参数和返回值,必须是基本类型,或者实现了Serializable接口
public class User implements Serializable {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
public interface RmiUserService extends Remote {
User login(String username, String password) throws RemoteException;
void create(String username, String password) throws RemoteException;
}
public class RmiUserServiceImpl extends UnicastRemoteObject implements RmiUserService {
protected RmiUserServiceImpl() throws RemoteException {
}
private Map<String, String> users = new HashMap<String, String>();
public void create(String username, String password) {
if (username == null || password == null)
throw new IllegalArgumentException("Invalid args.");
if (users.get(username) != null)
throw new RuntimeException("User exist!");
users.put(username, password);
}
public User login(String username, String password) {
if (username == null || password == null)
throw new IllegalArgumentException("Invalid args.");
if (password.equals(users.get(username)))
return new User(username, password);
throw new RuntimeException("Login failed.");
}
public static void main(String[] args) throws RemoteException, MalformedURLException, AlreadyBoundException {
LocateRegistry.createRegistry(1099);
Naming.bind("rmi://localhost:1099/UserService", new RmiUserServiceImpl());
}
}
public class Client {
public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException {
RmiUserService service = (RmiUserService) Naming.lookup("rmi://localhost:1099/UserService");
service.create("xace", "1");
System.out.println(service.login("xace", "1"));
}
}
调用:
>rmic RmiUserServiceImpl
>java Client
Spring对RMI提供的支持
不用写一行代码,直接在Spring的配置文件中声明,就可以将一个传统的java类作为RMI服务输出
<bean id="userService" class="example.rmi.UserServiceImpl" />
<bean id="rmiService" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="UserService"/>
<property name="service" ref="userService"/>
<property name="serviceInterface" value="example.rmi.UserService"/>
<property name="registryPort" value="1099"/>
</bean>
唯一编写的代码是main()方法,启动Spring容器
public static void main(String[] args) {
new ClassPathXmlApplicationContext("config.xml");
}
客户端代码
public class Client {
public static void main(String[] args) throws Exception {
RmiProxyFactoryBean factory = new RmiProxyFactoryBean();
factory.setServiceInterface(UserService.class);
factory.setServiceUrl("rmi://localhost:1099/UserService");
factory.afterPropertiesSet();
UserService service = (UserService) factory.getObject();
service.create("test", "password");
System.out.println(service.login("test", "password"));
try {
service.login("test", "bad-password");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
如果客户端也在Spring容器中启动,完全可以在XML配置文件中定义UserService并直接使用
<bean id="userServiceRmi" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1099/UserService" />
<property name="serviceInterface" value="example.rmi.UserService" />
</bean>
RMI虽然是Java标准的远程调用模式,但是它使用特定的Java Remote Method Protocol二进制协议,很难穿透防火墙,如果要跨防火墙,应该使用HTTP协议为基础的远程调用。
Hessian; Burlap; Spring HTTP Invoker。使用私有协议的Http远程调用没有成为标准,实际应用较少,只能用于Java应用程序之间的远程调用。如果希望和异构平台实现远程调用,就必须使用标准的Web服务(Web Services)
分享到:
相关推荐
Spring-RMI (RMI调用, HTTP调用) 本人测试过了
一个基于Spring的rmi实现,测试成功,可以远程调用。
1. 远程调用RMI(Remote Method Invocation): 通过使用 RmiProxyFactoryBean 和 RmiServiceExporter,并且,Spring支持两个传统的RMI(使用 java.rmi.Remote接口和java.rmi.RemoteException)和通过RMI调用器实现的...
spring RMI 远程接口调用 包含服务端客户端程序,可完整运行
Spring整合RMI的使用,实现不同项目间方法的远程调用。
spring rmi 多接口服务端配置 调用多接口客户端配置
spring rmi 客户端 服务器 调用
spring-rmi Spring RMI调用方法,本人测试过了
spring rmi 远程调用服务端和客户端源码,eclipse环境,jdk1.7.
三种方式实现java远程调用(rmi) 方式一:原始方式 方式二:spring 方式三:jndi 解压,放到myeclipse上可用
上文我们利用Spring rmi实现了Spring的远程访问(Spring 实现远程访问详解——rmi),本文主要讲解利用HttpInvoke实现远程访问。 Spring httpInvoker使用标准java序列化机制,通过Http暴露业务服务。如果你的参数和...
RMI远程调用,之前做的Demo希望对大家有所帮助!
由于spring2和spring3的rmi方式调用方式不同引起的,通过查阅相关文档后发现,spring3不在需要生成skeleton和stub了,所以把这个类从spring-context中删除了,解决办法就是想办法将它再加进来
主要介绍了Java的Spring框架下RMI与quartz的调用方法,Spring是Java的SSH三大web开发框架之一,需要的朋友可以参考下
(9) spring-remoting.jar 这个jar文件包含支持EJB、JMS、远程调用Remoting(RMI、Hessian、Burlap、Http Invoker、JAX-RPC)方面的类。 (10) spring-support.jar 这个jar文件包含支持缓存Cache(ehcache)、JCA、...
Spring为编写企业应用程序提供了轻量的解决方案,同时仍然支持使用声明式事务、 用RMI或web service远程调用、以及使用多种方式来将数据持久化到数据库。Spring提供了全功能的 MVC framework, 以及透明集成AOP到你...
word源码java fleet-spring-boot ...RMI 远程调用框架 :Spring Boot Hessian 远程调用框架 :Spring Boot Forest Http 请求 :Spring Boot HTTP 访问控制 :Spring Boot GraphQL 构建 API :Spring Boot
这个jar文件包含支持EJB、JMS、远程调用Remoting(RMI、Hessian、Burlap、Http Invoker、JAX-RPC)方面的类。 (10) spring-support.jar 这个jar文件包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java ...
spring 2.0中文参考手册 ,有spring的详细介绍包括很实用的定时程序和RMI远程调用程序。
这个jar文件包含支持EJB、JMS、远程调用Remoting(RMI、Hessian、Burlap、Http Invoker、JAX-RPC)方面的类。 (10) spring-support.jar 这个jar文件包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java ...