use of com.ds.retl.exception.UserInterfaceErrorException in project main by JohnPeng739.
the class ServerManageServiceImpl method serviceRest.
/**
* {@inheritDoc}
*
* @see ServerManageService#serviceRest(String, String, String)
*/
@Override
public boolean serviceRest(String cmd, String service, String machineIp) throws UserInterfaceErrorException {
if (StringUtils.isBlank(cmd) || StringUtils.isBlank(service) || StringUtils.isBlank(machineIp)) {
throw new UserInterfaceErrorException(UserInterfaceErrors.SYSTEM_ILLEGAL_PARAM);
}
RestClientInvoke invoke = new RestClientInvoke();
try {
JSONObject serviceConfig = getServiceConfig(machineIp, service);
int restPort = env.getProperty("restful.port", Integer.class, 9999);
String userCode = sessionDataStore.getCurrentUserCode();
JSONObject json = invoke.post(String.format("http://%s:%d/rest/server/service/local?cmd=%s&service=%s&userCode=%s", machineIp, restPort, cmd, service, userCode), serviceConfig, JSONObject.class);
return json.getBooleanValue("data");
} catch (RestInvokeException ex) {
if (logger.isErrorEnabled()) {
logger.error(ex);
}
throw new UserInterfaceErrorException(UserInterfaceErrors.SERVICE_STATUS_FAIL);
} finally {
invoke.close();
}
}
use of com.ds.retl.exception.UserInterfaceErrorException in project main by JohnPeng739.
the class StormRestfulServiceClientImpl method invokePost.
/**
* 使用Jersey发起一次POST操作
*
* @param url 业务地址
* @param content POST的内容
* @param mediaType POST内容的类型
* @param <T> POST内容的泛型定义
* @return POST返回的数据,JSON对象
* @throws UserInterfaceErrorException 调用过程中发生的异常
*/
private <T> JSONObject invokePost(String url, T content, MediaType mediaType) throws UserInterfaceErrorException {
String uri = prepareUri(url);
WebTarget target = this.client.target(uri);
Entity entity = null;
if (content != null) {
entity = Entity.entity(content, mediaType);
}
Response response = target.request().post(entity);
String jsonString = response.readEntity(String.class);
response.close();
JSONObject json = JSON.parseObject(jsonString);
if (response.getStatus() == 500) {
throw new UserInterfaceErrorException(json.getString("error"));
} else {
return json;
}
}
use of com.ds.retl.exception.UserInterfaceErrorException in project main by JohnPeng739.
the class TopologyManageServiceImpl method prepareTopologyConfigJsonObject.
/**
* 从拓扑的配置信息字符串中创建一个JSON格式的拓扑配置对象
*
* @param confStr 拓扑配置信息字符串
* @return 创建好的拓扑配置对象
* @throws UserInterfaceErrorException 创建过程中发生的异常
*/
private JSONObject prepareTopologyConfigJsonObject(String confStr) throws UserInterfaceErrorException {
JSONObject conf = JSON.parseObject(confStr);
if (conf == null) {
return null;
}
JSONObject topologyConf = loadTemplateJson("/template/topology-template.json");
if (topologyConf == null) {
if (logger.isErrorEnabled()) {
logger.error("The inline template config file not found in system.");
}
return null;
}
// 基础信息
topologyConf.put("name", conf.getString("name"));
topologyConf.put("type", conf.getString("type"));
topologyConf.put("cluster", true);
topologyConf.put("debug", conf.getBooleanValue("debug"));
topologyConf.put("maxSpoutPending", 1);
// topologyConf.put("maxTaskParallelism", conf.getIntValue("maxTaskParallelism"));
topologyConf.put("messageTimeoutSecs", conf.getIntValue("messageTimeoutSecs"));
// topologyConf.put("numAckers", conf.getIntValue("numAckers"));
topologyConf.put("numAckers", 1);
// topologyConf.put("numWorkers", conf.getIntValue("numWorkers"));
// 处理配置的ZOOKEEPER服务器配置
JSONArray zookeepers = conf.getJSONArray("zookeepers");
JSONObject zookeeper = new JSONObject();
StringBuffer serverList = new StringBuffer();
for (int index = 0; index < zookeepers.size(); index++) {
serverList.append(zookeepers.getString(index));
serverList.append(",");
}
int length = serverList.length();
if (serverList.length() > 0) {
length--;
}
zookeeper.put("serverList", serverList.substring(0, length));
topologyConf.put("zookeepers", zookeeper);
// 数据源信息
topologyConf.put("jdbcDataSources", conf.getJSONArray("jdbcDataSources"));
JSONArray jmsDataSources = conf.getJSONArray("jmsDataSources");
// 缓存
JSONArray caches = conf.getJSONArray("caches");
JSONObject tarCaches = new JSONObject();
if (caches != null) {
for (int index = 0; index < caches.size(); index++) {
JSONObject src = caches.getJSONObject(index);
JSONObject tar = new JSONObject();
String columnName = src.getString("columnName"), type = src.getString("type");
tar.put("type", type);
if ("STATIC".equals(type)) {
String dataEnum = src.getString("dataEnum");
tar.put("dataEnum", dataEnum);
} else if ("JDBC".equals(type)) {
String dataSource = src.getString("dataSource");
String sql = src.getString("sql");
int intervalSec = src.getIntValue("intervalSec");
tar.put("dataSource", dataSource);
tar.put("sql", sql);
tar.put("intervalSec", intervalSec);
} else {
throw new UserInterfaceErrorException(UserInterfaceErrors.SYSTEM_ILLEGAL_PARAM);
}
tarCaches.put(columnName, tar);
}
}
topologyConf.put("caches", tarCaches);
// spouts信息
JSONArray spouts = conf.getJSONArray("spouts");
// 如果指定了JDBC的Spout,那么必须将并行度全部设置为1
boolean foundJdbcSpout = false;
int defaultParallelism = 0;
for (int index = 0; index < spouts.size(); index++) {
JSONObject spout = spouts.getJSONObject(index);
String type = spout.getString("type");
int parallelism = spout.getIntValue("parallelism");
defaultParallelism = Math.max(defaultParallelism, parallelism);
if ("jdbc".equalsIgnoreCase(type)) {
spout.put("parallelism", 1);
foundJdbcSpout = true;
break;
}
}
if (defaultParallelism <= 0) {
defaultParallelism = 1;
}
if (foundJdbcSpout) {
if (spouts.size() != 1) {
throw new UserInterfaceErrorException(UserInterfaceErrors.TOPOLOGY_CONF_JDBC_SPOUT);
}
// 发现配置的是JDBC类型的采集源,则只能有一个采集源,并且并行度必须为1
topologyConf.put("maxTaskParallelism", 1);
topologyConf.put("numWorkers", 1);
} else {
topologyConf.put("maxTaskParallelism", defaultParallelism);
topologyConf.put("numWorkers", defaultParallelism);
}
spouts = prepareSpouts(spouts, jmsDataSources);
topologyConf.put("spouts", spouts);
// bolts信息
JSONArray bolts = new JSONArray();
String type = conf.getString("type");
if ("retl".equals(type)) {
JSONObject structureBolt = createBolt("structure-bolt", "STRUCTURE", foundJdbcSpout ? 1 : defaultParallelism);
setGroups(structureBolt, spouts.toJavaList(JSONObject.class));
JSONObject validateBolt = createBolt("validate-bolt", "VALIDATE", foundJdbcSpout ? 1 : defaultParallelism);
setGroups(validateBolt, Arrays.asList(structureBolt));
JSONObject transformBolt = createBolt("transform-bolt", "TRANSFORM", foundJdbcSpout ? 1 : defaultParallelism);
setGroups(transformBolt, Arrays.asList(validateBolt));
JSONObject errorBolt = createBolt("error-bolt", "ERROR", foundJdbcSpout ? 1 : defaultParallelism);
setGroups(errorBolt, Arrays.asList(structureBolt, validateBolt, transformBolt), true);
bolts.addAll(Arrays.asList(structureBolt, validateBolt, transformBolt, errorBolt));
// 创建一个JmsBolt
JSONObject jmsBolt = createBolt("jms-bolt", "JMS", foundJdbcSpout ? 1 : defaultParallelism);
JSONObject jmsBoltConfig = new JSONObject();
JSONObject jmsDataSource = conf.getJSONArray("jmsDataSources").getJSONObject(0);
jmsBolt.put("method", jmsDataSource.getString("method"));
jmsBoltConfig.put("connection", String.format("%s%s?trace=%s", jmsDataSource.getString("protocol"), jmsDataSource.getString("server"), jmsDataSource.getBooleanValue("trace") ? "true" : "false"));
jmsBoltConfig.put("user", jmsDataSource.getString("user"));
jmsBoltConfig.put("password", jmsDataSource.getString("password"));
jmsBoltConfig.put("destinations", conf.getJSONArray("destinations"));
jmsBolt.put("configuration", jmsBoltConfig);
setGroups(jmsBolt, Arrays.asList(transformBolt, errorBolt));
bolts.add(jmsBolt);
} else if ("persist".equals(type)) {
// persist信息
JSONObject persist = conf.getJSONObject("persist");
if (persist == null) {
throw new UserInterfaceErrorException(UserInterfaceErrors.TOPOLOGY_NO_CONF_PERSIST);
}
JSONObject persistBolt = createBolt("persist-bolt", "JDBC", defaultParallelism);
persistBolt.put("configuration", persist);
setGroups(persistBolt, spouts.toJavaList(JSONObject.class));
bolts.add(persistBolt);
// 为关联的JDBC数据源添加错误持久化配置信息
String dataSourceName = persist.getString("dataSource");
JSONArray jdbcDataSources = topologyConf.getJSONArray("jdbcDataSources");
JSONObject errorTable = this.loadTemplateJson("/template/error-template.json");
for (int index = 0; index < jdbcDataSources.size(); index++) {
JSONObject dataSource = jdbcDataSources.getJSONObject(index);
if (dataSourceName.equals(dataSource.getString("name"))) {
dataSource.put("errorTable", errorTable);
}
}
topologyConf.put("jdbcDataSources", jdbcDataSources);
} else {
if (logger.isWarnEnabled()) {
logger.warn(String.format("Unsupported topology type[%s].", type));
}
throw new UserInterfaceErrorException(UserInterfaceErrors.SYSTEM_ILLEGAL_PARAM);
}
topologyConf.put("bolts", bolts);
// columns信息
JSONArray columns = conf.getJSONArray("columns");
if (columns != null) {
topologyConf.put("columns", columns);
}
// validates信息
JSONObject validates = conf.getJSONObject("validates");
if (validates != null) {
topologyConf.put("validates", validates);
}
// transforms信息
JSONObject transforms = conf.getJSONObject("transforms");
if (transforms != null) {
topologyConf.put("transforms", transforms);
}
return topologyConf;
}
use of com.ds.retl.exception.UserInterfaceErrorException in project main by JohnPeng739.
the class TopologyManageServiceImpl method validateZookeepers.
/**
* {@inheritDoc}
*
* @see TopologyManageService#validateZookeepers(String)
*/
@Override
public boolean validateZookeepers(String resourceJsonStr) throws UserInterfaceErrorException {
if (logger.isDebugEnabled()) {
logger.debug("Start validate the Zookeeper server ...");
}
List<String> servers = JSON.parseObject(resourceJsonStr, List.class);
try {
ZooKeeper zk = new ZooKeeper(StringUtils.merge(servers, ","), 40 * 1000, null);
zk.getState();
zk.close();
if (logger.isDebugEnabled()) {
logger.debug("Validate the Zookeeper server successfully.");
}
return true;
} catch (IOException | InterruptedException ex) {
if (logger.isErrorEnabled()) {
logger.error(ex);
}
throw new UserInterfaceErrorException(UserInterfaceErrors.TOPOLOGY_VALIDATE_FAIL);
}
}
use of com.ds.retl.exception.UserInterfaceErrorException in project main by JohnPeng739.
the class TopologyManageServiceImpl method validateJdbcDataSource.
/**
* {@inheritDoc}
*
* @see TopologyManageService#validateJdbcDataSource(String)
*/
@Override
public boolean validateJdbcDataSource(String resourceJsonStr) throws UserInterfaceErrorException {
if (logger.isDebugEnabled()) {
logger.debug("Start validate the JDBC server ...");
}
JSONObject json = JSON.parseObject(resourceJsonStr);
String driver = json.getString("driver"), url = json.getString("url"), user = json.getString("user"), password = json.getString("password");
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
conn.isClosed();
conn.close();
if (logger.isDebugEnabled()) {
logger.debug("Validate the JDBC server successfully.");
}
return true;
} catch (ClassNotFoundException | SQLException ex) {
if (logger.isErrorEnabled()) {
logger.error(ex);
}
throw new UserInterfaceErrorException(UserInterfaceErrors.TOPOLOGY_VALIDATE_FAIL);
}
}
Aggregations