客户端与服务器传递对象:
在客户端与服务器之间可以传递的对象分两类,一类是Remote对象,另一类是Serializable对象,他们在处理过程中有些区别。
Remote对象,他传送到客户端的时候只是一个stub,客户端实际上是通过这个stub来操作服务器端的具体对象;而Serializable对象传
送到客户端是一个服务器端对象的拷贝,与服务器端对象没有了任何联系。
RMI还提供一种方法可以让服务器端在被访问的时候才动态启动。
1. 在服务器端实现类中继承Activatable,或是在构造函数中调用Activatable.exportObject(this, id, 0)
2. 然后实现构造函数:
public ...(ActivationID id, MarshalledObject data)
{
super(id, 0);
name = (String) data.get();
}
3. 定义activation group
Properties props = new Properties();
props.put("java.security.policy", "/server/server.policy");
ActivationGroupDesc group = new ActivationGroupDesc(props, null);
ActivationGroupID id = ActivationGroup.getSystem().registerGroup(group);
4. 定义参数
MarshalledObject param = new MarshalledObject("Blackwell Toaster");
5. 注册实现类
ActivationDesc desc = new ActivationDesc(id, "Distributed.ProductImpl", classURL, p1param);
6. 绑定对象
Product p = (Product) Activatable.register(desc);
namingContext.bind("toaster", p);
客户端程序不用做任何变化