(1)整体设计
(2)字典模块
(3)Redis模块
(4)加载总控线程
package com.crm.application.main;
import java.sql.Connection;
import java.sql.Statement;
import java.util.concurrent.CountDownLatch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.crm.application.load.DictionaryLoadThread;
import com.crm.h2.H2Manager;
import com.crm.h2.load.H2LoadThread;
import com.crm.redis.load.RedisLoadThread;
public class MainThread extends Thread {
private static Log log = LogFactory.getLog(MainThread.class);
public static final CountDownLatch mainThreadStopLatch = new CountDownLatch(1);
@Override
public void run() {
log.info("-------------------------【总制线程】BEGIN-------------------------");
try {
log.info("---------------【CRM系统初始化】【01】【H2初始化】---------------");
Connection conn = H2Manager.getConnection();
Statement stmt = conn.createStatement();
stmt.executeUpdate(H2Manager.getCreateCrmTranslateKindSql());
stmt.executeUpdate(H2Manager.getCreateCrmTranslateMapSql());
conn.close();
new H2LoadThread().start();
log.info("---------------【CRM系统初始化】【02】【字典加载】---------------");
new DictionaryLoadThread().start();
mainThreadStopLatch.await();
} catch (Exception e) {
e.printStackTrace();
}
log.info("---------------【CRM系统初始化】【03】【Redis缓存加载】---------------");
// 必须等待上面的2个线程执行完成后,在执行Redis
new RedisLoadThread().start();
log.info("-------------------------【总制线程】END-------------------------");
}
}
(5)字典加载线程
package com.crm.application.load;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.crm.application.CrmContext;
import com.crm.application.main.MainThread;
import com.crm.common.util.CrmConst;
import com.crm.model.common.BODwdCrmMemExtinfoDim;
import com.crm.model.dictionary.BOCrmDictionary;
import com.crm.service.base.BaseSVImpl;
import com.crm.service.dictionary.DictionarySVImpl;
import com.crm.service.dictionary.StaticDataSVImpl;
import com.crm.service.dictionary.load.implclass.interfaces.IDictionaryLoadSV;
public class DictionaryLoadThread extends Thread {
private static Log log = LogFactory.getLog(DictionaryLoadThread.class);
List<BODwdCrmMemExtinfoDim> list = null;
List<String> dimCodeList = null;
// 1.获取数据仓库的数据
BaseSVImpl BaseSVImpl = CrmContext.getBean("BaseSVImpl", BaseSVImpl.class);
DictionarySVImpl DictionarySVImpl = CrmContext.getBean("DictionarySVImpl", DictionarySVImpl.class);
StaticDataSVImpl StaticDataSVImpl = CrmContext.getBean("StaticDataSVImpl", StaticDataSVImpl.class);
@Override
public void run() {
try {
if (checkDataRepositoryNeedLoad()) {
doRun();
}
} catch (Exception e) {
log.error(e.getMessage());
} finally {
MainThread.mainThreadStopLatch.countDown();
}
}
private void doRun() {
// 1.清空静态数据
StringBuilder sql = new StringBuilder();
sql.append(" DELETE CRM_STATIC_DATA A ");
sql.append(" WHERE EXISTS (SELECT 1 ");
sql.append(" FROM CRM_DICTIONARY B ");
sql.append(" WHERE A.DICTIONARY_ID = B.ID ");
sql.append(" AND B.INIT_SRC = :INIT_SRC ) ");
Map params = new HashMap();
params.put("INIT_SRC", CrmConst.CrmDictionaryInitSrc.DATA_REPOSITORY);
BaseSVImpl.executeNativeUpdate(sql.toString(), params);
// 2.清空静态数据
sql.delete(0, sql.length());
sql.append(" DELETE CRM_DICTIONARY A WHERE A.INIT_SRC = :INIT_SRC ");
BaseSVImpl.executeNativeUpdate(sql.toString(), params);
// 3.新增字典数据
List<BOCrmDictionary> BOCrmDictionarys = new ArrayList<BOCrmDictionary>();
for (String dimCode : dimCodeList) {
BODwdCrmMemExtinfoDim bo = getBODwdCrmMemExtinfoDim(dimCode, list);
BOCrmDictionary d = new BOCrmDictionary();
d.setCityControl(bo.getCityValidFlag().toString());
d.setCode(bo.getDimCode().toString());
d.setCreateDate(new Date());
d.setInitImpl("com.crm.service.dictionary.load.implclass.impl.DefaultDictionaryLoadSVImpl");
d.setInitSrc(CrmConst.CrmDictionaryInitSrc.DATA_REPOSITORY);
d.setInitType(CrmConst.CrmDictionaryInitType.IMPL_CLASS);
d.setLoadOnStartUp(CrmConst.commonYesOrNoString.YES);
d.setName(bo.getDimName());
d.setRemarks("");
d.setState(CrmConst.CommonState.VALIDATION);
d.setUpdateDate(new Date());
BOCrmDictionarys.add(d);
}
DictionarySVImpl.saveBatch(BOCrmDictionarys);
// 获取所有的字典
sql.delete(0, sql.length());
sql.append(" from BOCrmDictionary a where a.state = :state and a.loadOnStartUp = :loadOnStartUp");
params = new HashMap();
params.put("state", CrmConst.CommonState.VALIDATION);
params.put("loadOnStartUp", CrmConst.commonYesOrNoString.YES);
List<BOCrmDictionary> existDRList = DictionarySVImpl.getEntitys(sql.toString(), params);
for (BOCrmDictionary b : existDRList) {
try {
if (b.getInitType().equals(CrmConst.CrmDictionaryInitType.STATIC_DATA)) {
} else if (b.getInitType().equals(CrmConst.CrmDictionaryInitType.IMPL_CLASS)) {
String className = b.getInitImpl();
IDictionaryLoadSV sv = (IDictionaryLoadSV) Class.forName(className).newInstance();
sv.execute(b, list);
} else if (b.getInitType().equals(CrmConst.CrmDictionaryInitType.SQL)) {
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private boolean checkDataRepositoryNeedLoad() {
boolean flag = true;
StringBuilder sql = new StringBuilder();
sql.append(" SELECT * FROM DWD_CRM_MEM_EXTINFO_DIM A ");
list = BaseSVImpl.executeNativeSelect(sql.toString(), new HashMap(), BODwdCrmMemExtinfoDim.class);
dimCodeList = new ArrayList<String>();
for (BODwdCrmMemExtinfoDim b : list) {
if (!dimCodeList.contains(b.getDimCode().toString())) {
dimCodeList.add(b.getDimCode().toString());
}
}
sql.delete(0, sql.length());
sql.append(" SELECT COUNT(1) TOTAL_COUNT ");
sql.append(" FROM CRM_STATIC_DATA A, CRM_DICTIONARY B ");
sql.append(" WHERE A.DICTIONARY_ID = B.ID ");
sql.append(" AND A.STATE = :STATE ");
sql.append(" AND B.STATE = :STATE ");
sql.append(" AND B.INIT_SRC = :INIT_SRC");
Map params = new HashMap();
params.put("STATE", CrmConst.CommonState.VALIDATION);
params.put("INIT_SRC", CrmConst.CrmDictionaryInitSrc.DATA_REPOSITORY);
List<Map> totalCountList = BaseSVImpl.executeNativeSelect(sql.toString(), params);
long dataRepositoryAmount = Long.parseLong(totalCountList.get(0).get("TOTAL_COUNT").toString());
if (list.size() == dataRepositoryAmount) {
flag = false;
}
return flag;
}
private BODwdCrmMemExtinfoDim getBODwdCrmMemExtinfoDim(String dimCode, List<BODwdCrmMemExtinfoDim> list) {
BODwdCrmMemExtinfoDim r = null;
for (BODwdCrmMemExtinfoDim b : list) {
if (dimCode.equals(b.getDimCode().toString())) {
r = b;
break;
}
}
return r;
}
}