提交 e19833fc 编写于 作者: T Tboy 提交者: qiaozhanwei

using spring context datasource (#1317)

上级 da319242
...@@ -25,6 +25,7 @@ import org.apache.dolphinscheduler.common.model.Server; ...@@ -25,6 +25,7 @@ import org.apache.dolphinscheduler.common.model.Server;
import org.apache.dolphinscheduler.dao.entity.MonitorRecord; import org.apache.dolphinscheduler.dao.entity.MonitorRecord;
import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.entity.ZookeeperRecord; import org.apache.dolphinscheduler.dao.entity.ZookeeperRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -38,6 +39,9 @@ import java.util.Map; ...@@ -38,6 +39,9 @@ import java.util.Map;
@Service @Service
public class MonitorService extends BaseService{ public class MonitorService extends BaseService{
@Autowired
private MonitorDBDao monitorDBDao;
/** /**
* query database state * query database state
* *
...@@ -47,7 +51,7 @@ public class MonitorService extends BaseService{ ...@@ -47,7 +51,7 @@ public class MonitorService extends BaseService{
public Map<String,Object> queryDatabaseState(User loginUser) { public Map<String,Object> queryDatabaseState(User loginUser) {
Map<String, Object> result = new HashMap<>(5); Map<String, Object> result = new HashMap<>(5);
List<MonitorRecord> monitorRecordList = MonitorDBDao.queryDatabaseState(); List<MonitorRecord> monitorRecordList = monitorDBDao.queryDatabaseState();
result.put(Constants.DATA_LIST, monitorRecordList); result.put(Constants.DATA_LIST, monitorRecordList);
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
......
...@@ -17,65 +17,47 @@ ...@@ -17,65 +17,47 @@
package org.apache.dolphinscheduler.dao; package org.apache.dolphinscheduler.dao;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSource;
import org.apache.dolphinscheduler.common.Constants; import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.dolphinscheduler.common.enums.DbType; import org.apache.dolphinscheduler.common.enums.DbType;
import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory;
import org.apache.dolphinscheduler.dao.entity.MonitorRecord; import org.apache.dolphinscheduler.dao.entity.MonitorRecord;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.dolphinscheduler.dao.utils.MysqlPerformance; import org.apache.dolphinscheduler.dao.utils.MysqlPerformance;
import org.apache.dolphinscheduler.dao.utils.PostgrePerformance; import org.apache.dolphinscheduler.dao.utils.PostgrePerformance;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.sql.*; import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/** /**
* database state dao * database state dao
*/ */
@Component
public class MonitorDBDao { public class MonitorDBDao {
private static Logger logger = LoggerFactory.getLogger(MonitorDBDao.class); private static Logger logger = LoggerFactory.getLogger(MonitorDBDao.class);
public static final String VARIABLE_NAME = "variable_name"; public static final String VARIABLE_NAME = "variable_name";
/** @Autowired
* load conf private DruidDataSource dataSource;
*/
private static Configuration conf;
static {
try {
conf = new PropertiesConfiguration(Constants.APPLICATION_PROPERTIES);
}catch (ConfigurationException e){
logger.error("load configuration excetpion",e);
System.exit(1);
}
}
/** /**
* get current db performance * get current db performance
* @return MonitorRecord * @return MonitorRecord
*/ */
public static MonitorRecord getCurrentDbPerformance(){ public MonitorRecord getCurrentDbPerformance(){
MonitorRecord monitorRecord = null; MonitorRecord monitorRecord = null;
Connection conn = null; Connection conn = null;
DruidDataSource dataSource = null;
try{ try{
dataSource = ConnectionFactory.getDataSource();
dataSource.setInitialSize(2);
dataSource.setMinIdle(2);
dataSource.setMaxActive(2);
conn = dataSource.getConnection(); conn = dataSource.getConnection();
if(conn == null){ String driverClassName = dataSource.getDriverClassName();
return monitorRecord; if(driverClassName.contains(DbType.MYSQL.toString().toLowerCase())){
}
if(conf.getString(Constants.SPRING_DATASOURCE_DRIVER_CLASS_NAME).contains(DbType.MYSQL.toString().toLowerCase())) {
return new MysqlPerformance().getMonitorRecord(conn); return new MysqlPerformance().getMonitorRecord(conn);
} else if(conf.getString(Constants.SPRING_DATASOURCE_DRIVER_CLASS_NAME).contains(DbType.POSTGRESQL.toString().toLowerCase())){ } else if(driverClassName.contains(DbType.POSTGRESQL.toString().toLowerCase())){
return new PostgrePerformance().getMonitorRecord(conn); return new PostgrePerformance().getMonitorRecord(conn);
} }
}catch (Exception e) { }catch (Exception e) {
...@@ -85,9 +67,6 @@ public class MonitorDBDao { ...@@ -85,9 +67,6 @@ public class MonitorDBDao {
if (conn != null) { if (conn != null) {
conn.close(); conn.close();
} }
if(dataSource != null){
dataSource.close();
}
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException ", e); logger.error("SQLException ", e);
} }
...@@ -99,7 +78,7 @@ public class MonitorDBDao { ...@@ -99,7 +78,7 @@ public class MonitorDBDao {
* query database state * query database state
* @return MonitorRecord list * @return MonitorRecord list
*/ */
public static List<MonitorRecord> queryDatabaseState() { public List<MonitorRecord> queryDatabaseState() {
List<MonitorRecord> list = new ArrayList<>(1); List<MonitorRecord> list = new ArrayList<>(1);
MonitorRecord monitorRecord = getCurrentDbPerformance(); MonitorRecord monitorRecord = getCurrentDbPerformance();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册