package com.walker.store.task; import com.walker.connector.AbstractConnector; import com.walker.connector.Address; import com.walker.connector.support.DatabaseConnector; import com.walker.connector.util.ConnectorUtils; import com.walker.db.DatabaseType; import com.walker.store.Storeable; import java.util.List; /** * 数据库类型的采集任务默认实现 * @author shikeying * @date 2015年12月23日 * */ public abstract class DatabaseGatherTask extends GatherTask { private Address address = null; private DatabaseType databaseType = DatabaseType.MYSQL; public DatabaseGatherTask(Storeable store, String taskName, Address address) { super(store, taskName); if(address == null){ throw new IllegalArgumentException("DatabaseGatherTask构造错误,缺少参数:address"); } this.address = address; } public void setDatabaseType(DatabaseType databaseType) { this.databaseType = databaseType; } @Override protected AbstractConnector getConnector() { DatabaseConnector dbConnector = null; if(databaseType == DatabaseType.MYSQL){ dbConnector = ConnectorUtils.createMySQLConnector(address); } else if(databaseType == DatabaseType.ORACLE){ dbConnector = ConnectorUtils.createOracleConnector(address); } else { throw new UnsupportedOperationException("其他类型数据库的采集任务还未实现:getConnector()"); } logger.debug(".........采集源数据库类型:" + databaseType); return dbConnector; } @Override protected List invokeRequest(AbstractConnector connector, Object[] params) throws Exception { int s = params.length; if(s < 2){ throw new IllegalArgumentException("数据库查询参数缺失,至少2个:sql, args, rowMapper(option)"); } DatabaseConnector conn = (DatabaseConnector)connector; List result = (List)conn.invoke(params); if(result == null || result.size() == 0){ return null; } logger.debug("//////////////// 数据库请求返回数据一次:" + result); return transferResultData(result); } /** * 把从数据库中的结果转换成系统需要的集合数据。
* 目前数据库实现的代码,数据库返回支持两种方式。 *
	 * 1、通过参数:sql, Object[], 返回Map集合
	 * 2、通过参数:sql, Object[], rowMapper, 返回映射对象集合
	 * 
* 子类来实现具体转换过程。 * @param resultList * @return */ protected abstract List transferResultData(List resultList); }