导出内部对象 [英] Export inner object
问题描述
我尝试过这样的东西,但是当从服务器请求对象时,我收到一个错误:不能将com.sun.proxy的实例赋给$ Proxy1到ServiceObjImpl的类型ServiceObjImpl.barWrapper $ BarWrapper,例如ServiceObjImpl
服务对象界面:
public interface ServiceObject {
public int foo();
public void bar(int var)throws RemoteException;
}
服务器界面
public interface Service extends Remote {
public ServiceObject get()throws RemoteException;
public void updateValue(int var)throws RemoteException;
}
服务器实现
public class Server extends UnicastRemoteObject implements Service {
private static final long serialVersionUID = 247610230875286173L;
private ServiceObject serviceObj = null;
public Server()throws RemoteException {
super();
serviceObj = new ServiceObjImpl();
}
public void updateValue(int var){
try {
serviceObj.bar(var);
} catch(RemoteException e){
e.printStackTrace();
}
}
public ServiceObject get()throws RemoteException {
return serviceObj;
}
}
服务对象实现
class ServiceObjImpl实现ServiceObject,Serializable {
private static final long serialVersionUID = 1576043218876737380L;
private int data = 0;
private BarWrapper barWrapper = new BarWrapper();
private class BarWrapper extends UnicastRemoteObject implements Remote {
private static final long serialVersionUID = -5451253032897838145L;
public BarWrapper()throws RemoteException {
super();
}
public void bar(int value){
data = value;
}
}
public ServiceObjImpl()throws RemoteException {
super();
}
public int foo(){
return data;
}
public void bar(int value)throws RemoteException {
barWrapper.bar(value);
}
}
客户端实施
public class Client {
private Service server = null;
private ServiceObject obj = null;
public void get(){
try {
obj = server.get();
} catch(RemoteException e){
}
}
public void updateValue(int value){
try {
server.updateValue (值);
} catch(RemoteException e){
}
}
public void foo(){
System.out.println(obj.foo() );
}
}
code> BarWrapper 不实现远程以外的任何远程接口。但它是一个导出的远程对象,所以它作为一个存根传递,而不是它自己的类型。所以客户端上的类型必须是由存根执行的类型,而不是具体的类型 BarWrapper
这就是为什么你得到例外
解决方案:
- 为其定义一个新的远程接口
- 使其实现那个,和
- 从类型
BarWrapper
更改字段barWrapper
的声明到新的远程接口类型。
Lets say I have a server and a client. The client requests a service object from the server. That object is transmitted by copy but also provides a callback method (bar). If the client then asks the server to update that object (updateValue) the server will call bar() using RMI. I tried something like this, but i get an error when requsting the object from the server: "cannot assign instance of com.sun.proxy.$Proxy1 to field ServiceObjImpl.barWrapper of type ServiceObjImpl$BarWrapper in instance of ServiceObjImpl"
Service object interface:
public interface ServiceObject{
public int foo();
public void bar(int var) throws RemoteException;
}
Server interface
public interface Service extends Remote {
public ServiceObject get() throws RemoteException;
public void updateValue(int var) throws RemoteException;
}
Server implementation
public class Server extends UnicastRemoteObject implements Service {
private static final long serialVersionUID = 247610230875286173L;
private ServiceObject serviceObj = null;
public Server() throws RemoteException {
super();
serviceObj = new ServiceObjImpl();
}
public void updateValue(int var){
try {
serviceObj.bar(var);
} catch (RemoteException e) {
e.printStackTrace();
}
}
public ServiceObject get() throws RemoteException {
return serviceObj;
}
}
Service object implementation
class ServiceObjImpl implements ServiceObject, Serializable{
private static final long serialVersionUID = 1576043218876737380L;
private int data = 0;
private BarWrapper barWrapper = new BarWrapper();
private class BarWrapper extends UnicastRemoteObject implements Remote{
private static final long serialVersionUID = -5451253032897838145L;
public BarWrapper() throws RemoteException {
super();
}
public void bar(int value){
data = value;
}
}
public ServiceObjImpl() throws RemoteException {
super();
}
public int foo() {
return data;
}
public void bar(int value) throws RemoteException {
barWrapper.bar(value);
}
}
Client implementation
public class Client{
private Service server = null;
private ServiceObject obj = null;
public void get(){
try {
obj = server.get();
} catch (RemoteException e) {
}
}
public void updateValue(int value){
try {
server.updateValue(value);
} catch (RemoteException e) {
}
}
public void foo(){
System.out.println(obj.foo());
}
}
BarWrapper
doesn't implement any remote interfaces other than remote. But it is an exported remote object, so it is passed around as a stub, not as its own type. So the type at the client must be a type implemented by the stub, not the concrete type BarWrapper
this is why you get the exception
Solution:
- Define a new remote interface for it
- make it implement that, and
- change the declaration of the field
barWrapper
from typeBarWrapper
to the new remote interface type.
这篇关于导出内部对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!