Exemplo n.º 1
0
  public void manual() {
    String ids = $V("IDs");
    String[] arr = ids.split("\\,");
    FullTextTaskManager ftm =
        (FullTextTaskManager) CronManager.getInstance().getCronTaskManager("IndexMaintenance");
    for (int i = 0; i < arr.length; i++) {
      long id = Long.parseLong(arr[i]);
      if (ftm.isRunning(id)) {
        this.response.setMessage("索引维护定时任务正在进行,请稍候重试!");
        return;
      }
      String file = Config.getContextRealPath() + "WEB-INF/data/index/" + id + "/time.lock";
      FileUtil.delete(file);
      ftm.execute(id);
      do
        try {
          Thread.sleep(100L);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      while (ftm.isRunning(id));
    }

    this.response.setMessage("生成索引成功");
  }
Exemplo n.º 2
0
  public boolean executeBatchJob(ZCDeployConfigSchema config, ZCDeployJobSet jobs) {
    if ((config == null) || (jobs.size() < 1)) {
      return false;
    }

    Transaction trans = new Transaction();
    String message = "";
    boolean connectFlag = true;

    String deployMethod = config.getMethod();
    if ("DIR".equals(deployMethod)) {
      for (int i = 0; i < jobs.size(); i++) {
        ZCDeployJobSchema job = jobs.get(i);

        ZCDeployLogSchema jobLog = new ZCDeployLogSchema();
        jobLog.setID(NoUtil.getMaxID("DeployLogID"));
        jobLog.setSiteID(job.getSiteID());
        jobLog.setJobID(job.getID());
        jobLog.setBeginTime(new Date());

        if (job.getStatus() == 3L) {
          job.setRetryCount(job.getRetryCount() + 1L);
        }

        String sourceFile = job.getSource();
        if (sourceFile.indexOf("template") != -1) {
          LogUtil.getLogger().info("模板文件" + sourceFile + "不复制,跳过");
          return true;
        }

        String target = job.getTarget();
        target = target.replace('\\', '/');
        String targetDir = target.substring(0, target.lastIndexOf("/"));
        File dir = new File(targetDir);
        if (!dir.exists()) {
          dir.mkdirs();
        }
        if (!targetDir.endsWith("/template")) {
          if ("delete".equalsIgnoreCase(job.getOperation())) {
            if (FileUtil.delete(target)) {
              message = "成功删除文件" + target;
              LogUtil.getLogger().info(message);
              job.setStatus(2L);
            } else {
              message = "失败:删除文件" + target;
              LogUtil.getLogger().info(message);
              job.setStatus(3L);
              Errorx.addError(message);
            }
          } else if (FileUtil.copy(sourceFile, target)) {
            message = "成功复制文件" + sourceFile + "到" + target;
            LogUtil.getLogger().info(message);
            job.setStatus(2L);
          } else {
            message = "失败:复制文件" + sourceFile + "到" + target;
            LogUtil.getLogger().info(message);
            job.setStatus(3L);
            Errorx.addError(message);
          }
        }

        jobLog.setMessage(message);
        jobLog.setEndTime(new Date());
        LogUtil.getLogger().info(message);

        trans.add(jobLog, OperateType.INSERT);
        trans.add(job, OperateType.UPDATE);
      }
    } else if ("FTP".equals(deployMethod)) {
      CommonFtp ftp = new CommonFtp();
      try {
        ftp.connect(
            config.getHost(), (int) config.getPort(), config.getUserName(), config.getPassword());
        connectFlag = true;
      } catch (IOException e1) {
        e1.printStackTrace();

        ZCDeployLogSchema jobLog = new ZCDeployLogSchema();
        jobLog.setID(NoUtil.getMaxID("DeployLogID"));
        jobLog.setSiteID(config.getSiteID());
        jobLog.setJobID(jobs.get(0).getID());
        jobLog.setBeginTime(new Date());
        jobLog.setEndTime(new Date());
        jobLog.setMessage(e1.getMessage());
        trans.add(jobLog, OperateType.INSERT);
        connectFlag = false;
      }
      if (connectFlag) {
        for (int i = 0; i < jobs.size(); i++) {
          ZCDeployJobSchema job = jobs.get(i);

          ZCDeployLogSchema jobLog = new ZCDeployLogSchema();
          jobLog.setID(NoUtil.getMaxID("DeployLogID"));
          jobLog.setSiteID(job.getSiteID());
          jobLog.setJobID(job.getID());
          jobLog.setBeginTime(new Date());

          if (job.getStatus() == 3L) {
            job.setRetryCount(job.getRetryCount() + 1L);
          }

          String target = job.getTarget();
          target = target.replace('\\', '/');
          if ("delete".equalsIgnoreCase(job.getOperation()))
            try {
              target = target.replaceAll("///", "/");
              if (ftp.delete(target)) {
                message = "FTP删除文件成功";
                job.setStatus(2L);
                jobLog.setMessage(message);
                jobLog.setEndTime(new Date());
                LogUtil.getLogger().info(message);

                trans.add(jobLog, OperateType.INSERT);
                trans.add(job, OperateType.UPDATE);
                break;
              }
              message = "FTP删除文件失败";
              job.setStatus(3L);
            } catch (Exception e) {
              job.setStatus(3L);
              message = e.getMessage();
              Errorx.addError(message);
            }
          else {
            try {
              String srcFile = job.getSource();
              srcFile = srcFile.replaceAll("///", "/");
              srcFile = srcFile.replaceAll("//", "/");
              String path = srcFile;
              ArrayList list = FileList.getAllFiles(path);
              if (list.size() == 0) {
                job.setStatus(3L);
                message = "文件不存在:" + path;
                Errorx.addError(message);
              } else {
                for (int j = 0; j < list.size(); j++) {
                  String name = (String) list.get(j);
                  if (name.indexOf("template") != -1) {
                    continue;
                  }
                  name = name.replace('\\', '/');
                  String targetName = name.replaceAll(path, "");
                  ftp.upload(name, target + targetName);
                }
                job.setStatus(2L);
                message = "FTP上传成功";
              }
            } catch (Exception e) {
              job.setStatus(3L);
              message = e.getMessage();
              Errorx.addError(message);
            }
          }
          label1106:
          jobLog.setMessage(message);
          jobLog.setEndTime(new Date());
          LogUtil.getLogger().info(message);

          trans.add(jobLog, OperateType.INSERT);
          trans.add(job, OperateType.UPDATE);
        }
      }
      ftp.disconnect();
    } else if ("SFTP".equals(deployMethod)) {
      SFtp sftp = new SFtp();
      try {
        sftp.connect(
            config.getHost(), (int) config.getPort(), config.getUserName(), config.getPassword());
        connectFlag = true;
      } catch (JSchException e1) {
        e1.printStackTrace();

        ZCDeployLogSchema jobLog = new ZCDeployLogSchema();
        jobLog.setID(NoUtil.getMaxID("DeployLogID"));
        jobLog.setSiteID(config.getSiteID());
        jobLog.setJobID(jobs.get(0).getID());
        jobLog.setBeginTime(new Date());
        jobLog.setEndTime(new Date());
        jobLog.setMessage(e1.getMessage());
        trans.add(jobLog, OperateType.INSERT);
        connectFlag = false;
      }
      if (connectFlag) {
        for (int i = 0; i < jobs.size(); i++) {
          ZCDeployJobSchema job = jobs.get(i);

          ZCDeployLogSchema jobLog = new ZCDeployLogSchema();
          jobLog.setID(NoUtil.getMaxID("DeployLogID"));
          jobLog.setSiteID(job.getSiteID());
          jobLog.setJobID(job.getID());
          jobLog.setBeginTime(new Date());

          if (job.getStatus() == 3L) {
            job.setRetryCount(job.getRetryCount() + 1L);
          }

          String target = job.getTarget();
          target = target.replace('\\', '/');
          if ("delete".equalsIgnoreCase(job.getOperation()))
            try {
              target = target.replaceAll("///", "/");
              if (sftp.delete(target)) {
                message = "SFTP删除文件成功";
                job.setStatus(2L);
                jobLog.setMessage(message);
                jobLog.setEndTime(new Date());
                LogUtil.getLogger().info(message);

                trans.add(jobLog, OperateType.INSERT);
                trans.add(job, OperateType.UPDATE);
              }
              message = "SFTP删除文件失败";
              job.setStatus(3L);
            } catch (Exception e) {
              job.setStatus(3L);
              message = e.getMessage();
              Errorx.addError(message);
            }
          else {
            try {
              String srcFile = job.getSource();
              srcFile = srcFile.replaceAll("///", "/");
              srcFile = srcFile.replaceAll("//", "/");
              String path = srcFile;
              ArrayList list = FileList.getAllFiles(path);
              if (list.size() == 0) {
                job.setStatus(3L);
                message = "文件不存在" + srcFile;
              } else {
                for (int j = 0; j < list.size(); j++) {
                  String name = (String) list.get(j);
                  if (name.indexOf("template") != -1) {
                    continue;
                  }
                  name = name.replace('\\', '/');
                  String targetName = name.replaceAll(path, "");
                  sftp.upload(name, target + targetName);
                }
                job.setStatus(2L);
                message = "SFTP上传成功";
              }
            } catch (Exception e) {
              job.setStatus(3L);
              message = e.getMessage();
              Errorx.addError(message);
            }
          }
          label1730:
          jobLog.setMessage(message);
          jobLog.setEndTime(new Date());
          LogUtil.getLogger().info(message);

          trans.add(jobLog, OperateType.INSERT);
          trans.add(job, OperateType.UPDATE);
        }
      }
      sftp.disconnect();
    }

    if (trans.commit()) {
      return true;
    }
    LogUtil.getLogger().info("添加部署任务时,数据库操作失败");
    Errorx.addError(message);
    return false;
  }