Zeroc Ice返回值类型对象的实现(四)

2014-11-24 01:39:58 · 作者: · 浏览: 7

*
* @return 本类的代理
*/
private MainOperatorPrx getOwnPrx() throws UGenericException {

// 代理为空时,自动获取代理。
if (this.mainOperatorPrx == null) {
// 环境为空时,初始化环境
if (this.ic == null) {
// 1, 初始化环境
ic = Ice.Util.initialize();
}
// 2, 创建代理基类对象
String str = "MainOperatorUID:default -h 127.0.0.1 -p 9999";

ObjectPrx objPrx = this.ic.stringToProxy(str);
// 3, 获取代理
this.mainOperatorPrx = MainOperatorPrxHelper.checkedCast(objPrx);

// 4, 测试是否可用,不可用时抛出异常。
if (this.mainOperatorPrx == null) {
throw new UGenericException(str + ", request proxy faild.");
}
}
return this.mainOperatorPrx;
}
// =========以<上>为私有方法,提供ICE支撑。=========
}


(ii)为客户端写个手工测试类

Java代码
/*
* file: StartAllClient.java
*/
package com.number.start;

import java.io.Serializable;
import com.number.bean.Bond;
import com.number.operator.MainOperatorClient;

/**
* 启动使用者
* @author zhnb
*
*/
public class StartAllClient implements Serializable {

private static final long serialVersionUID = -6282697303788648813L;

public static void main(String[] args) {

MainOperatorClient moc = new MainOperatorClient();
Bond bond = moc.getBean("something");

StringBuffer info = new StringBuffer();
if (bond == null) {
info.append("null");
} else {
info.append("Bond@" + bond.hashCode() + ":");

info.append("bName=" + bond.bName);
info.append(",bCode=" + bond.bCode);

info.append(":");
info.append("bName=" + bond.getbName());
info.append(",bCode=" + bond.getbCode());
}

System.out.println(info.toString());
System.exit(0);
}

}


OK,看样子写完了,可以跑了吧。试个……(提交一下,我去瞅个行号~。=)

念叨着,“先启服务run 'MainOperatorServer'……再启客户run 'StartAllClient'”……

哦~&*……*出错了!

Java代码
Exception in thread "main" Ice.NoObjectFactoryException
reason = ""
type = "::com::number::bean::Bond"
at IceInternal.BasicStream.readObject(BasicStream.java:1444)


Why ! 不是一直这么个写法嘛?!

——如果是这么个写法,我也就不花功夫写这篇文章了。

二、机制的简要说明

返回值有两种方式,一种是Ice最喜欢(也是最推荐的)“引用”方式,另一种是“传值”方式。在ICE中的含意如下:

“引用”,即客户端不会拿到类型实体的副本,只拿到一个代理,可以抽象成一个远程指针(C系)或者一个对象引用(J系)。
“传值”,跟“引用”相对,即拿到类型实体的副本。
(此处略去二者特点,即使用范围,约一千字。)

因此传接口的时候,就类似于“远程过程调用”的感觉,属于“行为”性。可抽象成一系列的接口,实现C-S间的规范协议。而传值时,有“序列反序列”的味道,属于“实体”性。需要传方有个打包成序列的模板,收方有个解包成对象的模板。回观上文报错,释然了。

三、一个Exception的解决

一个Exception指的是“NoObjectFactoryException”,无对象工厂异常。当客户方拿到一箱Bond的零件后,他找不到工厂给的对象装配图。傻眼了的意思。

人工建图。没有拿到模型,但是知道有个“Bond.java”抽象的不能使,那就直接实现一个最基础的吧。造个BondI当临时模板使着吧,先!

Java代码
/*
* file: BondI.java
*/
package com.number.bond;

import java.io.Serializable;
import Ice.Current;
import com.number.bean.Bond;

/**
* 自定义债券Bean(LC, 本地类)
* @author zhnb
*
*/
public class BondI extends Bond implements Serializable {

private static final long serialVersionUID = 8758902536680272427L;

// Methods
@Override
public String getbCode(Current current) {
return this.bCode;
}

@