java rmi 参数传递_如何在RMI方法的参数中传递对象?

我正在尝试在RMI方法中添加参数。当我添加例如String一切正常。但是我不确定是否可以传递我创建的对象。我是RMI的新手,所以我的代码非常简单:

HelloIF

public interface HelloIF extends Remote {

String greeting(Context c) throws RemoteException;

}

你好

public class Hello extends UnicastRemoteObject implements HelloIF {

public Hello() throws RemoteException {

}

public String greeting(Context c) throws RemoteException {

addToContext(c);

report(c);

return "greeting";

}

void addToContext(Context c) {

c.addID(Thread.currentThread().getId());

}

void report(Context c) {

System.out.println("Hello.greeting() thread : "

+ Thread.currentThread().getName() + " "

+ Thread.currentThread().getId());

System.out.println("Hello.greeting() context : "

+ c.getDistributedThreadName() + " " + c.getRequestType());

}

}

RMI服务器

public class RMIServer {

public static void main(String[] args) throws RemoteException, MalformedURLException {

LocateRegistry.createRegistry(1099);

HelloIF hello = new Hello();

Naming.rebind("server.Hello", hello);

System.out.println("server.RMI Server is ready.");

System.out.println("RMIServer.main() thread : " + Thread.currentThread().getName()

+ " " + Thread.currentThread().getId());

}

}

RMI客户端

public class RMIClient {

public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException {

Context context = new Context("request1", Thread.currentThread().getName()+System.currentTimeMillis());

Registry registry = LocateRegistry.getRegistry("localhost");

HelloIF hello = (HelloIF) registry.lookup("server.Hello");

System.out.println(hello.greeting(context));

System.out.println("RMIClient.mian() thread : " + Thread.currentThread().getName()

+ " " + Thread.currentThread().getId());

}

}

最后是我的课堂上下文

public class Context

{

private String requestType;

private String distributedThreadName;

private List IDList;

(...) getters/setters

}

我应该怎么做才能使传递上下文成为可能?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值