简体   繁体   English

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

[英]How to pass object in arguments in RMI method?

I am trying to add arguments in RMI method. 我试图在RMI方法中添加参数。 When I add eg String everything works fine. 当我添加例如String一切正常。 But I am not sure if I can pass an object I created. 但我不确定我是否可以传递我创建的对象。 I am new to RMI so my code is very simple: 我是RMI的新手,所以我的代码非常简单:

HelloIF HelloIF

public interface HelloIF extends Remote {
    String greeting(Context c) throws RemoteException;
}

Hello 你好

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());
    }
}

RMIServer 为RMIServer

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());
    }
}

RMIClient RMIClient

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());
    }
}

and finally my class Context 最后我的班级上下文

public class Context 
{
    private String requestType;
    private String distributedThreadName;
    private List<Long> IDList;

   (...) getters/setters
}

What should I do to make passing Context possible? 我该怎么做才能使传递上下文成为可能?

Your object should implement Serializable . 您的对象应该实现Serializable As I can see this would be one problem. 我可以看到这将是一个问题。 It is needed because the communication between both parts is done using serialization, so each object that needs to be sent to the other part, needs to be an instance of class implementing Serializable . 这是必需的,因为两个部分之间的通信是使用序列化完成的,因此需要发送到其他部分的每个对象都需要是实现Serializable的类的实例。

public class Context implements Serializable
{
    private String requestType;
    private String distributedThreadName;
    private List<Long> IDList;

   (...) getters/setters
}

and please add a serialVersionUID as a good practice. 请添加一个serialVersionUID作为一个好习惯。 Something like: 就像是:

private static final long serialVersionUID = 20120731125400L;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM