コード例 #1
0
 public void registerAndPush(int configId, int envId) {
   Config config = getConfig(configId);
   if (config == null) {
     throw new EntityNotFoundException("config[id=" + configId + "] not found, maybe deleted.");
   }
   ConfigInstance defaultInst = findDefaultInstance(configId, envId);
   if (defaultInst == null) {
     throw new RuntimeBusinessException("该环境下配置项不存在!");
   }
   try {
     ConfigRegisterService registerService = getRegisterService(envId);
     // register context value需要放在register default value前面,重要的操作在后面,确保zk与db数据一致,大部分配置无context value
     // 第一个注册操作就需要push,第二个不需要,确保第一个操作时客户端感知到变化时就已经感知到了push事务
     registerService.registerAndPushContextValue(
         config.getKey(), getConfigContextValue(configId, envId));
     registerService.registerDefaultValue(
         config.getKey(), resolveConfigFinalValue(defaultInst.getValue(), envId));
   } catch (RuntimeException e) {
     Environment environment = environmentService.findEnvByID(envId);
     logger.error(
         "Register and push config["
             + config.getKey()
             + "] to env["
             + (environment != null ? environment.getLabel() : envId)
             + "] failed.",
         e);
     throw e;
   }
 }
コード例 #2
0
 @Override
 public ConfigDeleteResult delete(int configId) {
   final ConfigDeleteResult result = new ConfigDeleteResult();
   final Config config = getConfig(configId);
   if (config != null) {
     result.setConfig(config);
     List<Environment> environments = environmentService.findAll();
     for (final Environment environment : environments) {
       try {
         this.transactionTemplate.execute(
             new TransactionCallbackWithoutResult() {
               @Override
               protected void doInTransactionWithoutResult(TransactionStatus status) {
                 deleteInstance(config, environment.getId());
               }
             });
       } catch (RuntimeException e) {
         logger.error(
             "Delete config["
                 + config.getKey()
                 + "] in environment["
                 + environment.getLabel()
                 + "] failed.",
             e);
         result.addFailedEnv(environment.getLabel());
         if (e instanceof ReferencedConfigForbidDeleteException) {
           result.setHasReference(true);
         }
       }
     }
     if (result.isSucceed()) {
       configDao.delete(configId);
       cacheClient.remove(ServiceConstants.CACHE_CONFIG_PREFIX + configId);
       cacheClient.remove(ServiceConstants.CACHE_CONFIG_PREFIX + config.getKey());
     }
   }
   return result;
 }