Spring对远程服务的支持
Java程序有以下的远程调用技术选择:

远程过程调用(RPC)是同步的,客户端在服务器端返回结果之前将一直被阻塞。
各种技术适用的场景如下:

典型的RMI开发的过程如下:
- 定义一个接口,用于客户端和服务器端的交互,接口要继承Remote接口,所有方法都要抛出RemoteException。
- 编写服务器端的实现,实现第一步所编写的接口。
- 编写一个注册类,基于某个某个IP和端口(默认是1099)注册服务器端类的实现。
- 编写客户端的调用,基于IP,端口和注册的名称查找服务器端对应的类。
- RMI支持传递对象,要求对象实现Serializable接口。
下面是Spring对RMI的支持,配置也很简单:
一:服务器端
1. 要暴露的服务的接口:
package com.excellence.webservice;
import java.util.List;
public interface AccountService {
public void insertAccount(Account account);
public List getAccounts(String name);
}
2. 实现了该接口的类:
package com.excellence.webservice;
import java.util.List;
public class AccountServiceImpl implements AccountService {
public void insertAccount(Account account) {
System.out.println("inser!");
}
public List getAccounts(String name) {
System.out.println("get");
return null;
}
}
3. 在配置文件中公布改接口为RMI
<bean id="accountService" class="com.excellence.webservice.AccountServiceImpl" />
<bean name="service" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="AccountService" ></property>
<property name="service" ref="accountService"></property>
<property name="serviceInterface" value="com.excellence.webservice.AccountService"></property>
<property name="registryPort" value="1199"></property>
</bean>
4. 运行该RMI:
public class Demo {
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext ("classpath:applicationContext.xml");
RmiServiceExporter obj = (RmiServiceExporter)ctx.getBean("service");
}
}
二、客户端
1.在配置文件中进行配置:
<bean id="accClient" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1199/AccountService"></property>
<property name="serviceInterface" value="com.excellence.webservice.AccountService"></property>
</bean>
2.调用RMI的方法:
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext ("classpath:applicationContext.xml");
AccountService service = (AccountService)ctx.getBean("accClient");
service.insertAccount(new Account());
service.getAccounts("dd");
}
这里需要注意的是:
从普通RMI改到使用Spring集成RMI时,要将实现类的extends UnicastRemoteObject去掉,否则会报错:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'refreshService' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.rmi.server.ExportException: object already exported] 因为UnicastRemoteObject的作用就是生成stub object
Spring对远程服务的支持的更多相关文章
- java 利用spring JavaMailSenderImpl发送邮件,支持普通文本、附件、html、velocity模板
java 利用spring JavaMailSenderImpl发送邮件,支持普通文本.附件.html.velocity模板 博客分类: Java Spring 本文主要介绍利用JavaMailS ...
- Spring AOP和AspectJ支持
学了Spring之后发现我都不知道java为何物-- 在这一章中有好几节,讲的切面编程 第一节:在项目中启用Spring的AspectJ注解支持 第二节:用AspectJ注解声明aspect 第三节: ...
- Spring使用拦截器支持国际化(转)
Spring使用拦截器支持国际化很方便,使用时只需要两个步骤: 一.spring配置 具体配置方式如下: <!-- 资源文件绑定器,文件名称:messages.properties(没有找到时的 ...
- 1.Spring对JDBC整合支持
1.Spring对JDBC整合支持 Spring对DAO提供哪些支持 1)Spring对DAO异常提供统一处理 2)Spring对DAO编写提供支持的抽象类 3)提高编程效率,减少DAO编码量 Spr ...
- [Spring学习笔记 7 ] Spring中的数据库支持 RowMapper,JdbcDaoSupport 和 事务处理Transaction
1.Spring中的数据库支持 把具有相同功能的代码模板抽取到一个工具类中.2.关于jdbc template的应用 jdbcTemplate模板操作类,把访问jdbc的模板抽取到template中, ...
- Spring Boot 添加JSP支持【转】
Spring Boot 添加JSP支持 大体步骤: (1) 创建Maven web project: (2) 在pom.xml文件添加依赖: (3) ...
- 8 -- 深入使用Spring -- 8...1 Spring提供的DAO支持
8.8.1 Spring提供的DAO支持. DAO模式是一种标准的Java EE设计模式,DAO模式的核心思想是,所有的数据库访问都通过DAO组件完成,DAO组件封装了数据库的增.删.查.改等原子操作 ...
- 8 -- 深入使用Spring -- 2... Spring的“零配置”支持
8.2 Spring的“零配置”支持 Spring支持使用Annotation来代替XML配置文件.
- Java缓存学习之五:spring 对缓存的支持
(注意标题,Spring对缓存的支持 这里不单单指Ehcache ) 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache ...
随机推荐
- 计蒜客:百度的科学计算器(简单)【python神解】
题目链接:https://nanti.jisuanke.com/t/15504 题解:python大法好啊,三行代码无人能敌啊! 下面给出AC代码: b=input() a=input() print ...
- BZOJ 1316: 树上的询问 (点分治+set)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1316 因为只要求存在某条路径长度为K,所以点分,然后用set判断差值是否在set中就可以了. ...
- sublime text 3如何安装插件
原博客地址:http://blog.csdn.net/weixin_40682842/article/details/78727266 我自己的部分操作如下: 学习Sublime Text扩展插件的安 ...
- Spring框架学习笔记(9)——Spring对JDBC的支持
一.使用JdbcTemplate和JdbcDaoSupport 1.配置并连接数据库 ①创建项目并添加jar包,要比之前Spring项目多添加两个jar包c3p0-0.9.1.2.jar和mysql- ...
- Java应用开发中的字符集与字符编码
事出有因 在向HttpURLConnection的输出流写入内容时,因没有设置charset,导致接收方对数据的验签不一致. URL url = new URL(requestUrl); //打开连接 ...
- Linux学习之XShell与虚拟机的连接
最近在慕课网上学习Linux视频,记录一下解决问题的方法和过程. 实验软件环境: 虚拟机软件Vmware Workstation10.0.虚拟机系统CentOS 6.3(32位).XShell 5.0 ...
- TI-RTOS 之 PWM
TI-RTOS 之 PWM CC1310 有4个定时器,8个PWM通道,在TI-RTOS它的驱动是写好的,引用时需要包含 PWM.h头文件即可. 一般是任务主体之前,或者主函数进行初始化. Board ...
- [OpenCV][ARM9下移植OpenCV]
[OpenCV][ARM9下移植OpenCV] 安装环境 宿主机: Red Hat Enterprise Linux Server 6.3 开发板: mini2440 相关软件: cmake-3. ...
- 在Sql Server Intergration Service中设置Catalog下所部署所有项目的参数值
在Sql Server 2012开始,微软给SSIS添加了Project Model这种新的项目类型,与之对应的是在Sql Server数据库引擎中引入了Intergration Services C ...
- HDU 1068 Girls and Boys(模板——二分图最大匹配)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1068 Problem Description the second year of the univ ...