MySchedule2.java
12.6 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//package org.yrhl.syncdata.task;
//
//import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.yrhl.syncdata.domain.*;
//import org.yrhl.syncdata.mapper.local.CourseOneMapper;
//import org.yrhl.syncdata.mapper.remote.CourseTwoMapper;
//
//import javax.annotation.Resource;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
//@EnableScheduling //开启定时
//@Component
//public class MySchedule2 {
// private static final Logger log = LoggerFactory.getLogger(MySchedule2.class);
// @Resource
// private CourseOneMapper courseOneMapper;
// @Resource
// private CourseTwoMapper courseTwoMapper;
// /**
// * 每隔10秒执行一次 执行生产者 准备数据
// */
// @Scheduled(fixedDelay = 20000)
// public void test(){
// //先查询需要同步的表信息
// List<SyncTable> syncTableList = courseOneMapper.getSyncTableList();
// if(syncTableList.isEmpty()){
// log.error("没有可用的同步表,请查看sync_table表是否有可用的数据");
// return ;
// }
// for(SyncTable syncTable:syncTableList){
// int i = insertIntoSyncResult(syncTable.getTableName());
// log.error("插入SyncResult状态为{}",i==1?"成功":"失败");
// }
// log.info("--------------------插入SyncResult表成功--------------------");
// log.info("--------------------开始备份数据到目标库--------------------");
// test1();
// }
//
//
//// @Scheduled(fixedDelay = 40000)
// public void test1() {
// //从中间表查询需要同步的数据
// List<SyncTable> syncTableList = courseOneMapper.getSyncTableList();
// for (SyncTable syncTable : syncTableList) {
// syncTargetSoucre(syncTable.getTableName());
// }
// log.error("数据同步到目标数据库成功");
// }
//
//
// /***
// * 获取可备份的业务数据并将数据插入到SyncResult中待备份
// * @param tablename
// * @return
// */
// private int insertIntoSyncResult(String tablename ) {
//
// int page = 1;
// while (true) {
// //将数据插入到sync_result 中
// List<SyncResult> syncResultListForWait = null;
// if("allinonepushresults".equals(tablename)){
// syncResultListForWait = courseOneMapper.getSyncResultListForWaitForAllinonepushresults(tablename,(page-1)*100,100);
// }else{
// syncResultListForWait = courseOneMapper.getSyncResultListForWait(tablename,(page-1)*100,100);
// }
// if(CollectionUtils.isEmpty(syncResultListForWait)) {
// break;
// }
// page++;
// try {
// // 1.在中间表查询是否存在数据(根据业务主键):如果存在,忽略,如果不存在,插入
// List<SyncResult> res= new ArrayList<>();
// for(SyncResult syncResult:syncResultListForWait){
// // 遍历逐条分析:
// int exesit = courseOneMapper.isExesit(syncResult.getInfoId());
// if(0==exesit){
// res.add(syncResult);
// }
// }
// if(CollectionUtils.isNotEmpty(res)){
// courseOneMapper.insertSyncResult(res);
// return 1;
// }
// }catch (Exception e){
// log.error("向SyncResult表插入数据失败", e);
// return 0;
// }
// }
//
// return 0;
// }
//
//
// private int syncTargetSoucre(String tableName){
//
// if ("allinonepushresults".equals(tableName)) {
// int page = 1;
// while (true) {
// log.info("消费者:获取{}的数据","allinonepushresults");
// List<AllinonepushresultsEntity> syncResultListForWait = courseOneMapper.getSyncResultListForWaitForConsumer(tableName,(page-1)*100,100);
// List<Long> ids = null;
// if (CollectionUtils.isEmpty(syncResultListForWait)) {
// break;
// }
// page++;
// try {
// List<AllinonepushresultsEntity> resData = new ArrayList<>();
// for (AllinonepushresultsEntity allinonepushresultsEntity : syncResultListForWait) {
// int count = courseTwoMapper.isExist("allinonepushresults", allinonepushresultsEntity.getId());
// if (0 == count) {
// resData.add(allinonepushresultsEntity);
// }
// }
// if (CollectionUtils.isNotEmpty(resData)) {
// courseTwoMapper.insertAllinonepushresultsEntityBatch(resData);
// }
// ids = syncResultListForWait.stream()
// .map(AllinonepushresultsEntity::getId)
// .collect(Collectors.toList());
// for (Long id : ids) {
// courseOneMapper.updateSyncResult(id);
// }
// } catch (Exception e) {
// ids = syncResultListForWait.stream()
// .map(AllinonepushresultsEntity::getId)
// .collect(Collectors.toList());
// for (Long id : ids) {
// courseOneMapper.updateSyncResultFail(id);
// }
// }
// }
// log.info("消费者:表名{}同步完毕","allinonepushresults");
// }
// else if ("block_new_source_data".equals(tableName)) {
// int page = 1;
// while (true) {
// log.info("消费者:获取{}数据","block_new_source_data");
// List<Long> ids = null;
// List<Map<String, Object>> list3 = courseOneMapper.getSyncResultListForWaitForOtherConsumer(tableName,(page-1)*100,100);
// if (CollectionUtils.isEmpty(list3)) {
// break;
// }
// List<BlockNewSourceDataEntity> res1 = list3.stream()
// .map(BlockNewSourceDataEntity::fromMap)
// .collect(Collectors.toList());
// page++;
// try {
// List<BlockNewSourceDataEntity> resData = new ArrayList<>();
// for (BlockNewSourceDataEntity blockNewSourceDataEntity : res1) {
// int count = courseTwoMapper.isExist("block_new_source_data", blockNewSourceDataEntity.getId());
// if (0 == count) {
// resData.add(blockNewSourceDataEntity);
// }
// }
// if (CollectionUtils.isNotEmpty(resData)) {
// courseTwoMapper.insertBlockNewSourceDataEntityBatch(resData);
// }
// ids = res1.stream()
// .map(BlockNewSourceDataEntity::getId)
// .collect(Collectors.toList());
// for (Long id : ids) {
// courseOneMapper.updateSyncResult(id);
// }
// } catch (Exception e) {
// ids = res1.stream()
// .map(BlockNewSourceDataEntity::getId)
// .collect(Collectors.toList());
// for (Long id : ids) {
// courseOneMapper.updateSyncResultFail(id);
// }
// }
// }
// log.info("消费者:表名{}同步完毕","block_new_source_data");
// }
// else if("block_source_data".equals(tableName)){
// {
// int page = 1;
// while (true) {
// log.info("消费者:获取{}的数据","block_source_data");
// List<Long> ids = null;
// List<Map<String, Object>> list3 = courseOneMapper.getSyncResultListForWaitForOtherConsumer(tableName,(page-1)*100,100);
// if (CollectionUtils.isEmpty(list3)) {
// break;
// }
// List<BlockSourceDataEntity> res1 = list3.stream()
// .map(BlockSourceDataEntity::fromMap)
// .collect(Collectors.toList());
// page++;
// try {
// List<BlockSourceDataEntity> resData = new ArrayList<>();
// for (BlockSourceDataEntity blockSourceDataEntity : res1) {
// int count = courseTwoMapper.isExist("block_source_data", blockSourceDataEntity.getId());
// if (0 == count) {
// resData.add(blockSourceDataEntity);
// }
// }
// if (CollectionUtils.isNotEmpty(resData)) {
// courseTwoMapper.insertBlockSourceDataEntityBatch(resData);
// }
// ids = res1.stream()
// .map(BlockSourceDataEntity::getId)
// .collect(Collectors.toList());
// for (Long id : ids) {
// courseOneMapper.updateSyncResult(id);
// }
// } catch (Exception e) {
// ids = res1.stream()
// .map(BlockSourceDataEntity::getId)
// .collect(Collectors.toList());
// for (Long id : ids) {
// courseOneMapper.updateSyncResultFail(id);
// }
// }
// }
// log.info("消费者:表名{}同步完毕","block_source_data");
// }
//
// }
// else if("business_data_results".equals(tableName)){
// {
// int page = 1;
// while (true) {
// log.info("消费者:获取{}数据","business_data_results");
// List<Long> ids = null;
// List<Map<String, Object>> list3 = courseOneMapper.getSyncResultListForWaitForOtherConsumer(tableName,(page-1)*100,100);
// if (CollectionUtils.isEmpty(list3)) {
// break;
// }
// List<BusinessDataResultsEntity> res1 = list3.stream()
// .map(BusinessDataResultsEntity::fromMap)
// .collect(Collectors.toList());
// page++;
// try {
// List<BusinessDataResultsEntity> resData = new ArrayList<>();
// for (BusinessDataResultsEntity businessDataResultsEntity : res1) {
// int count = courseTwoMapper.isExist("business_data_results", businessDataResultsEntity.getId());
// if (0 == count) {
// resData.add(businessDataResultsEntity);
// }
// }
// if (CollectionUtils.isNotEmpty(resData)) {
// courseTwoMapper.insertBusinessDataResultsBatch(resData);
// }
// ids = res1.stream()
// .map(BusinessDataResultsEntity::getId)
// .collect(Collectors.toList());
// for (Long id : ids) {
// courseOneMapper.updateSyncResult(id);
// }
// } catch (Exception e) {
// ids = res1.stream()
// .map(BusinessDataResultsEntity::getId)
// .collect(Collectors.toList());
// for (Long id : ids) {
// courseOneMapper.updateSyncResultFail(id);
// }
// }
// }
// log.info("消费者:表名{}同步完毕","business_data_results");
// }
// }
//
// return 0;
// }
//}