wangting
2025-01-16 18c43123b51a1688ab4ae01fe3d171c7d92e619b
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
 * Copyright 2018-2118 the original author or authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.vci.dbsync;
 
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
import com.vci.corba.common.VCIError;
import com.vci.dbsync.entity.DBInfo;
import com.vci.dbsync.entity.JobInfo;
import com.vci.dbsync.entity.JobItem;
import com.vci.dbsync.entity.VMInfo;
import com.vci.dbsync.filesync.FileSync;
import com.vci.dbsync.log.SyncLog;
import com.vci.dbsync.DBSyncFactory;
import com.vci.dbsync.DBSync;
 
//import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
 
/**
 * @author liuyazhuang
 * @date 2018/9/11 10:30
 * @description 同步数据库任务的具体实现
 * @version 1.0.0
 */
public class JobTask implements Job {
    //private Logger logger = Logger.getLogger(JobTask.class);
 
    /**
     * 执行同步数据库任务
     *
     */
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDataMap data = context.getJobDetail().getJobDataMap();
        JobInfo jobInfo = (JobInfo) data.get("jobInfo");
 
        SyncLog.logger.info("开始任务【" + jobInfo.getName() + "】 调度: " + new Date());
 
        Connection inConn = null;
        Connection outConn = null;
        DBInfo srcDb = (DBInfo) data.get("srcDb");
        DBInfo destDb = (DBInfo) data.get("destDb");
        
        VMInfo srcVM = (VMInfo) data.get("srcVM");
        VMInfo destVM = (VMInfo) data.get("destVM");
        
        String logTitle = (String) data.get("logTitle");
        
        try {
            inConn = createConnection(srcDb);
            outConn = createConnection(destDb);
            if (inConn == null) {
                SyncLog.logger.info("请检查源数据连接!");
                return;
            } else if (outConn == null) {
                SyncLog.logger.info("请检查目标数据连接!");
                return;
            }
            
            DBSync dbHelper = DBSyncFactory.create(destDb.getDbtype());
            
            if (dbHelper == null) {
                SyncLog.logger.info("不支持的数据库类型!");
                return;
            }
            
            
            JobItem[] items = jobInfo.Items();
            
            //ArrayList<String> lstSql = new ArrayList<String>();
            for (JobItem item : items) {
                SyncLog.logger.info("开始同步: " + item.getName());
                
                long start = new Date().getTime();
                
                FileSync fileSync = null;
                if (item.getName().equalsIgnoreCase("PLATFORMBTM_FILEOBJECT")){
                    fileSync = new FileSync(srcVM, destVM);
                    if (item.getDirection()) {
                        fileSync.Init(true);
                    } else {
                        fileSync.Init(false);
                    }
                }
 
                long count = 0;
                if (item.getDirection())
                {
                    count = dbHelper.assembleSQL(item, inConn, outConn, fileSync);
                }
                else
                {
                    count = dbHelper.assembleSQL(item, outConn, inConn, fileSync);
                }
                
                SyncLog.logger.info("同步【" + item.getName() + "】查询到 " + count + " 条记录,同步耗时: " + (new Date().getTime() - start) + "ms");
                
                if (fileSync != null){
                    SyncLog.logger.info("同步【" + item.getName() + "】文件数: " + fileSync.getCount());
                    fileSync.close();
                }
 
//                try {
//                    FileWriter file = new FileWriter("e:\\test.sql");
//
//                    String sNewLine = System.getProperty("line.separator");
//                    for (int i = 0; i < sqls.length; i++) {
//                        file.write(sqls[i]); // 向文件中写入数据
//                        file.write(sNewLine); // \r\n表示换行
//                    }
//                    // file.write(sqls.toString());
//                    file.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
 
//                SyncLog.logger.info("组装SQL耗时:: " + (new Date().getTime() - start) + "ms");
//                if (lstSql.size() > 0) {
//                    SyncLog.logger.debug(lstSql);
//                    long eStart = new Date().getTime();
//                    dbHelper.executeSQL(lstSql, outConn);
//                    SyncLog.logger.info("执行SQL语句耗时: " + (new Date().getTime() - eStart) + "ms");
//                }
            }
        } catch (SQLException e) {
            try {
                outConn.rollback();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            e.printStackTrace();
            SyncLog.logger.error(logTitle + e.getMessage());
            SyncLog.logger.error(logTitle + " SQL执行出错,请检查是否存在语法错误");
        } catch (VCIError ex){
            ex.printStackTrace();
        } catch (Exception ex){
            ex.printStackTrace();
        }
        finally {
            SyncLog.logger.info("关闭源数据库连接");
            destoryConnection(inConn);
            SyncLog.logger.info("关闭目标数据库连接");
            destoryConnection(outConn);
        }
    }
 
    /**
     * 创建数据库连接
     * @param db
     * @return
     */
    private Connection createConnection(DBInfo db) {
        try {
            Class.forName(db.getDriver());
            Connection conn = DriverManager.getConnection(db.getUrl(), db.getUsername(), db.getPassword());
            conn.setAutoCommit(false);
            return conn;
        } catch (Exception e) {
            SyncLog.logger.error(e.getMessage());
        }
        return null;
    }
 
    /**
     * 关闭并销毁数据库连接
     * @param conn
     */
    private void destoryConnection(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
                conn = null;
                //SyncLog.logger.info("数据库连接关闭");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}