HeadFirst设计模式之RMI介绍
2016-03-11 14:16
316 查看
一、使用步骤
1.generate stubs and skeletons:Run rmic on the remote implementation class
如:D:\Workspaces\MyEclipse10\HeadFirstDesignPattern\bin>rmic headfirst.designpatterns.rmi.MyRemoteImpl
2.Bring up a terminal and start the rmiregistry
例如:D:\Workspaces\MyEclipse10\HeadFirstDesignPattern\bin>rmiregistry
3.start the service:Bring up another terminal and start your service
在MyEclipse中执行service的main方法
4.执行client的main方法
二、代码实现
1.
2.
3.
三、结果
1.generate stubs and skeletons:Run rmic on the remote implementation class
如:D:\Workspaces\MyEclipse10\HeadFirstDesignPattern\bin>rmic headfirst.designpatterns.rmi.MyRemoteImpl
2.Bring up a terminal and start the rmiregistry
例如:D:\Workspaces\MyEclipse10\HeadFirstDesignPattern\bin>rmiregistry
3.start the service:Bring up another terminal and start your service
在MyEclipse中执行service的main方法
4.执行client的main方法
二、代码实现
1.
package headfirst.designpatterns.rmi; import java.rmi.*; public interface MyRemote extends Remote { public String sayHello() throws RemoteException; }
2.
package headfirst.designpatterns.rmi; import java.rmi.*; import java.rmi.server.*; //Extending UnicastRemoteObject is the public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote { //Be sure arguments and return values are primitives or Serializable public String sayHello() { return "Server says, ‘Hey’"; } public MyRemoteImpl() throws RemoteException {} public static void main(String[] args) { try { MyRemote service = new MyRemoteImpl(); Naming.rebind("RemoteHello", service); } catch (Exception ex) { ex.printStackTrace(); } } }
3.
package headfirst.designpatterns.rmi; import java.rmi.*; public class MyRemoteClient { public static void main(String[] args) { new MyRemoteClient().go(); } public void go() { try { MyRemote service = (MyRemote) Naming.lookup("rmi://127.0.0.1/RemoteHello"); String s = service.sayHello(); System.out.println(s); }catch (Exception ex) { ex.printStackTrace(); } } }
三、结果
相关文章推荐
- 基于springAop的日志管理功能
- 10030---CSS圆角效果
- metrics和graphite监控
- Java的jdbc使用addBatch进行批处理操作的几种方式
- Chapter 11. Frame, MainWindow, and Toplevel Widgets 框架,主窗体,顶级部件
- Java工程师成神之路~
- sizeof
- Flume日志收集
- RAC 执行root.sh 报错 libcap.so.1: cannot open shared object file: No such file or directory
- LeetCode 3Sum Smaller
- sql 操作表增加字段
- Chapter 11. Frame, MainWindow, and Toplevel Widgets 框架,主窗体,顶级部件
- Chapter 11. Frame, MainWindow, and Toplevel Widgets 框架,主窗体,顶级部件
- OC字符串和数组
- 使用_CrtSetDbgFlag检测内存泄露
- Google DeepMind围棋程序AlphaGo的分析
- leetcode 113. Path Sum II
- curl模拟post请求提交
- dwz 多选删除
- 微信第三方登录Android注意事项