cacheConfig.xml中读取配置文件信息一2010-07-07

本文介绍了一个用于解析缓存配置文件的Java类CacheConfigReader。该类通过XML文件加载缓存服务器列表、默认Keyspace、默认ColumnFamily及DataBinding映射等配置信息。
package com.huawei.support.cache.impl;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
* 从cacheConfig.xml中读取配置文件.
*/
public final class CacheConfigReader
{
/**
* sLog
*/
private static Log sLog = LogFactory.getLog(CacheConfigReader.class);
/**
* 本实例
*/
private static CacheConfigReader sInstance;
/**
* 最终修改时间
*/
private static long sLastModified = -1;
/**
* 缓存服务器列表
*/
private static String[] sServerList;
/**
* 默认Keyspace
*/
private static String sDefaultKeyspace;
/**
* 默认ColumnFamily
*/
private static String sDefaultColumnFamily;
/**
* DataBinding Map
*/
private static Map<String, String> sDataBindingMap =
new HashMap<String, String>();

/** 默认构造函数.
* @param aConfigFile 配置文件名
*/
private CacheConfigReader(String aConfigFile)
{
try
{
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(aConfigFile);
XPathFactory pathFactory = XPathFactory.newInstance();
XPath xpath = pathFactory.newXPath();
// 解析缓存服务器列表.
sServerList = readServerList(xpath, doc);
// 解析默认Keyspace.
sDefaultKeyspace = readDefaultKeyspace(xpath, doc);
// 解析默认ColumnFamily
sDefaultColumnFamily = readDefaultColumnFamily(xpath, doc);
// 解析DataBindings.
sDataBindingMap = readDataBindingMap(xpath, doc);
}
catch (ParserConfigurationException e)
{
sLog.error(e);
}
catch (XPathExpressionException e)
{
sLog.error(e);
}
catch (IOException e)
{
sLog.error(e);
}
catch (SAXException e)
{
sLog.error(e);
}
}
package cn.com.chinatelecom.billing.gth.gather.job.config; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; /** * 配置加载器 - 支持JSON和YAML格式 */ @Slf4j public class ConfigLoader { private static final ObjectMapper jsonMapper = new ObjectMapper(); private static final ObjectMapper yamlMapper = new ObjectMapper(); /** * 从文件加载配置 */ public static StreamConfig loadFromFile(String filePath) { try { File configFile = new File(filePath); if (!configFile.exists()) { log.warn("Config file not found: {}", filePath); return createDefaultConfig(); } String content = FileUtils.readFileToString(configFile, StandardCharsets.UTF_8); // 根据文件扩展名选择解析器 if (filePath.endsWith(".yaml") || filePath.endsWith(".yml")) { return yamlMapper.readValue(content, StreamConfig.class); } else { return jsonMapper.readValue(content, StreamConfig.class); } } catch (IOException e) { log.error("Failed to load config from file: {}", filePath, e); return createDefaultConfig(); } } /** * 从资源文件加载配置 */ public static StreamConfig loadFromResource(String resourcePath) { try { ClassLoader classLoader = ConfigLoader.class.getClassLoader(); // 根据文件扩展名选择解析器 if (resourcePath.endsWith(".yaml") || resourcePath.endsWith(".yml")) { return yamlMapper.readValue(classLoader.getResourceAsStream(resourcePath), StreamConfig.class); } else { return jsonMapper.readValue(classLoader.getResourceAsStream(resourcePath), StreamConfig.class); } } catch (IOException e) { log.error("Failed to load config from resource: {}", resourcePath, e); return createDefaultConfig(); } } /** * 从字符串加载配置 */ public static StreamConfig loadFromString(String configContent, String format) { try { if ("yaml".equalsIgnoreCase(format) || "yml".equalsIgnoreCase(format)) { return yamlMapper.readValue(configContent, StreamConfig.class); } else { return jsonMapper.readValue(configContent, StreamConfig.class); } } catch (IOException e) { log.error("Failed to load config from string", e); return createDefaultConfig(); } } /** * 创建默认配置 */ private static StreamConfig createDefaultConfig() { log.info("Creating default stream processing configuration"); StreamConfig config = new StreamConfig(); // 默认输入配置 InputConfig inputConfig = new InputConfig(); inputConfig.setType("KAFKA"); KafkaConfig kafkaConfig = new KafkaConfig(); kafkaConfig.setBootstrapServers("localhost:9092"); kafkaConfig.setTopic("stream-events"); kafkaConfig.setGroupId("flink-stream-processor"); inputConfig.setKafka(kafkaConfig); config.setInputConfig(inputConfig); // 默认输出配置 OutputConfig outputConfig = new OutputConfig(); outputConfig.setType("FILE"); FileOutputConfig fileOutputConfig = new FileOutputConfig(); fileOutputConfig.setBasePath("/data/output"); fileOutputConfig.setFormat("json"); fileOutputConfig.setEncoding("UTF-8"); fileOutputConfig.setRolloverInterval(600L); outputConfig.setFile(fileOutputConfig); config.setOutputConfig(outputConfig); // 默认缓存配置 CacheConfig cacheConfig = new CacheConfig(); cacheConfig.setType("LOCAL"); config.setCacheConfig(cacheConfig); // 默认并行度配置 ParallelismConfig parallelismConfig = new ParallelismConfig(); parallelismConfig.setDefaultParallelism(1); config.setParallelismConfig(parallelismConfig); // 默认检查点配置 CheckpointConfig checkpointConfig = new CheckpointConfig(); checkpointConfig.setInterval(60000L); checkpointConfig.setTimeout(600000L); checkpointConfig.setMinPauseBetweenCheckpoints(500); checkpointConfig.setMaxConcurrentCheckpoints(1); config.setCheckpointConfig(checkpointConfig); return config; } /** * 验证配置 */ public static boolean validateConfig(StreamConfig config) { if (config == null) { log.error("Config is null"); return false; } if (config.getInputConfig() == null) { log.error("Input config is required"); return false; } if (config.getOutputConfig() == null) { log.error("Output config is required"); return false; } // 验证输入配置 InputConfig inputConfig = config.getInputConfig(); if (inputConfig.getType() == null) { log.error("Input type is required"); return false; } // 验证输出配置 OutputConfig outputConfig = config.getOutputConfig(); if (outputConfig.getType() == null) { log.error("Output type is required"); return false; } log.info("Config validation passed"); return true; } } 仔细阅读代码,完善配置文件加载功能,要求可以解析yaml文件
最新发布
12-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值