com.fizzgate.global_resource.GlobalResourceService Maven / Gradle / Ivy
/*
* 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.global_resource;
import org.noear.snack.ONode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import org.springframework.stereotype.Service;
import com.fizzgate.config.AggregateRedisConfig;
import com.fizzgate.fizz.input.PathMapping;
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
*/
@Service
public class GlobalResourceService {
private static final Logger log = LoggerFactory.getLogger(GlobalResourceService.class);
public static ONode resNode;
private Map resourceMap = new HashMap<>(64);
private Map objectMap = new HashMap<>(64);
@Resource(name = AggregateRedisConfig.AGGREGATE_REACTIVE_REDIS_TEMPLATE)
private ReactiveStringRedisTemplate rt;
@PostConstruct
public void init() throws Throwable {
Result> result = initGlobalResource();
if (result.code == Result.FAIL) {
throw new RuntimeException(result.msg, result.t);
}
result = lsnGlobalResourceChange();
if (result.code == Result.FAIL) {
throw new RuntimeException(result.msg, result.t);
}
updateResNode();
}
private void updateResNode() {
resNode = PathMapping.toONode(objectMap);
log.info("global resource node is updated, new keys: {}", objectMap.keySet());
}
private Result> initGlobalResource() {
Result> result = Result.succ();
Flux> resources = rt.opsForHash().entries("fizz_global_resource");
resources.collectList()
.defaultIfEmpty(Collections.emptyList())
.flatMap(
es -> {
if (!es.isEmpty()) {
String json = null;
try {
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy