示例#1
0
  /**
   * go on loop : 1.ask the server for tasks 2. execute the tasks 3. sleep x time
   *
   * @param args
   * @throws Exception
   */
  public static void main(String[] args) {

    AgentServiceLogger logger = AgentServiceLogger.getInstance();
    logger.init();

    // get the configuration  of the agent service
    try {
      getConf();

      Communicate net = new Communicate();

      // install all models
      Parser parser = new Parser();
      ImplementorManager impManager = ImplementorManager.getInstance();

      // run for ever
      while (true) {

        // get and updates from the net
        String updates = net.getNewTasks();

        if (updates != null && !updates.equals("null")) {
          ArrayList<Message> messages = null;

          // parse the string message that get from the server
          try {
            messages = parser.parseMessage(updates);
          } catch (Exception e) {
            // error while parsing send acknowledgment to the server
            ACK ack = new ACK();
            ack.setOK(false);
            ack.setErrorMsg("problem to parse update string -" + updates);
            ack.setFullExceptionString(ExceptionHelper.getCustomStackTrace(e));
            ack.setTaskId("0");
            net.sendResponse(ack, conf);
          }

          // commit all the tasks
          if (messages != null) {
            for (Message msg : messages) {
              ACK retMsg = impManager.commitTask(msg);
              net.sendResponse(retMsg);
            }
          }
        } // end of if(updates!=null)

        // commit all task
        try {
          Thread.sleep(conf.getSleepTime() * 1000);
        } catch (InterruptedException e) {
        }
      } // end of while(true)

    } catch (AgentServiceException e1) {
      unCatchException(e1, conf);
    }
  }