浏览 589 次
|
锁定老贴子 主题:rmi实现分页查询的求助
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2007-01-15 关键字: 求助
//分页代码:
public class abstract Page ...{
public Page(int count, Remote remote){
//...
this.count = count;
this.remote = remote;
}
public int count()...
public int pageSize()...
public int currentPage()...
public Object[] gotoPage(int index){
//is legal index
//...
return handle(start, end);
}
//子类覆盖实现不同查询条件
protected abstract Object[] handle(int start, int end);
//保存远程对象(实际返回时会替换成stub)
protected java.rmi.Remote remote;
private int count;
private int pageSize;
private int index;
}
//远程接口
public interface Service extends Remote{
Page querySomething();
}
//远程代理接口
public interface ServiceProxy extends Remote{
Object[] querySomething(int start, int end);
}
//远程对象实现
public class ServiceRemote extends UnicastRemoteObject implements Service, ServiceProxy{
public Page querySomething(){
int count = //...
return new Page(count, this){
protected Object[] handle(int start, int end){
ServiceProxy sp = (ServiceProxy)remote;
return sp.querySomething(start, end);
}
});
}
public Object[] querySomething(int start, int end){
//...
}
}
//客户端调用
public class Client{
public static void main(String args[]){
Service s = //get remote object
Page page = s.querySomething();//error! 错误信息在下面
int count = page.count();
//printf(page.gotoPage());
}
}
/*java.lang.ClassCastException: cannot assign instance of ServiceRemote_Stub
to field ServiceRemote$1.this$0 of type ServiceRemote in instance of ServiceRemote$1*/
//如果改成
public class UglyPage extends Page{
public UglyPage(int count, Remote remote){
super(count, remote);
}
protected Object[] handle(int start, int end){
ServiceProxy sp = (ServiceProxy)remote;
return sp.querySomething(start, end);
}
}
//然后服务端这样实现
//...
public Page querySomething(){
int count = //...
return new UglyPage(count, this);
}
//...
没有问题,但是失去了匿名类的简洁(主要是名字污染,因为可能有很多个不同query方法) 我的推断是匿名内部类时把Remote偷偷换成了final的属性,而服务端返回远程对象时都是返回相应的stub,于是替换时发生错误(final变量不可替换),不知道对不对。 其实匿名内部类相当于传递代码块参数,怎么在rmi中实现呢? 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2007-01-15
做了几下测试,只要涉及到非静态的内部类,不管匿名与否,都报相同错误。静态的则不管匿名与否都可以
|
|
| 返回顶楼 | |



