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
/**
 * 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 static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
 
import com.vci.dbsync.entity.DBInfo;
import com.vci.dbsync.entity.JobInfo;
import com.vci.dbsync.entity.VMInfo;
import com.vci.dbsync.log.SyncLog;
import com.vci.dbsync.utils.Tool;
import com.vci.dbsync.JobTask;
 
/**
 * @author liuyazhuang
 * @date 2018/9/11 10:08
 * @description 同步数据库数据的Builder对象
 * @version 1.0.0
 */
public class DBSyncBuilder {
 
    private DBInfo srcDb;
    private DBInfo destDb;
    
    private VMInfo srcVM;
    private VMInfo destVM;
    
    private List<JobInfo> jobList;
    private String code;
    //private static Logger logger = Logger.getLogger(DBSyncBuilder.class);
 
    private DBSyncBuilder(){
    }
 
    /**
     * 创建DBSyncBuilder对象
     * @return DBSyncBuilder对象
     */
    public static DBSyncBuilder builder(){
        return new DBSyncBuilder();
    }
 
    /**
     * 初始化数据库信息并解析jobs.xml填充数据
     * @return DBSyncBuilder对象
     */
    public DBSyncBuilder init() {
        srcDb = new DBInfo();
        destDb = new DBInfo();
        
        srcVM = new VMInfo();
        destVM = new VMInfo();
        
        jobList = new ArrayList<JobInfo>();
        SAXReader reader = new SAXReader();
        try {
            // 读取xml的配置文件名,并获取其里面的节点
            Element root = reader.read("properties/dbjobs.xml").getRootElement();
            Element src = root.element("source");
            Element dest = root.element("dest");
            
            Element srcVMNode = root.element("srcVM");
            Element destVMNode = root.element("destVM");
            
            Element jobs = root.element("jobs");
            // 遍历job即同步的表
            for (@SuppressWarnings("rawtypes")
                Iterator it = jobs.elementIterator("job"); it.hasNext();) {
                Element e = (Element) it.next();
                
                JobInfo job = new JobInfo();
                job.setName(e.elementTextTrim("name"));
                job.setCron(e.elementTextTrim("cron"));
                
                job.initJobItems(e);
                
                jobList.add(job);
                
                //jobList.add((JobInfo) elementInObject((Element) it.next(), new JobInfo()));
            }
            //
            Tool.elementInObject(src, srcDb);
            Tool.elementInObject(dest, destDb);
            Tool.elementInObject(srcVMNode, srcVM);
            Tool.elementInObject(destVMNode, destVM);
            
            code = root.element("code").getTextTrim();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }
 
//    /**
//     * 解析e中的元素,将数据填充到o中
//     * @param e 解析的XML Element对象
//     * @param o 存放解析后的XML Element对象
//     * @return 存放有解析后数据的Object
//     * @throws IllegalArgumentException
//     * @throws IllegalAccessException
//     */
//    public Object elementInObject(Element e, Object o) throws IllegalArgumentException, IllegalAccessException {
//        Field[] fields = o.getClass().getDeclaredFields();
//        for (int index = 0; index < fields.length; index++) {
//            Field item = fields[index];
//            //当前字段不是serialVersionUID,同时当前字段不包含serialVersionUID
//            if (!Constants.FIELD_SERIALVERSIONUID.equals(item.getName()) && !item.getName().contains(Constants.FIELD_SERIALVERSIONUID)){
//                item.setAccessible(true);
//                item.set(o, e.element(item.getName()).getTextTrim());
//            }
//        }
//        return o;
//    }
 
    /**
     * 启动定时任务,同步数据库的数据
     */
    public void start() {
        for (int index = 0; index < jobList.size(); index++) {
            JobInfo jobInfo = jobList.get(index);
            String logTitle = "[" + code + "]" + jobInfo.getName() + " ";
            try {
                SchedulerFactory sf = new StdSchedulerFactory();
                Scheduler sched = sf.getScheduler();
                JobDetail job = newJob(JobTask.class).withIdentity("job-" + jobInfo.getName(), code).build();
                job.getJobDataMap().put("srcDb", srcDb);
                job.getJobDataMap().put("destDb", destDb);
                job.getJobDataMap().put("srcVM", srcVM);
                job.getJobDataMap().put("destVM", destVM);
                
                job.getJobDataMap().put("jobInfo", jobInfo);
                job.getJobDataMap().put("logTitle", logTitle);
                SyncLog.logger.info(jobInfo.getCron());
                CronTrigger trigger = newTrigger().withIdentity("trigger-" + jobInfo.getName(), code).withSchedule(cronSchedule(jobInfo.getCron())).build();
                sched.scheduleJob(job, trigger);
                sched.start();
            } catch (Exception e) {
                SyncLog.logger.info(logTitle + e.getMessage());
                SyncLog.logger.info(logTitle + " run failed");
                continue;
            }
        }
    }
}