java - 我在 java rmi 多客户端代码中遇到问题

标签 java rmi

我正在编写一个java rmi multi_client代码,其中第一个客户端(client1)有权修改或创建对象,并且后续客户端(client2)有权访问由client1创建的对象返回对象以查看它,但是它不工作 第一类客户

 import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;


public class ChefDeDepartement {

    static Scanner sc1  =  new Scanner(System.in);
    static Scanner sc2  =  new Scanner(System.in);
    static Scanner sc3  =  new Scanner(System.in);


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

        int i=0;
            try
            {
             //java.rmi.registry.LocateRegistry.createRegistry(1000);

             InterfaceChefDept c = (InterfaceChefDept)Naming.lookup("rmi://localhost/ImlementationInterfaceChefDept");

                System.out.println("connection au serveur");


                c.Ajouter("1ere avis");
                c.Ajouter("2eme avis");
                c.Ajouter("3eme avis");
                List<String> avis = c.returnArrayList();
                System.out.println("Bienvenu !!!!!!!!!!");


                while(i==0){
                    System.out.println(" ");
                    System.out.println(" ");
                    System.out.println(" ");


//affichage*************************************************************                    
                    System.out.println("La liste des avis :");

                    List<String> listAvis = avis;
                    int j=0;
                    int k=0;
                    for(String e: listAvis){
                        j=j+1;
                        System.out.println(j + " :" + e);

                    }

//affichage*************************************************************                    


                    System.out.println("*********************");
                    System.out.println("**********");
                    System.out.println("choisire votre Action :");
                    System.out.println("             1: Ajouter un avis \n");
                    System.out.println("             2: Supprimer \n");
                    int choice  = sc1.nextInt();
                    String newAvis;
                    int idAvis;

                    if (choice==1)

                        {
                            System.out.println("donner le nouvel avis : ");
                            newAvis = sc2.nextLine();
                            c.Ajouter(newAvis);
                            avis = c.returnArrayList();
/*                          
//affichage*************************************************************                    
                            System.out.println("La liste des avis :");

                            int l=0;
                            for(String f: avis){
                                l=l+1;
                                System.out.println(l + " :" + f);

                            }

//affichage*************************************************************

  */
                        }


                        if (choice==2)
                        {
                            System.out.println("donner l'indice de l'avis a supprimer ");
                            idAvis = sc3.nextInt();
                            System.out.println("scan done");
                            c.Supprimer(idAvis);
                            System.out.println("supp done ");
                            avis = c.returnArrayList();
/*                          
 //affichage*************************************************************                   
                            System.out.println("La liste des avis :");

                            int n=0;
                            for(String f: avis){
                                n=n+1;
                                System.out.println(n + " :" + f);

                            }

//affichage*************************************************************                            
*/                          
                        }
                }


            }

            catch(Exception e)
            {
                System.out.println(e);

            }
            }

}

第二个客户类别

 import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

public class Etudiant {

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

        try{
            int i=0;

            InterfaceChefDept c = (InterfaceChefDept) Naming.lookup("rmi://localhost/ImlementationInterfaceChefDept");
            InterfaceEtudiant s = (InterfaceEtudiant) Naming.lookup("rmi://localhost/ServerDesAvis");

            List<String> avis = c.returnArrayList();
            List<String> lavis = s.AfficherAvis(avis);

            System.out.println("La liste des avis disponibles :");

            for(String f: lavis){
                i=i+1;
                System.out.println(i + " :   " + f);

            }
            System.out.println(":) :) :) :) :) ");
        }
        catch(Exception e){
            e.printStackTrace();
        }

    }



}

服务器类

    import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.util.ArrayList;


public class ServerDesAvis {




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


        try
        {


            ImlementationInterfaceChefDept c = new ImlementationInterfaceChefDept();
            Naming.rebind("rmi://localhost/ServerDesAvis", c);

            ImplementationInterfaceEtudiant s = new ImplementationInterfaceEtudiant();
            Naming.rebind("rmi://localhost/ServerDesAvis", s);


            System.out.println("server is running");
            System.out.println(".......");
            System.out.println(".....");
            System.out.println("...");
        }

        catch(RemoteException re){
            re.printStackTrace();
        }

        catch(Exception e) {
                    System.out.println(e);
                }



    }

}

java ChefDeDepartement localhost之后 我收到这个错误 java.rmi.NotBoundException:ImlementationInterfaceChefDept

最佳答案

您的 ImlementationInterfaceChefDept RMI 服务器注册不正确。您将两者绑定(bind)到同一个 ServerDesAvis 端点:

ImlementationInterfaceChefDept c = new ImlementationInterfaceChefDept();
Naming.rebind("rmi://localhost/ServerDesAvis", c);

ImplementationInterfaceEtudiant s = new ImplementationInterfaceEtudiant();
Naming.rebind("rmi://localhost/ServerDesAvis", s);

...这意味着第一个 (ImlementationInterfaceChefDept) 将被第二个覆盖。您需要更换:

ImlementationInterfaceChefDept c = new ImlementationInterfaceChefDept();
Naming.rebind("rmi://localhost/ServerDesAvis", c);

...这样:

ImlementationInterfaceChefDept c = new ImlementationInterfaceChefDept();
Naming.rebind("rmi://localhost/ImlementationInterfaceChefDept", c);

希望这有帮助。

关于java - 我在 java rmi 多客户端代码中遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59603819/

相关文章:

java - 是否可以重构这些方法以避免代码重复?

Java:远程方法调用(RMI)

java - RMI理论,下载 stub 文件

java - RMI和CORBA的区别?

java - rmir​​egistry 或 JNDI 是否以序列化形式存储对象

java - 将 String 转换为 BigDecimal 而不丢失格式

Java StringBuilder.setLength() - 时间复杂度是 O(1) 吗?

java - 当我将参数传递给无法比较时,ProcessBuilder 不工作

java - 在 Firebase URL 中使用 Firebase 服务器时间戳

java - 我如何在 JBoss 中禁用 RMI?