All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fizzgate.dedicated_line.DedicatedLineInfoService Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
/*
 *  Copyright (C) 2020 the original author or authors.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see .
 */

package com.fizzgate.dedicated_line;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import org.springframework.stereotype.Service;

import com.fizzgate.config.AggregateRedisConfig;
import com.fizzgate.config.SystemConfig;
import com.fizzgate.util.JacksonUtils;
import com.fizzgate.util.Result;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hongqiaowei
 */

@ConditionalOnProperty(name = SystemConfig.FIZZ_DEDICATED_LINE_CLIENT_ENABLE, havingValue = "true")
@Service
public class DedicatedLineInfoService {

    private static final Logger log = LoggerFactory.getLogger(DedicatedLineInfoService.class);

    private Map serviceDedicatedLineInfoMap = new HashMap<>(32);

    @Resource(name = AggregateRedisConfig.AGGREGATE_REACTIVE_REDIS_TEMPLATE)
    private ReactiveStringRedisTemplate rt;

    @Value("${fizz.dedicated-line.client.enable:true}")
    private boolean fizzDedicatedLineClientEnable;

    @PostConstruct
    public void init() throws Throwable {
        if (fizzDedicatedLineClientEnable) {
            Result result = initDedicatedLineInfo();
            if (result.code == Result.FAIL) {
                throw new RuntimeException(result.msg, result.t);
            }
            result = lsnApiPairingInfoChange();
            if (result.code == Result.FAIL) {
                throw new RuntimeException(result.msg, result.t);
            }
        }
    }

    private Result initDedicatedLineInfo() {
        Result result = Result.succ();
        Flux> resources = rt.opsForHash().entries("fizz_dedicated_line_info");
        resources.collectList()
                 .defaultIfEmpty(Collections.emptyList())
                 .flatMap(
                         es -> {
                             if (!es.isEmpty()) {
                                 String json = null;
                                 try {
                                     for (Map.Entry e : es) {
                                         json = (String) e.getValue();
                                         DedicatedLineInfo info = JacksonUtils.readValue(json, DedicatedLineInfo.class);
                                         for (String service : info.services) {
                                             serviceDedicatedLineInfoMap.put(service, info);
                                         }
                                         log.info("init dedicated line info: {}", info);
                                     }
                                 } catch (Throwable t) {
                                     result.code = Result.FAIL;
                                     result.msg  = "init dedicated line info error, info: " + json;
                                     result.t    = t;
                                 }
                             } else {
                                 log.info("no dedicated line info");
                             }
                             return Mono.empty();
                         }
                 )
                 .onErrorReturn(
                         throwable -> {
                             result.code = Result.FAIL;
                             result.msg  = "init dedicated line info error";
                             result.t    = throwable;
                             return true;
                         },
                         result
                 )
                 .block();
        return result;
    }

    private Result lsnApiPairingInfoChange() {
        Result result = Result.succ();
        String channel = "fizz_dedicated_line_info_channel";
        rt.listenToChannel(channel)
          .doOnError(
                  t -> {
                      result.code = Result.FAIL;
                      result.msg  = "lsn error, channel: " + channel;
                      result.t    = t;
                      log.error("lsn channel {} error", channel, t);
                  }
          )
          .doOnSubscribe(
                  s -> {
                      log.info("success to lsn on {}", channel);
                  }
          )
          .doOnNext(
                  msg -> {
                      String message = msg.getMessage();
                      try {
                          DedicatedLineInfo info = JacksonUtils.readValue(message, DedicatedLineInfo.class);
                          if (info.isDeleted) {
                              for (String service : info.services) {
                                  serviceDedicatedLineInfoMap.remove(service);
                              }
                              log.info("remove dedicated line info: {}", info);
                          } else {
                              for (String service : info.services) {
                                  serviceDedicatedLineInfoMap.put(service, info);
                              }
                              log.info("update dedicated line info: {}", info);
                          }
                      } catch (Throwable t) {
                          log.error("update dedicated line info error, {}", message, t);
                      }
                  }
          )
          .subscribe();
        return result;
    }

    public DedicatedLineInfo get(String service) {
        return serviceDedicatedLineInfoMap.get(service);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy