package com.yc.api.schedule; import cn.jiguang.common.resp.APIConnectionException; import cn.jiguang.common.resp.APIRequestException; import com.yc.api.bean.attendance.T180252; import com.yc.api.service.AttendanceServiceIfc; import com.yc.api.utils.PushMessageType; import com.yc.entity.AttachmentConfig; import com.yc.entity.DataSourceEntity; import com.yc.factory.FactoryBean; import com.yc.jpush.JpushAction; import com.yc.multiData.MultiDataSource; import com.yc.multiData.SpObserver; import com.yc.service.BaseService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.scheduling.Trigger; import org.springframework.scheduling.TriggerContext; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.support.CronTrigger; import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.time.Duration; import java.time.Instant; import java.util.*; import java.util.concurrent.TimeUnit; /** * spring加载完成后启动 * * 定时更新前一天的打卡考勤记录,每天凌晨4点后执行 * */ @Service public class AttendanceSchedule implements ApplicationListener { protected final Logger log = LoggerFactory.getLogger(this.getClass()); private static JpushAction jpushAction =JpushAction.getInstance(); @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { if (contextRefreshedEvent.getApplicationContext().getParent() == null) { return; } String isAttendanceRunSchedule = AttachmentConfig.get("isAttendanceRunSchedule");//只在设置了定时任务的服务器上运行 if ("1".equals(isAttendanceRunSchedule)) { ThreadPoolTaskScheduler threadPoolTaskScheduler = (ThreadPoolTaskScheduler) FactoryBean.getBean("threadPoolTaskScheduler"); //考勤统计功能 threadPoolTaskScheduler.schedule(new PushData(), new Trigger() { @Override public Date nextExecutionTime(TriggerContext triggerContext) { return new CronTrigger("0 15 4 * * ?").nextExecutionTime(triggerContext);//每天凌晨4点15分执行 } }); //打卡提醒功能 threadPoolTaskScheduler.scheduleWithFixedDelay(new PushDataAlertTime(), Instant.now().plusMillis(TimeUnit.MINUTES.toMillis(3)), Duration.ofMinutes(1));//1分钟执行一次 } } private class PushDataAlertTime implements Runnable { @Override public void run() { System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "打卡提醒推送..."); log.info("启动定时打卡提醒推送....."); ThreadPoolTaskExecutor threadPoolExecutor= (ThreadPoolTaskExecutor) FactoryBean.getBean("threadPoolExecutor"); Map infoList = MultiDataSource.getDataSourceMaps(); for (Map.Entry entry : infoList.entrySet()) { DataSourceEntity dataSourceEntity = entry.getValue(); //非托管系统跳过执行 if(ScheduleUtils.isOnbusPlatform(dataSourceEntity)){ continue;} //threadPoolExecutor.execute(new AlertTimeThread(dataSourceEntity,threadPoolExecutor )); try { String dbid = dataSourceEntity.getDbId() + ""; log.info(dataSourceEntity.getSystemDescribe() + "开始执行"); SpObserver.setDBtoInstance("_" + dbid); AttendanceServiceIfc attendanceServiceIfc = (AttendanceServiceIfc) FactoryBean.getBean("attendanceService"); final List list = attendanceServiceIfc.getT180252List(null,0); log.info(dataSourceEntity.getSystemDescribe() + "准备推送:" + (list != null ? list.size() : 0)); //保存需要更新的sql,批量提交 List seqList = new ArrayList<>(); if (list != null && list.size() > 0){ for (T180252 t180252 : list) { String usercode = t180252.getUserCode(); Map extras = new HashMap<>(); extras.put("actionType", PushMessageType.ATTENDANCE); extras.put("msg", t180252.getDescription() + ""); extras.put("msgCount", "1"); extras.put("title", "打卡提醒"); List alais = new ArrayList(); alais.add(dataSourceEntity.getDbId() + "_" + usercode); try { boolean flag = jpushAction.sendPush( JpushAction.buildPushObject_with_extra( alais, t180252.getTitle(), extras)); if (flag) { //推送成功 //log.info(dataSourceEntity.getSystemDescribe()+":"+usercode+">推送成功"); seqList.add(String.valueOf(t180252.getSeq())); } } catch (APIConnectionException e) { // Connection error, should retry later log.error(usercode + " Connection error, should retry later" + e.getMessage()); } catch (APIRequestException e) { // Should review the error, and fix the request log.error("dbid: " + dataSourceEntity.getDbId() + ">" + usercode); log.error("HTTP Status: " + e.getStatus()); log.error("Error Code: " + e.getErrorCode()); log.error("Error Message: " + e.getErrorMessage()); } catch (Exception e) { log.error(dataSourceEntity.getSystemDescribe() + ":" + usercode + ">推送出错:" + e.getMessage()); e.printStackTrace(); } } } if(seqList.size()>0){ //更新可发送次数 attendanceServiceIfc.updateT180252(seqList); } }catch (RuntimeException r){ throw r; }catch (Exception ex){ ex.printStackTrace(); log.info(dataSourceEntity.getSystemDescribe()+":"+ex.getMessage()); }finally { SpObserver.setDBtoInstance(); } } log.info("定时打卡提醒推送完成....."); } } private class AlertTimeThread implements Runnable { final DataSourceEntity dataSourceEntity; final ThreadPoolTaskExecutor threadPoolExecutor; public AlertTimeThread(DataSourceEntity dataSourceEntity, ThreadPoolTaskExecutor threadPoolTaskExecutor) { this.dataSourceEntity = dataSourceEntity; this.threadPoolExecutor=threadPoolTaskExecutor; } @Override public void run() { try { String dbid = dataSourceEntity.getDbId() + ""; log.info(dataSourceEntity.getSystemDescribe() + "开始执行"); SpObserver.setDBtoInstance("_" + dbid); AttendanceServiceIfc attendanceServiceIfc = (AttendanceServiceIfc) FactoryBean.getBean("attendanceService"); final List list = attendanceServiceIfc.getT180252List(null,0); log.info(dataSourceEntity.getSystemDescribe() + "准备推送:" + (list != null ? list.size() : 0)); //保存需要更新的sql,批量提交 List seqList = new ArrayList<>(); if (list != null && list.size() > 0){ for (T180252 t180252 : list) { String usercode = t180252.getUserCode(); Map extras = new HashMap<>(); extras.put("actionType", PushMessageType.ATTENDANCE); extras.put("msg", t180252.getDescription() + ""); extras.put("msgCount", "1"); extras.put("title", "打卡提醒"); List alais = new ArrayList(); alais.add(dataSourceEntity.getDbId() + "_" + usercode); try { boolean flag = jpushAction.sendPush( JpushAction.buildPushObject_with_extra( alais, t180252.getTitle(), extras)); if (flag) { //推送成功 //log.info(dataSourceEntity.getSystemDescribe()+":"+usercode+">推送成功"); seqList.add(String.valueOf(t180252.getSeq())); } } catch (APIConnectionException e) { // Connection error, should retry later log.error(usercode + " Connection error, should retry later" + e.getMessage()); } catch (APIRequestException e) { // Should review the error, and fix the request log.error("dbid: " + dataSourceEntity.getDbId() + ">" + usercode); log.error("HTTP Status: " + e.getStatus()); log.error("Error Code: " + e.getErrorCode()); log.error("Error Message: " + e.getErrorMessage()); } catch (Exception e) { log.error(dataSourceEntity.getSystemDescribe() + ":" + usercode + ">推送出错:" + e.getMessage()); e.printStackTrace(); } } } if(seqList.size()>0){ //更新可发送次数 attendanceServiceIfc.updateT180252(seqList); } }catch (RuntimeException r){ throw r; }catch (Exception ex){ ex.printStackTrace(); log.info(dataSourceEntity.getSystemDescribe()+":"+ex.getMessage()); }finally { SpObserver.setDBtoInstance(); } } } private class PushData implements Runnable { @Override public void run() { System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "更新考勤记录..."); ThreadPoolTaskExecutor threadPoolExecutor= (ThreadPoolTaskExecutor) FactoryBean.getBean("threadPoolExecutor"); Map infoList = MultiDataSource.getDataSourceMaps(); log.info("启动定时更新考勤记录任务.....["+(infoList!=null?infoList.size():0)+"]"); for (Map.Entry entry : infoList.entrySet()) { DataSourceEntity dataSourceEntity = entry.getValue(); //非托管系统跳过执行 if(ScheduleUtils.isOnbusPlatform(dataSourceEntity)){ continue;} // try { // //一个系统开一个线程 // threadPoolExecutor.execute(new FormThread(dataSourceEntity,threadPoolExecutor )); // Thread.sleep(10000);//暂停10秒,避免过多的并发 // } catch (Exception e) { // e.printStackTrace(); // } //---单线程 try { String dbid = dataSourceEntity.getDbId() + ""; log.info(dataSourceEntity.getSystemDescribe() + "-开始执行更新考勤"); SpObserver.setDBtoInstance("_"+dbid); BaseService baseService= (BaseService) FactoryBean.getBean("BaseService"); baseService.getSimpleJdbcTemplate().execute("exec p180251"); log.info(dataSourceEntity.getSystemDescribe() + "-更新考勤记录完成"); }catch (Exception ex){ ex.printStackTrace(); log.error(dataSourceEntity.getSystemDescribe()+":"+ex.getMessage()); }finally { SpObserver.setDBtoInstance(); } } } } private class FormThread implements Runnable { final DataSourceEntity dataSourceEntity; final ThreadPoolTaskExecutor threadPoolExecutor; public FormThread(DataSourceEntity dataSourceEntity, ThreadPoolTaskExecutor threadPoolTaskExecutor) { this.dataSourceEntity = dataSourceEntity; this.threadPoolExecutor=threadPoolTaskExecutor; } @Override public void run() { try { String dbid = dataSourceEntity.getDbId() + ""; log.info(dataSourceEntity.getSystemDescribe() + "开始执行"); SpObserver.setDBtoInstance("_"+dbid); BaseService baseService= (BaseService) FactoryBean.getBean("BaseService"); baseService.getSimpleJdbcTemplate().execute("exec p180251"); log.info(dataSourceEntity.getSystemDescribe() + "-更新考勤记录完成"); } catch (Exception ex){ log.error(dataSourceEntity.getSystemDescribe()+":"+ex.getMessage()); }finally { SpObserver.setDBtoInstance(); } } } }