package com.changhong.epc.parsing.service.loop.impl; import com.changhong.epc.bean.admin.InitCompany; import com.changhong.epc.config.tool.MybatisTool; import com.changhong.epc.constter.base.BaseConst; import com.changhong.epc.constter.parsing.loop.LoopServiceConst; import com.changhong.epc.parsing.service.loop.ILoop; import com.changhong.epc.parsing.service.loop.tools.ICalcSleepTime; import com.changhong.epc.parsing.service.loop.tools.TimerHandle; import com.changhong.epc.parsing.service.loop.tools.impl.CalcDefaultSleepTime; import com.changhong.epc.parsing.service.loop.tools.thread.MyThread; import com.changhong.epc.rely.api.epc.admin.InitCompanyApi; import com.iemsoft.framework.cloud.core.exception.IEMRuntimeException; import com.iemsoft.framework.cloud.core.thread.ThreadData; import com.iemsoft.framework.cloud.core.tools.JSONTool; import lombok.extern.slf4j.Slf4j; import javax.annotation.Resource; import java.util.*; import java.util.concurrent.TimeUnit; /** * 服务父类 * @ClassName: LoopSuper * @author [九鼎联合科技] * @date 2017年6月19日 下午6:41:18 */ @Slf4j public abstract class LoopSuper implements ILoop, BaseConst { /** * 业务处理线程 */ private MyThread currThread ; /** * 执行计数器 */ private long countLoop; /** * 计算等待时间接口 * 默认使用 CalcDefaultSleepTime */ private ICalcSleepTime calcSleepTime; @Resource private InitCompanyApi initCompanyApi; /** * 设置线程名称 * @return */ public String getThreadName(){ return this.getClass().getSimpleName(); } public LoopSuper() { } /** * 初始化 调用子类业务处理 */ // @PostConstruct public void init(){ log.debug("启动服务"); try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { log.error(e.getMessage(), e); Thread.currentThread().interrupt(); } currThread = TimerHandle.createThread(()->{ Long sleep = LoopServiceConst.SLEEP_DEF_TIME; try { doService(); } catch (Exception e) { log.error(e.getMessage(), e); } // if(calcSleepTime != null) // sleep = calcSleepTime.getSleepTime(); currThread.setSleepTime(sleep); ++ countLoop; printServiceRunStartLog(); },getThreadName(), LoopServiceConst.EXE_SERVICE_SPACE); calcSleepTime = calcSleepTime(); // 启动当前服务 start(); currThread.setSleepTime(LoopServiceConst.SLEEP_DEF_TIME); } protected void doService(){ // 执行业务服务 List initCompanies = initCompanyApi.getAllInitCompany(); Set distincd = new TreeSet<>((init1, init2)->init1.getCompanyId()-init2.getCompanyId()); log.debug("当前系统租户:{}", JSONTool.toJson(initCompanies)); initCompanies.stream() .forEach(initCompany -> { if(distincd.contains(initCompany)){ return; } distincd.add(initCompany); log.debug("准备执行租户:{}", JSONTool.toJson(initCompany)); ThreadData.set(TENANT_ID, Objects.toString(initCompany.getTenantId(), "")); ThreadData.set(COMPANY_ID, initCompany.getCompanyId()); MybatisTool.initDataSource(initCompany); businessHandle(); }); } /** * 打印每个服务运行状态到日志 * @Title: printServiceRunStartLog * @param * @return void 返回类型 * @throws */ private void printServiceRunStartLog(){ log.debug("\n\n------------------->"+currThread + "\t" + countLoop); StringBuilder logStr = new StringBuilder(); logStr.append("\n\t 服务启动时间:").append(currThread.getCurrThreadRunTime()/1000/60).append(" 分"); logStr.append(getThreadName()).append("服务状态: ").append(countLoop).append("\n"); logStr.append("\n\t 本次执行时间: ").append(currThread.getOnceServiceRunTime()).append("毫秒"); logStr.append("\n\t 本次休息时间").append(currThread.getSleepTime()).append("毫秒\n\n"); log.debug(logStr.toString() ); } /** * 自定义等待算法需要重写calcSleepTime */ public ICalcSleepTime calcSleepTime() { return new CalcDefaultSleepTime(currThread); } @Override public void start() { currThread.start(); } @Override public void stop() { currThread.setRun(false); } @Override public Long currServiceExecuteTime() { return currThread.getCurrThreadRunTime(); } @Override public void restart() { stop();start(); } public MyThread getCurrThread() throws Exception { if (currThread != null) { return currThread; } throw new IEMRuntimeException("currThread 为空"); } public void setCurrThread(MyThread currThread) { this.currThread = currThread; } @Override public Long smcols(){ return countLoop; } public ICalcSleepTime getCalcSleepTime() { return calcSleepTime; } public void setCalcSleepTime(ICalcSleepTime calcSleepTime) { this.calcSleepTime = calcSleepTime; } public static void main(String... args) { String names = "epc_app_stander_def,epc_bill_check_msg,epc_bill_data,epc_bill_info,epc_cost_from_mapping,epc_cost_type,epc_department_relation,epc_err_system_msg,epc_fixed_asset,epc_fixed_err_asset,epc_fixed_summary,epc_form_contract,epc_form_contract_phase,epc_journey_form_mapping,epc_master_define,epc_master_element,epc_master_value,epc_order_status,epc_parent_budget_org,epc_rule_msg,epc_supp_stander,epc_system_config,epc_system_math,epc_system_math_param"; Arrays.asList(names.split(",")).stream() .forEach(name->{ System.out.printf("SET @createSql = CONCAT(\"CREATE TABLE IF NOT EXISTS %s_\", tenantId, \"_\", companyId, \" LIKE %s\");\n" + "PREPARE createSql FROM @createSql;\n" + "EXECUTE createSql;\n\n", name, name); }); } }