1 package com.cfets.ts.u.limitapi.job; 2 import java.text.SimpleDateFormat; 3 import java.util.Date; 4 import java.util.concurrent.Executors; 5 import java.util.concurrent.ScheduledExecutorService; 6 import java.util.concurrent.TimeUnit; 7 8 import org.junit.Before; 9 10 import jodd.datetime.JDateTime; 11 import junit.runner.BaseTestRunner; 12 13 import com.cfets.cwap.s.util.db.JdbcManager; 14 import com.cfets.cwap.s.util.db.JdbcProperty; 15 import com.cfets.ts.s.authentication.AuthenticationHelper; 16 import com.cfets.ts.s.log.TsLogger; 17 import com.cfets.ts.u.limitapi.LimitApiHelper; 18 import com.cfets.ts.u.limitapi.service.impl.LimitServiceImpl; 19 import com.cfets.ts.u.limitapi.util.DateUtils; 20 import com.cfets.ts.u.limitapimgmt.LimitapimgmtProvider; 21 import com.cfets.ts.u.limitapimgmt.entity.vo.FlowControlVO; 22 23 public class LimitApilLinsenerJob{ 24 25 private static final TsLogger logger = TsLogger.getLogger(LimitServiceImpl.class); 26 public static final int TASK_START_TIME = 1000; 27 public static final int TASK_END_TIME = 5000; 28 /* 29 public static void main(String[] args) { 30 ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor(); 31 TimerTask timerTask = new TimerTask(LimitApilLinsenerJob.TASK_END_TIME); 32 logger.debug("起始时间:", new SimpleDateFormat("HH:mm").format(new Date())); 33 //延时1s后,按3s的周期执行任务 34 timer.scheduleAtFixedRate(timerTask, LimitApilLinsenerJob.TASK_START_TIME, LimitApilLinsenerJob.TASK_END_TIME, TimeUnit.MILLISECONDS); 35 }*/ 36 public static class TimerTask implements Runnable{ 37 private final int sleepTime; 38 private final SimpleDateFormat dateFormat; 39 public TimerTask(int sleepTime){ 40 this.sleepTime = sleepTime; 41 dateFormat = new SimpleDateFormat("HH:mm"); 42 } 43 @Override 44 public void run() { 45 logger.debug("任务开始,当前时间:" + dateFormat.format(new Date())); 46 try { 47 FlowControlVO flowControl = LimitapimgmtProvider.getLimitapimgmtProvider().findtimeAndthreshold(); 48 logger.debug("start time is: " + flowControl.getStarttime() + "|end time is:" +flowControl.getEndtime()); 49 // 系统登录 50 JDateTime startTime = DateUtils.parseDate(flowControl.getStarttime(), LimitApiHelper.TIME_INTETERVAL_PARTTERN); 51 // 系统登录的结束时间 52 JDateTime endTime = DateUtils.parseDate(flowControl.getEndtime(), LimitApiHelper.TIME_INTETERVAL_PARTTERN); 53 // 当前时间 54 JDateTime currTime = new JDateTime(); 55 logger.debug("currTime" + currTime); 56 if (!(currTime.isAfter(startTime) && currTime.isBefore(endTime))) { 57 logger.debug("模拟任务执行....."); 58 logger.debug("自动登出-----"); 59 //此处先做删除登录用户信息,然后保存到用户登录历史信息表 authentication提供删除和保存方法 60 } 61 Thread.sleep(sleepTime); 62 } catch (Exception e) { 63 logger.debug("This is e {}", e); 64 } 65 logger.debug("任务结束,当前时间:" + dateFormat.format(new Date())); 66 } 67 68 } 69 }
时间: 2024-12-28 13:16:47