|
@@ -1,162 +1,162 @@
|
|
|
-package com.ff.apimngr.task;
|
|
|
-
|
|
|
-
|
|
|
-import java.text.ParseException;
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
-import java.util.Calendar;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-import javax.annotation.Resource;
|
|
|
-
|
|
|
-import org.springframework.context.annotation.Configuration;
|
|
|
-import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
-import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
-import org.springframework.scheduling.annotation.Scheduled;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.ff.apimngr.controller.DemoController;
|
|
|
-import com.ff.apimngr.entity.App;
|
|
|
-import com.ff.apimngr.entity.AppApi;
|
|
|
-import com.ff.apimngr.service.ApiLogService;
|
|
|
-import com.ff.apimngr.service.AppApiService;
|
|
|
-import com.ff.apimngr.service.AppService;
|
|
|
-import com.ff.apimngr.util.Config;
|
|
|
-import com.ff.apimngr.util.HttpUtils;
|
|
|
-
|
|
|
-@Configuration
|
|
|
-@EnableScheduling
|
|
|
-public class ScheduleTask {
|
|
|
-
|
|
|
- @Resource
|
|
|
- ApiLogService apiLogService;
|
|
|
-
|
|
|
- @Resource
|
|
|
- AppService appService;
|
|
|
-
|
|
|
- @Resource
|
|
|
- private StringRedisTemplate stringRedisTemplate;
|
|
|
-
|
|
|
- @Resource
|
|
|
- AppApiService appApiService;
|
|
|
- /**
|
|
|
- * 更新日志表--日表
|
|
|
- * 每天凌晨1点执行一次
|
|
|
- */
|
|
|
- @Scheduled(cron = "0 0 1 * * ?")
|
|
|
- //@Scheduled(fixedRate = 300000) // 5分钟执行一次
|
|
|
- public void updateDayApiDayLog() throws ParseException {
|
|
|
- System.out.println("===日表定时任务开始...");
|
|
|
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- Date date = new Date();
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- calendar.setTime(date);
|
|
|
- calendar.add(Calendar.DAY_OF_MONTH, -1);
|
|
|
- date = (Date) calendar.getTime();
|
|
|
- String day = df.format(date);
|
|
|
- System.out.println("===日表定时任务日期..." + day);
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
- Date beginDay = sdf.parse(day + " 00:00:00");
|
|
|
- Date endDay = sdf.parse(day + " 23:59:59");
|
|
|
- long begin = beginDay.getTime();
|
|
|
- long end = endDay.getTime();
|
|
|
- apiLogService.insertToDayTable(begin, end);
|
|
|
-
|
|
|
- System.out.println("===日表定时任务结束...");
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 更新日志表--时表
|
|
|
- * 每天01点30分执行
|
|
|
- */
|
|
|
-// @Scheduled(cron = "0 30 01 * * ?")
|
|
|
- @Scheduled(fixedRate = 3600000) // 1小时执行一次
|
|
|
- public void updateDayApiHourLog() throws ParseException {
|
|
|
- System.out.println("===时表定时任务开始...");
|
|
|
- Date today = new Date();
|
|
|
- SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- String day = formatDate.format(today);
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
- Date beginDay = sdf.parse(day + " 00:00:00");
|
|
|
- long begin = beginDay.getTime();
|
|
|
- long end = begin + 3600000;
|
|
|
- for (int i = 0; i < 24; i++) {
|
|
|
- apiLogService.insertToHourTable(begin, end);
|
|
|
- begin += 3600000;
|
|
|
- end = begin + 3600000;
|
|
|
- }
|
|
|
- System.out.println("===时表定时任务结束...");
|
|
|
- }
|
|
|
-
|
|
|
-// @Scheduled(fixedRate = 86400000) // 1天执行一次
|
|
|
- public void getAccessToken() {
|
|
|
- //fixme, 使用 “演示应用” appid=11来测试
|
|
|
- try
|
|
|
- {
|
|
|
- App app = appService.findAppById(45);
|
|
|
- Map<String, Object> param = new HashMap<>();
|
|
|
- param.put("grant_type", "client_credentials");
|
|
|
- param.put("client_id", app.getApiKey());
|
|
|
- param.put("client_secret", app.getSecretKey());
|
|
|
- String url = String.format("%s/oauth/2.0/token", Config.BASE_HOST) ;
|
|
|
- JSONObject json = new JSONObject(param);
|
|
|
- String PostString = HttpUtils.doPostByJson(url, null, json);
|
|
|
- System.out.println(PostString);
|
|
|
- JSONObject object = JSONObject.parseObject(PostString);
|
|
|
-
|
|
|
- String accessToken = object.getString("access_token");
|
|
|
- System.out.println(accessToken);
|
|
|
- DemoController.accessToken = accessToken;
|
|
|
- }
|
|
|
- catch (Exception ee)
|
|
|
- {
|
|
|
- ee.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Scheduled(fixedRate = 300000) // 5分钟执行一次
|
|
|
- public void updateAppApiUsed() {
|
|
|
- //每5分钟获取一次调用已使用量redis数据,同步到mysql
|
|
|
- System.out.println("定时任务开启" + System.currentTimeMillis());
|
|
|
- //获取有多少个key需要同步
|
|
|
- List<AppApi> list = appApiService.listAll();
|
|
|
- if(null != list && list.size() > 0) {
|
|
|
- System.out.println("===total:" + list.size() + "need read value from redis!");
|
|
|
- for(AppApi appApi : list) {
|
|
|
- String key = "CALL_LIMIT_S:" + appApi.getApp().getId() + ":" + appApi.getApi().getId();
|
|
|
- System.out.println("key: " + key);
|
|
|
- String value = stringRedisTemplate.opsForValue().get(key);
|
|
|
- System.out.println("value: " + value);
|
|
|
- appApi.setUsed(value);
|
|
|
- appApiService.updateAppApi(appApi);
|
|
|
- }
|
|
|
- }
|
|
|
- System.out.println("定时任务结束" + System.currentTimeMillis());
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 更新到期日期的app状态
|
|
|
- * 每天01点10分执行
|
|
|
- */
|
|
|
-// @Scheduled(cron = "0 1 0 * * ?")
|
|
|
- //@Scheduled(fixedRate = 300000)
|
|
|
- public void updateAppExpireStatus() throws ParseException {
|
|
|
- System.out.println("===更新到期日期的app状态 定时任务开始...");
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- calendar.add(Calendar.DATE, -1);
|
|
|
- Date yesterday = calendar.getTime();
|
|
|
- SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- String day = formatDate.format(yesterday);
|
|
|
- List<App> apps = appService.listAppsByExpireDate(day);
|
|
|
- if (null != apps && apps.size() > 0) {
|
|
|
- for (App app : apps) {
|
|
|
- app.setStatus(0);
|
|
|
- appService.updateApp(app);
|
|
|
- }
|
|
|
- }
|
|
|
- System.out.println("===更新到期日期的app状态 定时任务结束...");
|
|
|
- }
|
|
|
-}
|
|
|
+//package com.ff.apimngr.task;
|
|
|
+//
|
|
|
+//
|
|
|
+//import java.text.ParseException;
|
|
|
+//import java.text.SimpleDateFormat;
|
|
|
+//import java.util.Calendar;
|
|
|
+//import java.util.Date;
|
|
|
+//import java.util.HashMap;
|
|
|
+//import java.util.List;
|
|
|
+//import java.util.Map;
|
|
|
+//
|
|
|
+//import javax.annotation.Resource;
|
|
|
+//
|
|
|
+//import org.springframework.context.annotation.Configuration;
|
|
|
+//import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
+//import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
+//import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+//
|
|
|
+//import com.alibaba.fastjson.JSONObject;
|
|
|
+//import com.ff.apimngr.controller.DemoController;
|
|
|
+//import com.ff.apimngr.entity.App;
|
|
|
+//import com.ff.apimngr.entity.AppApi;
|
|
|
+//import com.ff.apimngr.service.ApiLogService;
|
|
|
+//import com.ff.apimngr.service.AppApiService;
|
|
|
+//import com.ff.apimngr.service.AppService;
|
|
|
+//import com.ff.apimngr.util.Config;
|
|
|
+//import com.ff.apimngr.util.HttpUtils;
|
|
|
+//
|
|
|
+//@Configuration
|
|
|
+//@EnableScheduling
|
|
|
+//public class ScheduleTask {
|
|
|
+//
|
|
|
+// @Resource
|
|
|
+// ApiLogService apiLogService;
|
|
|
+//
|
|
|
+// @Resource
|
|
|
+// AppService appService;
|
|
|
+//
|
|
|
+// @Resource
|
|
|
+// private StringRedisTemplate stringRedisTemplate;
|
|
|
+//
|
|
|
+// @Resource
|
|
|
+// AppApiService appApiService;
|
|
|
+// /**
|
|
|
+// * 更新日志表--日表
|
|
|
+// * 每天凌晨1点执行一次
|
|
|
+// */
|
|
|
+// @Scheduled(cron = "0 0 1 * * ?")
|
|
|
+// //@Scheduled(fixedRate = 300000) // 5分钟执行一次
|
|
|
+// public void updateDayApiDayLog() throws ParseException {
|
|
|
+// System.out.println("===日表定时任务开始...");
|
|
|
+// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+// Date date = new Date();
|
|
|
+// Calendar calendar = Calendar.getInstance();
|
|
|
+// calendar.setTime(date);
|
|
|
+// calendar.add(Calendar.DAY_OF_MONTH, -1);
|
|
|
+// date = (Date) calendar.getTime();
|
|
|
+// String day = df.format(date);
|
|
|
+// System.out.println("===日表定时任务日期..." + day);
|
|
|
+// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+// Date beginDay = sdf.parse(day + " 00:00:00");
|
|
|
+// Date endDay = sdf.parse(day + " 23:59:59");
|
|
|
+// long begin = beginDay.getTime();
|
|
|
+// long end = endDay.getTime();
|
|
|
+// apiLogService.insertToDayTable(begin, end);
|
|
|
+//
|
|
|
+// System.out.println("===日表定时任务结束...");
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 更新日志表--时表
|
|
|
+// * 每天01点30分执行
|
|
|
+// */
|
|
|
+//// @Scheduled(cron = "0 30 01 * * ?")
|
|
|
+// @Scheduled(fixedRate = 3600000) // 1小时执行一次
|
|
|
+// public void updateDayApiHourLog() throws ParseException {
|
|
|
+// System.out.println("===时表定时任务开始...");
|
|
|
+// Date today = new Date();
|
|
|
+// SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+// String day = formatDate.format(today);
|
|
|
+// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+// Date beginDay = sdf.parse(day + " 00:00:00");
|
|
|
+// long begin = beginDay.getTime();
|
|
|
+// long end = begin + 3600000;
|
|
|
+// for (int i = 0; i < 24; i++) {
|
|
|
+// apiLogService.insertToHourTable(begin, end);
|
|
|
+// begin += 3600000;
|
|
|
+// end = begin + 3600000;
|
|
|
+// }
|
|
|
+// System.out.println("===时表定时任务结束...");
|
|
|
+// }
|
|
|
+//
|
|
|
+//// @Scheduled(fixedRate = 86400000) // 1天执行一次
|
|
|
+// public void getAccessToken() {
|
|
|
+// //fixme, 使用 “演示应用” appid=11来测试
|
|
|
+// try
|
|
|
+// {
|
|
|
+// App app = appService.findAppById(45);
|
|
|
+// Map<String, Object> param = new HashMap<>();
|
|
|
+// param.put("grant_type", "client_credentials");
|
|
|
+// param.put("client_id", app.getApiKey());
|
|
|
+// param.put("client_secret", app.getSecretKey());
|
|
|
+// String url = String.format("%s/oauth/2.0/token", Config.BASE_HOST) ;
|
|
|
+// JSONObject json = new JSONObject(param);
|
|
|
+// String PostString = HttpUtils.doPostByJson(url, null, json);
|
|
|
+// System.out.println(PostString);
|
|
|
+// JSONObject object = JSONObject.parseObject(PostString);
|
|
|
+//
|
|
|
+// String accessToken = object.getString("access_token");
|
|
|
+// System.out.println(accessToken);
|
|
|
+// DemoController.accessToken = accessToken;
|
|
|
+// }
|
|
|
+// catch (Exception ee)
|
|
|
+// {
|
|
|
+// ee.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Scheduled(fixedRate = 300000) // 5分钟执行一次
|
|
|
+// public void updateAppApiUsed() {
|
|
|
+// //每5分钟获取一次调用已使用量redis数据,同步到mysql
|
|
|
+// System.out.println("定时任务开启" + System.currentTimeMillis());
|
|
|
+// //获取有多少个key需要同步
|
|
|
+// List<AppApi> list = appApiService.listAll();
|
|
|
+// if(null != list && list.size() > 0) {
|
|
|
+// System.out.println("===total:" + list.size() + "need read value from redis!");
|
|
|
+// for(AppApi appApi : list) {
|
|
|
+// String key = "CALL_LIMIT_S:" + appApi.getApp().getId() + ":" + appApi.getApi().getId();
|
|
|
+// System.out.println("key: " + key);
|
|
|
+// String value = stringRedisTemplate.opsForValue().get(key);
|
|
|
+// System.out.println("value: " + value);
|
|
|
+// appApi.setUsed(value);
|
|
|
+// appApiService.updateAppApi(appApi);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// System.out.println("定时任务结束" + System.currentTimeMillis());
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 更新到期日期的app状态
|
|
|
+// * 每天01点10分执行
|
|
|
+// */
|
|
|
+//// @Scheduled(cron = "0 1 0 * * ?")
|
|
|
+// //@Scheduled(fixedRate = 300000)
|
|
|
+// public void updateAppExpireStatus() throws ParseException {
|
|
|
+// System.out.println("===更新到期日期的app状态 定时任务开始...");
|
|
|
+// Calendar calendar = Calendar.getInstance();
|
|
|
+// calendar.add(Calendar.DATE, -1);
|
|
|
+// Date yesterday = calendar.getTime();
|
|
|
+// SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+// String day = formatDate.format(yesterday);
|
|
|
+// List<App> apps = appService.listAppsByExpireDate(day);
|
|
|
+// if (null != apps && apps.size() > 0) {
|
|
|
+// for (App app : apps) {
|
|
|
+// app.setStatus(0);
|
|
|
+// appService.updateApp(app);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// System.out.println("===更新到期日期的app状态 定时任务结束...");
|
|
|
+// }
|
|
|
+//}
|