简体   繁体   English

使用java RMI连接

[英]Connection using java RMI

to connect to the server using RMI I use the below code.. works perfectly. 使用RMI连接到服务器我使用下面的代码..完美的工作。

However as you can see below every time a button is pressed I have to re connect and keep doing so with every button.. is there a way to connect once and keep using it through out the class?? 然而,正如你在每次按下按钮时都可以看到的那样,我必须重新连接并继续按下每个按钮..有没有办法连接一次并继续在整个课程中使用它?

For example instead of keep writing that same code to connect again and again.. is there a way to do it once?.. so i can make the program more efficient. 例如,而不是继续写相同的代码来反复连接..有没有办法做一次?..所以我可以使程序更有效。 Thanks in advance. 提前致谢。

 if ( evt.getSource() == button1) 
    {
        try {           
            Remote kay = Naming.lookup("rmi://localhost/hey");
            Hey u = (Hey) kay;  
            u.canSaw(que);              
            } catch(Exception e) {
                System.out.println("fail");
            }

    }

    if ( evt.getSource() == button2) 
    {
        try {

            Remote kay = Naming.lookup("rmi://localhost/hey");
            Hey u = (Hey) kay;  
            u.canSaw(see);
            } catch(Exception e) {
                System.out.println("failed");
            }


    }   

RMI connections are implicit. RMI连接是隐含的。 You can't manage them yourself. 你自己无法管理它们。

But you don't have to keep looking up the same remote object. 但是您不必继续查找同一个远程对象。 Look it up once, and save the result. 查一次,保存结果。

You shouldn't be doing any of this on the event thread. 你不应该在事件线程上做任何这样的事情。

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

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