initial commit
This commit is contained in:
66
nms_sync/src/com/nms/main/SyncData.java
Normal file
66
nms_sync/src/com/nms/main/SyncData.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package com.nms.main;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.jfinal.aop.Duang;
|
||||
import com.jfinal.kit.PropKit;
|
||||
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
|
||||
import com.jfinal.plugin.activerecord.Db;
|
||||
import com.jfinal.plugin.druid.DruidPlugin;
|
||||
import com.nms.model.SyncDbInfo;
|
||||
import com.nms.thread.SyncSlaveToMasterThread;
|
||||
import com.nms.thread.SyncThread;
|
||||
/**
|
||||
* 数据同步主功能 相当于主动推送操作
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
public class SyncData{
|
||||
public static void main(String[] args) {
|
||||
Logger logger = Logger.getLogger(SyncData.class);
|
||||
logger.info("同步程序开始启动");
|
||||
//从配置文件获取数据库连接信息
|
||||
PropKit.use("db.properties");
|
||||
//创建主数据库数据源
|
||||
DruidPlugin masterDruid=new DruidPlugin(PropKit.get("dburl"),PropKit.get("dbusername"),PropKit.get("dbpassword"));
|
||||
masterDruid.setInitialSize(Integer.valueOf(PropKit.get("dbInitialSize")));
|
||||
masterDruid.setMaxActive(Integer.valueOf(PropKit.get("dbMaxActive")));
|
||||
masterDruid.setMinIdle(Integer.valueOf(PropKit.get("dbMinIdle")));
|
||||
masterDruid.setMaxWait(Integer.valueOf(PropKit.get("dbMaxWait")));
|
||||
ActiveRecordPlugin masterArp=new ActiveRecordPlugin("masterDataSource",masterDruid);
|
||||
masterArp.setShowSql(true);
|
||||
masterDruid.start();
|
||||
masterArp.start();
|
||||
logger.info("加载配置文件 设置当前同步 masterDataSource 完成");
|
||||
List<SyncDbInfo> syncDbInfos = SyncDbInfo.dao.use("masterDataSource").find("select * from sync_db_info");
|
||||
logger.info("数据库获取其它分库 数据库连接信息"+JSON.toJSONString(syncDbInfos));
|
||||
if(syncDbInfos!=null&&syncDbInfos.size()>0){
|
||||
//创建其它数据源的连接
|
||||
Conn.createConn(syncDbInfos);
|
||||
logger.info("分库数据库连接池创建完成");
|
||||
// 定时周期执行线程池 用于周期执行线程的运行
|
||||
ScheduledExecutorService scheduleService = Executors.newScheduledThreadPool(syncDbInfos.size());
|
||||
logger.info("创建线程池完毕 数量大小为"+syncDbInfos.size());
|
||||
// 使用scheduleWithFixedDleay在上一个线程任务执行完成后 5分钟执行下一次任务
|
||||
for(SyncDbInfo syncDbInfo : syncDbInfos){
|
||||
// 主库向分库同步数据
|
||||
SyncThread syncThread = Duang.duang(new SyncThread(syncDbInfo));
|
||||
logger.info("创建主库同步分库线程执行任务");
|
||||
scheduleService.scheduleWithFixedDelay(syncThread, 0, Integer.valueOf(PropKit.use("config.properties").get("syncMaterToSlaveTime")), TimeUnit.MILLISECONDS);
|
||||
// 分库向主库同步数据
|
||||
logger.info("创建分库数据同步到主库线程执行任务");
|
||||
//scheduleService.scheduleWithFixedDelay(new SyncSlaveToMasterThread(syncDbInfo), 0, Integer.valueOf(PropKit.use("config.properties").get("syncSlaveToMaterTime")), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}else{
|
||||
logger.info("获取同步记录信息失败 请检查数据库数据信息");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user