Ejemplo n.º 1
0
 static {
   try {
     ps = new Properties();
     ps.load(
         new InputStreamReader(
             FileUtil.openPropertiesInputStream("jobserver.properties"), FileUtil.fileEncode));
     job_name = ps.getProperty("jobserver.name");
     max_thread = Integer.parseInt(ps.getProperty("jobserver.max-thread"));
     jobServerType = ps.getProperty("jobserver.jobServerType");
     String sleepTimeStr = ps.getProperty("jobserver.sleepTime");
     if (StringUtils.isNotBlank(sleepTimeStr)) {
       sleepTime = Integer.parseInt(sleepTimeStr);
     }
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
   }
 }
Ejemplo n.º 2
0
 static {
   try {
     ps = new Properties();
     ps.load(
         new InputStreamReader(
             FileUtil.openPropertiesInputStream("rabbitmq.properties"), FileUtil.fileEncode));
     host = ps.getProperty("rabbitmq.host");
     exchange = ps.getProperty("jobserver.rabbitmq.exchange");
     username = ps.getProperty("rabbitmq.username");
     password = ps.getProperty("rabbitmq.password");
     securityKey = ps.getProperty("rabbitmq.securityKey");
     if (StringUtils.isEmpty(securityKey)) {
       securityKey = Constants.DEFAULT_SECURITY_KEY;
     }
     queue = ps.getProperty("jobserver.rabbitmq.queue");
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
   }
 }
Ejemplo n.º 3
0
  /**
   * @param args
   * @throws IOException
   * @throws FileNotFoundException
   * @throws UnsupportedEncodingException
   * @throws InterruptedException
   */
  public static void main(String[] args)
      throws UnsupportedEncodingException, FileNotFoundException, IOException,
          InterruptedException {
    Properties ps = new Properties();
    ps.load(
        new InputStreamReader(
            FileUtil.openPropertiesInputStream("log4j.properties"), FileUtil.fileEncode));
    PropertyConfigurator.configure(ps);

    // 创建线程池
    HSCloudEventHandler job = new HSCloudEventHandler(job_name, max_thread);

    // 从RabbitMQ接收消息并处理
    try {
      // 初始化RabbitMQ连接
      ConnectionFactory factory = new ConnectionFactory();
      factory.setHost(host);
      factory.setUsername(username);
      factory.setPassword(SecurityHelper.DecryptData(password, securityKey));
      Connection connection = factory.newConnection();
      Channel channel = connection.createChannel();
      channel.exchangeDeclare(exchange, "fanout", durable);
      DeclareOk ok = channel.queueDeclare(queue, durable, false, false, null);

      String queueName = ok.getQueue();
      channel.queueBind(queueName, exchange, "");
      channel.basicQos(1); // 消息分发处理
      QueueingConsumer consumer = new QueueingConsumer(channel);
      channel.basicConsume(queueName, false, consumer);
      logger.info(
          "Begin to receive RabbitMQ event: host["
              + host
              + "] exchange["
              + exchange
              + "] queue["
              + queueName
              + "] max-thread["
              + max_thread
              + "]");
      while (true) {
        Date now = new Date();
        if (expirationDate.before(now)) {
          logger.error("证书失效,系统终止服务。");
          System.exit(-1);
        }
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        if (FileUtil.stopProcess()) {
          logger.info("JobServer receive stop process event and will be end.....");
          break;
        }
        String message = new String(delivery.getBody());
        logger.debug(" [" + queue + "] Received '" + message + "'");
        // 处理创建虚拟机的消息,创建虚拟机需要分配ip,多以单独放在一个队列里面处理。
        if (jobServerType.equals("master")) {
          if (message.indexOf("createVM_") != -1) { // 根据消息内容判断消息类型
            try {
              // 对创建vm的消息进行处理,分配ip并填充到消息中
              message = handleMessage(message);
              // 创建线程去向openstack请求创建vm
              job.Handler(message);
            } catch (Exception e) {
              // 如果在分配ip过程中出现异常则捕获,把消息中的result:3,置换为result:4,error_info:错误信息
              logger.error(e.getMessage(), e);
              try {
                // 对消息填充和置换ip分配失败的信息
                message = saveGetIpExceptionLog(message, e.getMessage());
                // 即使ip分配失败仍然创建线程去向openstack请求创建vm
                job.Handler(message);
              } catch (Exception ex) {
                logger.error(ex.getMessage(), e);
              }
              channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
              continue;
            }
          }
          channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        } else if (jobServerType.equals("slaver")) { // 执行非创建vm的任务
          if (message.indexOf("createVM_") == -1) {
            job.Handler(message);
          }
          channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

        } else if (jobServerType.equals("all")) {
          try {
            message = handleMessage(message);
            job.Handler(message);
          } catch (Exception e) {
            logger.error(e.getMessage(), e);
            try {
              saveGetIpExceptionLog(message, e.getMessage());
              channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
              continue;
            } catch (Exception ex) {
              logger.error(ex.getMessage(), ex);
            }
          }
          channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
        TimeUnit.MILLISECONDS.sleep(sleepTime);
      }
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
    }
    // 退出线程池
    job.Shutdown();

    while (!job.IsTerminate()) {
      logger.info("Waiting for JobServer finished!");
    }

    logger.info("JobServer finished!");
    System.exit(0);
  }