Пример #1
0
  public static void main(String args[]) {

    String host = "localhost";
    int port = 1099;

    if (args.length > 0) {
      host = args[0];
      port = Integer.parseInt(args[1]);
    }

    // Initialise auctions from permanent storage
    BufferedReader br = null;
    BufferedWriter bw = null;
    try {
      AuctionServer rmiServer = new AuctionServer();
      Registry reg = LocateRegistry.getRegistry(host, port);
      reg.rebind("auctionServer", rmiServer);
      System.out.println("AuctionServer is ready");
      System.out.println(
          "Select one of the two options :\n1: To save live auctions\n2: To load test auctions that expire in a minute (note you need to start 5 clients first)  ");
      Scanner sc = new Scanner(System.in);
      bw = new BufferedWriter(new FileWriter("auctionsSaved.dat"));
      switch (sc.nextInt()) {
        case 1:
          for (AuctionItem item : rmiServer.liveAuctionItems.values()) {
            bw.write(item.toString() + "\n");
          }
          System.out.println("Saved auctions state.");
          break;
        case 2:
          GenerateAuctions generator = new GenerateAuctions();
          generator.generateList();
          br = new BufferedReader(new FileReader("auctionsBackup.dat"));
          String currentLine;
          while ((currentLine = br.readLine()) != null) {
            String[] array = currentLine.split(",");
            String name = array[0];
            double min_item_value = Double.parseDouble(array[1]);
            Date closing_date = new SimpleDateFormat("d/M/y H:mm:s").parse(array[2]);
            int client = Integer.parseInt(array[3]);
            rmiServer.registerAuctionItem(name, min_item_value, closing_date, client);
          }
          System.out.println("Initialised auctions...");
          break;
      }

    } catch (RemoteException e) {
      System.out.println("Exception in AuctionServer.main " + e);
    } catch (MalformedURLException ue) {
      System.out.println("MalformedURLException in RMIServerImp.main " + ue);
    } catch (IOException e) {
      System.out.println("Could not open file ");
      e.printStackTrace();
    } catch (ParseException e) {
      System.out.println("Unable to parse date");
      e.printStackTrace();
    } finally {
      if (br != null) {
        try {
          br.close();
        } catch (IOException e) {
          System.out.println("Unable to close file.");
        }
      }
      if (bw != null) {
        try {
          bw.close();
        } catch (IOException e) {
          System.out.println("Unable to close file.");
        }
      }
    }
  }