Browse Source

using spring context datasource (#1317)

pull/2/head
Tboy 5 years ago committed by qiaozhanwei
parent
commit
e19833fca4
  1. 6
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/MonitorService.java
  2. 51
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/MonitorDBDao.java

6
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/MonitorService.java

@ -25,6 +25,7 @@ import org.apache.dolphinscheduler.common.model.Server;
import org.apache.dolphinscheduler.dao.entity.MonitorRecord;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.entity.ZookeeperRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
@ -38,6 +39,9 @@ import java.util.Map;
@Service
public class MonitorService extends BaseService{
@Autowired
private MonitorDBDao monitorDBDao;
/**
* query database state
*
@ -47,7 +51,7 @@ public class MonitorService extends BaseService{
public Map<String,Object> queryDatabaseState(User loginUser) {
Map<String, Object> result = new HashMap<>(5);
List<MonitorRecord> monitorRecordList = MonitorDBDao.queryDatabaseState();
List<MonitorRecord> monitorRecordList = monitorDBDao.queryDatabaseState();
result.put(Constants.DATA_LIST, monitorRecordList);
putMsg(result, Status.SUCCESS);

51
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/MonitorDBDao.java

@ -17,65 +17,47 @@
package org.apache.dolphinscheduler.dao;
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.dao.datasource.ConnectionFactory;
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.PostgrePerformance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* database state dao
*/
@Component
public class MonitorDBDao {
private static Logger logger = LoggerFactory.getLogger(MonitorDBDao.class);
public static final String VARIABLE_NAME = "variable_name";
/**
* load conf
*/
private static Configuration conf;
@Autowired
private DruidDataSource dataSource;
static {
try {
conf = new PropertiesConfiguration(Constants.APPLICATION_PROPERTIES);
}catch (ConfigurationException e){
logger.error("load configuration excetpion",e);
System.exit(1);
}
}
/**
* get current db performance
* @return MonitorRecord
*/
public static MonitorRecord getCurrentDbPerformance(){
public MonitorRecord getCurrentDbPerformance(){
MonitorRecord monitorRecord = null;
Connection conn = null;
DruidDataSource dataSource = null;
try{
dataSource = ConnectionFactory.getDataSource();
dataSource.setInitialSize(2);
dataSource.setMinIdle(2);
dataSource.setMaxActive(2);
conn = dataSource.getConnection();
if(conn == null){
return monitorRecord;
}
if(conf.getString(Constants.SPRING_DATASOURCE_DRIVER_CLASS_NAME).contains(DbType.MYSQL.toString().toLowerCase())) {
String driverClassName = dataSource.getDriverClassName();
if(driverClassName.contains(DbType.MYSQL.toString().toLowerCase())){
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);
}
}catch (Exception e) {
@ -85,9 +67,6 @@ public class MonitorDBDao {
if (conn != null) {
conn.close();
}
if(dataSource != null){
dataSource.close();
}
} catch (SQLException e) {
logger.error("SQLException ", e);
}
@ -99,7 +78,7 @@ public class MonitorDBDao {
* query database state
* @return MonitorRecord list
*/
public static List<MonitorRecord> queryDatabaseState() {
public List<MonitorRecord> queryDatabaseState() {
List<MonitorRecord> list = new ArrayList<>(1);
MonitorRecord monitorRecord = getCurrentDbPerformance();

Loading…
Cancel
Save