RMI应用程序中客户端和服务器之间的持续联系以预订机票

时间:2015-10-20 20:14:23

标签: java interface server client rmi

我一直在尝试创建一个高级RMI应用程序,用户可以在该应用程序中预订航空公司航班的机票。我有机票预订系统,但它似乎不是完美的代码,我觉得可能有一个更简单的方法。

这是我的界面:

public interface BookingIF extends Remote {

    int bookTicket(int destination) throws RemoteException;
    void checkAvailable(int journey) throws RemoteException;

}

我的服务器:

public class Server extends RemoteObject implements BookingIF {


int madrid = 3;
int london = 3;
int krakow = 3;

public static void main(String[] args) {
    try { 
        BookingIF obj = new Server();           
        BookingIF stub = (BookingIF) UnicastRemoteObject.exportObject( obj , 0);
        Registry registry = LocateRegistry.createRegistry(1098);
        registry.bind("RemoteObject", stub);

        System.out.println("Server running...");

        } 
    catch (Exception e) { 
        System.out.println("Server err: " + e.getMessage()); 
        e.printStackTrace(); 
    }
}

public int bookTicket(int journey) {
    if (journey > 3) {
        System.out.println("Error, press number between 1 and 3");
    } else {
        checkAvailable(journey);
    }

    return journey;
}

public void checkAvailable(int journey) {
    if(journey == 1) {
        if (madrid != 0) {
            madrid--;
            System.out.println("Journey to madrid booked");
            System.out.println("Number of tickets left for madrid : " + madrid);
        }
    }
    else if(journey == 2) {
        if (london != 0) {
            london--;
            System.out.println("Journey to london booked");
            System.out.println("Number of tickets left for london : " + london);
        }

    } 
    else if(journey == 3) {
        if (krakow != 0) {
            krakow--;
            System.out.println("Journey to krakow booked");
            System.out.println("Number of tickets left for krakow : " + krakow);
        }
    }

}

}

我的客户:

public class Client {

private Client() {

}

public static void main(String[] args) {
    int message; 
    BookingIF obj = null; 
    try { 
        Registry registry = LocateRegistry.getRegistry("localhost", 1098);
        obj = (BookingIF) registry.lookup("RemoteObject");
        System.out.println("Press 1 for Madrid");
        System.out.println("Press 2 for London");
        System.out.println("Press 3 for Krakow");
        int numEntered;
        Scanner scan = new Scanner(System.in);
        numEntered = scan.nextInt();
        message = obj.bookTicket(numEntered);
        System.out.println(message);
    } catch (Exception e) { 
        System.out.println("Client exception: " + e.getMessage()); 
        e.printStackTrace(); 
        }
    }
}

我遇到的问题是在预订机票后,我不能完全确定如何回到客户端以允许他们购买另一张机票。

我觉得应该更容易做到这一点,但我可以完全围绕RMI。有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

也许这个循环:

public static void main(String[] args) {
    int message; 
    BookingIF obj = null; 
    try { 
        Registry registry = LocateRegistry.getRegistry("localhost", 1098);
        obj = (BookingIF) registry.lookup("RemoteObject");

        while (true) {
            System.out.println("Press 1 for Madrid");
            System.out.println("Press 2 for London");
            System.out.println("Press 3 for Krakow");
            System.out.println("Press 9 to exit");

            int numEntered;
            Scanner scan = new Scanner(System.in);
            numEntered = scan.nextInt();

            if (numEntered == 9) {
                break;
            }
            message = obj.bookTicket(numEntered);
            System.out.println(message);
        }

    } catch (Exception e) { 
        System.out.println("Client exception: " + e.getMessage()); 
        e.printStackTrace(); 
        }
    }
}

答案 1 :(得分:0)

关于RMI部分的一些评论。

current.next

制作current UnicastRemoteObject。

public class Server extends RemoteObject

如果你扩展extends,你可以删除这一行,并将远程对象本身传递给下面的 implements BookingIF { int madrid = 3; int london = 3; int krakow = 3; public static void main(String[] args) { try { BookingIF obj = new Server(); BookingIF stub = (BookingIF) UnicastRemoteObject.exportObject( obj , 0); 调用而不是存根。

UnicastRemoteObject

bind()应该是静态的。否则它可以被垃圾收集,这将关闭整个程序。

        Registry registry = LocateRegistry.createRegistry(1098);

见上文。

registry

打印不好。它是服务器,而不是客户端。客户端不会看到这一点。输入的验证应该在客户端,而不是在这里。

        registry.bind("RemoteObject", stub);

见上文。该方法可能返回 System.out.println("Server running..."); } catch (Exception e) { System.out.println("Server err: " + e.getMessage()); e.printStackTrace(); } } public int bookTicket(int journey) { if (journey > 3) { System.out.println("Error, press number between 1 and 3"); 表示成功或失败。             }

在这里打印一些 } else { checkAvailable(journey); } return journey; } public void checkAvailable(int journey) { if(journey == 1) { if (madrid != 0) { madrid--; System.out.println("Journey to madrid booked"); System.out.println("Number of tickets left for madrid : " + madrid); 案例会很有用,表示没有票据。同样适用于此。

boolean