发表于: 2017-05-18 23:20:26
1 969
今天完成的事:
上午,下午听老大讲代码生成工具,傍晚听群分享小课堂。
参考自:http://blog.csdn.net/zmx729618/article/details/52130722
参考教程将service与web进行分离我先用spring 封装rmi的方式但是出现如下异常,折腾了很久,没解决。
Connection refused to host: 127.0.0.1; nested exception
Connection refused: connect
后面改用Javarmi的方式进行分离测试成功了原代码如下
public class HelloServer {
public static void main(String args[]) {
try {
//创建一个远程对象
IHello rhello = new HelloImpl();
//生成远程对象注册表Registry的实例,并指定端口为8888(默认端口是1099)
LocateRegistry.createRegistry(8888);
//把远程对象注册到RMI注册服务器上,并命名为RHello
//绑定的URL标准格式为:rmi://host:port/name(协议名可以省略,下面两种写法都可以)
Naming.bind("rmi://127.0.0.1:8888/RHello", rhello);
System.out.println(">>INFO:远程IHello对象绑定成功!");
} catch (RemoteException e) {
System.out.println("创建远程对象发生异常!");
e.printStackTrace();
} catch (AlreadyBoundException e) {
System.out.println("发生重复绑定对象异常!");
e.printStackTrace();
} catch (MalformedURLException e) {
System.out.println("发生URL畸形异常!");
e.printStackTrace();
}
}
}
public class HelloImpl extends UnicastRemoteObject implements IHello{
private static final long serialVersionUID = 1961558474342609777L;
public HelloImpl()throws RemoteException {
super();
}
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
@Override
public int sum(int a, int b) {
return a+b;
}
}
public interface IHello extends Remote {
public String sayHello(String name) throws RemoteException;
public int sum(int a, int b)throws RemoteException;
}
public class HelloClient {
public static void main(String args[]) {
try {
// 在RMI服务注册表中查找名称为RHello的对象,并调用其上的方法
IHello rhello = (IHello) Naming.lookup("rmi://127.0.0.1:8888/RHello");
System.out.println(rhello.sayHello("chengkai88888"));
System.out.println(rhello.sum(454, 1000));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public interface IHello extends Remote {
public String sayHello(String name) throws RemoteException;
public int sum(int a, int b)throws RemoteException;
}
明天计划的事:明天一定要用spring将service与web分离成功
遇到的问题:spring封装rmi 连接异常,
收获:没感觉,
总结:好好学习
评论