很好,到目前为止,一切都很顺利!
下面分析ExecutorManager的初始化过程
======================================================================
public class JdbcExecutorLoader extends AbstractJdbcLoader implements
ExecutorLoader {
小的就不说了,主要是看AbstractJdbcLoader ,有个成员
public AbstractJdbcLoader(Props props) {
dataSource = DataSourceUtils.getDataSource(props);
}
其实是生成了一个MysqlBasicDataSource
public static AzkabanDataSource getDataSource(Props props) {//看到这里了
String databaseType = props.getString("database.type");
AzkabanDataSource dataSource = null;
if (databaseType.equals("mysql")) {//一般都是mysql
int port = props.getInt("mysql.port");//下面都是获取参数
String host = props.getString("mysql.host");
String database = props.getString("mysql.database");
String user = props.getString("mysql.user");
String password = props.getString("mysql.password");
int numConnections = props.getInt("mysql.numconnections");
dataSource =
getMySQLDataSource(host, port, database, user, password,
numConnections);
} else if (databaseType.equals("h2")) {
String path = props.getString("h2.path");
dataSource = getH2DataSource(path);
}
return dataSource;
}
---
/**
* Create a MySQL DataSource
*
* @param host
* @param port
* @param dbName
* @param user
* @param password
* @param numConnections
* @return
*/
public static AzkabanDataSource getMySQLDataSource(String host, Integer port,
String dbName, String user, String password, Integer numConnections) {
return new MySQLBasicDataSource(host, port, dbName, user, password,
numConnections);
}
所以,前面都是取属性,然后生成MySQLBasicDataSource
====================================================
关注下这个类的结构
public static class MySQLBasicDataSource extends AzkabanDataSource {
public abstract class AzkabanDataSource extends BasicDataSource {
所以说,最终还是用了dbcp的连接池,到这里,读者对这个类大概也就知道怎么回事了,
其它细节其实都不用关心。
最后要强调的是:在
public static class MySQLBasicDataSource extends AzkabanDataSource {
private static MonitorThread monitorThread = null;//有一个监听线程
代码如下:
private class MonitorThread extends Thread {
private static final long MONITOR_THREAD_WAIT_INTERVAL_MS = 30 * 1000;
private boolean shutdown = false;
MySQLBasicDataSource dataSource;
public MonitorThread(MySQLBasicDataSource mysqlSource) {
this.setName("MySQL-DB-Monitor-Thread");
dataSource = mysqlSource;
}
@SuppressWarnings("unused")
public void shutdown() {
shutdown = true;
this.interrupt();
}
public void run() {
while (!shutdown) {
synchronized (this) {
try {
pingDB();
wait(MONITOR_THREAD_WAIT_INTERVAL_MS);
} catch (InterruptedException e) {
logger.info("Interrupted. Probably to shut down.");
}
}
}
}
private void pingDB() {
Connection connection = null;
try {
connection = dataSource.getConnection();
PreparedStatement query = connection.prepareStatement("SELECT 1");
query.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger
.error("MySQL connection test failed. Please check MySQL connection health!");
} finally {
DbUtils.closeQuietly(connection);
}
}
}
}
有兴趣的可以研究下具体做啥的 :)
那么到了这里, JDBC方面就初始化好了。
真正的execManager的初始化就不说了,读者可以自己研究下 :)