inter12 commented on a change in pull request #1797: URL: https://github.com/apache/incubator-inlong/pull/1797#discussion_r750056197
########## File path: inlong-dataproxy/dataproxy-source/src/main/java/org/apache/inlong/dataproxy/config/holder/CacheClusterConfigHolder.java ########## @@ -0,0 +1,138 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.inlong.dataproxy.config.holder; + +import java.util.Date; +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; + +import org.apache.commons.lang.ClassUtils; +import org.apache.flume.Context; +import org.apache.flume.conf.Configurable; +import org.apache.inlong.dataproxy.config.loader.CacheClusterConfigLoader; +import org.apache.inlong.dataproxy.config.loader.ContextCacheClusterConfigLoader; +import org.apache.inlong.dataproxy.config.pojo.CacheClusterConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * CacheClusterConfigHolder + */ +public class CacheClusterConfigHolder implements Configurable { + + public static final Logger LOG = LoggerFactory.getLogger(CacheClusterConfigHolder.class); + + protected Context context; + private long reloadInterval; + private Timer reloadTimer; + private CacheClusterConfigLoader loader; + + private List<CacheClusterConfig> configList; + + /** + * configure + * + * @param context + */ + @Override + public void configure(Context context) { + this.context = context; + this.reloadInterval = context.getLong("reloadInterval", 60000L); + String loaderType = context.getString("cacheClusterConfig.type", + ContextCacheClusterConfigLoader.class.getName()); + try { + Class<?> loaderClass = ClassUtils.getClass(loaderType); + Object loaderObject = loaderClass.getDeclaredConstructor().newInstance(); + if (loaderObject instanceof CacheClusterConfigLoader) { + this.loader = (CacheClusterConfigLoader) loaderObject; + } + } catch (Throwable t) { + LOG.error("Fail to init loader,loaderType:{},error:{}", loaderType, t.getMessage()); + LOG.error(t.getMessage(), t); + } + if (this.loader == null) { + this.loader = new ContextCacheClusterConfigLoader(); + } + this.loader.configure(context); + } + + /** + * start + */ + public void start() { + try { + this.reload(); + this.setReloadTimer(); + } catch (Exception e) { + e.printStackTrace(); + LOG.error(e.getMessage(), e); + } + } + + /** + * close + */ + public void close() { + try { + this.reloadTimer.cancel(); + } catch (Exception e) { + LOG.error(e.getMessage(), e); + } + } + + /** + * setReloadTimer + */ + private void setReloadTimer() { + reloadTimer = new Timer(true); + TimerTask task = new TimerTask() { Review comment: TimerTask is not recommend to use . use ScheduledExecutorService instead -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
