commandclient.java

来自「this is a trade sale system realized by 」· Java 代码 · 共 85 行

JAVA
85
字号
package trader.nw;
import java.io.*;
import java.net.*;
import trader.*;
import trader.db.*;

/** AddCommand and DeleteCommand UnitTest 2.
    Compile from classes directory
    javac -d . ../src/trader/nw/CommandClient.java

    In one terminal window start the CommandServer class
    java trader.nw.CommandServer

    In another terminal window start the CommandClient class
    java trader.nw.CommandClient
*/

class CommandClient {


  public static void main(String args[]) {
    String dbHost = "localhost";
    Command cmd;
    String id = "333_33_3333";

    Customer retCust;
    Object obj;

    System.out.println("CommandClient - AddCommand  " +
      "DelCommand Unit Test 2");
    try {
      if (args.length > 0) {
        dbHost = args[0];
      }
      NwClient client = new NwClient(dbHost, 6001);
      BrokerModel brokerModel = new BrokerModelDbImpl(dbHost);
      Thread.sleep(2000);
      while (true){
        try {
          System.out.println("Adding Customer " + id);
          Customer addCust = new Customer("333_33_3333",
            "Dough Nut", "Bakery Lane");
          Command addCmd = new AddCustomerCommand(addCust);
          client.send(addCmd);
          Thread.sleep(2000);
          cmd = (Command)client.receive();
          obj = cmd.getResult();
          Thread.sleep(5000);
        } catch(Exception e) {
          e.printStackTrace();
        }

        try {
          System.out.println("Getting Customer " + id);
          Command getCmd = new GetCustomerCommand(id);
          client.send(getCmd);
          Thread.sleep(2000);
          cmd = (Command)client.receive();
          System.out.println("Got Customer " +
            (Customer) cmd.getResult());
          Thread.sleep(5000);
        } catch(Exception e) {
          e.printStackTrace();
        }

        try {
          System.out.println("Deleting Customer " + id);
          Customer delCust = new Customer("333_33_3333",
            "Dough Nut", "Bakery Lane");
          Command delCmd = new DeleteCustomerCommand(delCust);
          client.send(delCmd);
          Thread.sleep(2000);
          cmd = (Command)client.receive();
          obj = cmd.getResult();
          Thread.sleep(5000);
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?