rmi spring
来源:互联网 发布:115网盘淘宝怎么搜 编辑:程序博客网 时间:2024/04/29 14:22
传统的实现RMI,需要
1.服务接口必须从Remote派生,每个方法抛出RemoteException
2.实现类必须从UnicastRemoteObject派生
3.所有方法的参数和返回值,必须是基本类型,或者实现了Serializable接口
Spring对RMI提供的支持
不用写一行代码,直接在Spring的配置文件中声明,就可以将一个传统的java类作为RMI服务输出
唯一编写的代码是main()方法,启动Spring容器
客户端代码
如果客户端也在Spring容器中启动,完全可以在XML配置文件中定义UserService并直接使用
RMI虽然是Java标准的远程调用模式,但是它使用特定的Java Remote Method Protocol二进制协议,很难穿透防火墙,如果要跨防火墙,应该使用HTTP协议为基础的远程调用。
Hessian; Burlap; Spring HTTP Invoker。使用私有协议的Http远程调用没有成为标准,实际应用较少,只能用于Java应用程序之间的远程调用。如果希望和异构平台实现远程调用,就必须使用标准的Web服务(Web Services)
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)
0 0
- spring RMI
- Spring rmi
- rmi spring
- Spring RMI
- spring rmi
- Spring RMI
- Spring - RMI
- spring rmi
- Spring RMI 支持
- Spring RMI 学习
- Spring配置RMI服务
- spring rmi 实现
- spring 对rmi支持
- Spring 实现RMI
- 远程调用 - spring+rmi
- spring rmi代理性能
- spring rmi 例子
- spring rmi 的使用
- Android 中 有趣味的GridView
- ios 学习笔记<二> 【经典】
- scu oj 4437: Carries (2015年四川省程序ACM设计竞赛B题目 )
- 精确监听 Android ListView 滑动到底部
- 从KPCR中获取内核基地址
- rmi spring
- programming-challenges Erdös Numbers (110206) 题解
- CSS带箭头的DIV方框,自适应高度
- 【SuperMap .Net 组件】投影坐标与大地坐标转换
- android.graphics.Camera实现图像的旋转、缩放
- rmi接口连接失败
- 串口中断----数组循环队列
- [每天读书半小时] 6-15
- ”互联网+ “,移动办公新思维