shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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<Object> 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<Object> result = (List<Object>)conn.invoke(params);
        if(result == null || result.size() == 0){
            return null;
        }
        logger.debug("//////////////// 数据库请求返回数据一次:" + result);
        return transferResultData(result);
    }
 
    /**
     * 把从数据库中的结果转换成系统需要的集合数据。<br>
     * 目前数据库实现的代码,数据库返回支持两种方式。
     * <pre>
     * 1、通过参数:sql, Object[], 返回Map集合
     * 2、通过参数:sql, Object[], rowMapper, 返回映射对象集合
     * </pre>
     * 子类来实现具体转换过程。
     * @param resultList
     * @return
     */
    protected abstract List<Object> transferResultData(List<Object> resultList);
}