Răsfoiți Sursa

动态定时任务

ouyang 3 ani în urmă
părinte
comite
b4d54d2683

+ 25 - 33
src/main/java/com/izouma/nineth/service/scheduledTask/CloseSubscribedTask.java

@@ -31,38 +31,30 @@ public class CloseSubscribedTask implements SchedulingConfigurer {
 
     @Override
     public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
-        taskRegistrar.addTriggerTask(() -> {
-            b:
-            if (Collections.isEmpty(collections)){
-                return;
-            }else {
-                for (Collection collection : collections) {
-                    if (collection.getEndTime().isBefore(LocalDateTime.now()) && collection.getEndTime().isBefore(LocalDateTime.now().minusSeconds(2))){
-                        collectionRepo.setState(collection.getId(),"CLOSESUBSCRIBE");
-                        collections.remove(0);
-                        break b;
-                    }
-                }
-            }
-        }, triggerContext -> {
-            if (Collections.isEmpty(collections)){
-                //首先获取当前时间
-                LocalDateTime startTime = LocalDateTime.now();
-                //获取四分钟以后的时间
-                LocalDateTime endTime = startTime.plusMinutes(4);
-                //根据开始时间和结束时间去数据库把这两个时间段中 符合的预约发布时间中获取藏品
-                List<Collection> collectionList = collectionRepo.findAllByStartTimeLessThanEqualAndEndTimeGreaterThanEqual(startTime, endTime);
-                //判断获取的藏品列表是否为空
-                if (Collections.isEmpty(collectionList)) {
-//                    return new CronTrigger("0 0/10 * * * ?").nextExecutionTime(triggerContext);
-                    //藏品列表为空 则设置下次的时间查询为结束时间的前30秒
-                    return Date.from(endTime.minusSeconds(5).atZone(ZoneId.systemDefault()).toInstant());
-                }
-                //藏品列表不为空则将取出来的藏品列表根据预约发布的时间排序放入等待的藏品列表
-                collections = collectionList.stream().sorted(Comparator.comparing(Collection::getEndTime)).collect(Collectors.toList());
-            }
-            //取第一个藏品的预约结束时间的前10秒做下次定时任务的时间
-            return Date.from(collections.get(0).getEndTime().minusSeconds(2).atZone(ZoneId.systemDefault()).toInstant());
-        });
+//        taskRegistrar.addTriggerTask(() -> {
+//            b:
+//            if (Collections.isEmpty(collections)){
+//                return;
+//            }else {
+//                for (Collection collection : collections) {
+//                    if (collection.getEndTime().isBefore(LocalDateTime.now()) && collection.getEndTime().isBefore(LocalDateTime.now().minusSeconds(2))){
+//                        collectionRepo.setState(collection.getId(),"CLOSESUBSCRIBE");
+//                        collections.remove(0);
+//                        break b;
+//                    }
+//                }
+//            }
+//        }, triggerContext -> {
+//            if (Collections.isEmpty(collections)){
+//                LocalDateTime startTime = LocalDateTime.now();
+//                LocalDateTime endTime = startTime.plusMinutes(4);
+//                List<Collection> collectionList = collectionRepo.findAllByStartTimeLessThanEqualAndEndTimeGreaterThanEqual(startTime, endTime);
+//                if (Collections.isEmpty(collectionList)) {
+//                    return Date.from(endTime.minusSeconds(5).atZone(ZoneId.systemDefault()).toInstant());
+//                }
+//                collections = collectionList.stream().sorted(Comparator.comparing(Collection::getEndTime)).collect(Collectors.toList());
+//            }
+//            return Date.from(collections.get(0).getEndTime().minusSeconds(2).atZone(ZoneId.systemDefault()).toInstant());
+//        });
     }
 }

+ 31 - 57
src/main/java/com/izouma/nineth/service/scheduledTask/OpenSubscribedTask.java

@@ -31,65 +31,39 @@ public class OpenSubscribedTask implements SchedulingConfigurer {
 
     @Override
     public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
-        taskRegistrar.addTriggerTask(() -> {
-            b:
-            if (Collections.isEmpty(collections)){
-                return;
-            }else {
-                for (Collection collection : collections) {
-                    if (collection.getStartTime().isBefore(LocalDateTime.now()) && collection.getStartTime().isBefore(LocalDateTime.now().minusSeconds(2))){
-                        collectionRepo.setState(collection.getId(),"OPENSUBSCRIBE");
-                        collections.remove(0);
-                        break b;
-                    }
-                }
-            }
-//            //获取当前时间
-//            LocalDateTime startTime = LocalDateTime.now().minusMinutes(1);
-//            //获取四分钟以后的时间
-//            LocalDateTime endTime = startTime.plusMinutes(4);
-//            //根据开始时间和结束时间去数据库把这两个时间段中 符合的预约发布时间中获取藏品
-//            List<Collection> collectionList = collectionRepo.selectOpenSubscribe(startTime, endTime);
-//            //判断获取的藏品列表是否为空
-//            if (Collections.isEmpty(collectionList)){
+//        taskRegistrar.addTriggerTask(() -> {
+//            b:
+//            if (Collections.isEmpty(collections)){
 //                return;
-//            }
-//            collectionList.forEach(collection -> {
-//                if (collection.getStartTime().isBefore(LocalDateTime.now()) || collection.getEndTime().isAfter(LocalDateTime.now())){
+//            }else {
+//                for (Collection collection : collections) {
+//                    if (collection.getStartTime().isBefore(LocalDateTime.now()) && collection.getStartTime().isBefore(LocalDateTime.now().minusSeconds(2))){
+//                        collectionRepo.setState(collection.getId(),"OPENSUBSCRIBE");
+//                        collections.remove(0);
+//                        break b;
+//                    }
 //                }
-////                if (collection.getEndTime().isBefore(LocalDateTime.now()) && collection.getPublishTime().isAfter(LocalDateTime.now())){
-////                    collectionRepo.setState(collection.getId(),"CLOSESUBSCRIBE");
-////                }
-////                if (collection.getPublishTime().isBefore(LocalDateTime.now()) && collection.getPurchaseTime().isAfter(LocalDateTime.now())){
-////                    collectionRepo.setState(collection.getId(),"PUBLISH");
-////                }
-////                if (collection.getPurchaseTime().isBefore(LocalDateTime.now())){
-////                    collectionRepo.setState(collection.getId(),"PURCHASE");
-////                }
-//                if (!Collections.isEmpty(collections)){
-//                    collections.remove(0);
+//            }
+//        }, triggerContext -> {
+//            if (Collections.isEmpty(collections)){
+//                //首先获取当前时间
+////                LocalDateTime startTime = LocalDateTime.now().minusMinutes(1);
+//                LocalDateTime startTime = LocalDateTime.now();
+//                //获取四分钟以后的时间
+//                LocalDateTime endTime = startTime.plusMinutes(4);
+//                //根据开始时间和结束时间去数据库把这两个时间段中 符合的预约发布时间中获取藏品
+//                List<Collection> collectionList = collectionRepo.findAllByStartTimeGreaterThanEqual(startTime);
+//                //判断获取的藏品列表是否为空
+//                if (Collections.isEmpty(collectionList)) {
+////                    return new CronTrigger("0 0/10 * * * ?").nextExecutionTime(triggerContext);
+//                    //藏品列表为空 则设置下次的时间查询为结束时间的前30秒  前10秒  前10
+//                    return Date.from(endTime.minusSeconds(5).atZone(ZoneId.systemDefault()).toInstant());
 //                }
-//            });
-        }, triggerContext -> {
-            if (Collections.isEmpty(collections)){
-                //首先获取当前时间
-//                LocalDateTime startTime = LocalDateTime.now().minusMinutes(1);
-                LocalDateTime startTime = LocalDateTime.now();
-                //获取四分钟以后的时间
-                LocalDateTime endTime = startTime.plusMinutes(4);
-                //根据开始时间和结束时间去数据库把这两个时间段中 符合的预约发布时间中获取藏品
-                List<Collection> collectionList = collectionRepo.findAllByStartTimeGreaterThanEqual(startTime);
-                //判断获取的藏品列表是否为空
-                if (Collections.isEmpty(collectionList)) {
-//                    return new CronTrigger("0 0/10 * * * ?").nextExecutionTime(triggerContext);
-                    //藏品列表为空 则设置下次的时间查询为结束时间的前30秒  前10秒  前10
-                    return Date.from(endTime.minusSeconds(5).atZone(ZoneId.systemDefault()).toInstant());
-                }
-                //藏品列表不为空则将取出来的藏品列表根据预约发布的时间排序放入等待的藏品列表
-                collections = collectionList.stream().sorted(Comparator.comparing(Collection::getStartTime)).collect(Collectors.toList());
-            }
-            //取第一个藏品的预约发布时间的前10秒做下次定时任务的时间  换成五秒  两秒
-            return Date.from(collections.get(0).getStartTime().minusSeconds(2).atZone(ZoneId.systemDefault()).toInstant());
-        });
+//                //藏品列表不为空则将取出来的藏品列表根据预约发布的时间排序放入等待的藏品列表
+//                collections = collectionList.stream().sorted(Comparator.comparing(Collection::getStartTime)).collect(Collectors.toList());
+//            }
+//            //取第一个藏品的预约发布时间的前10秒做下次定时任务的时间  换成五秒  两秒
+//            return Date.from(collections.get(0).getStartTime().minusSeconds(2).atZone(ZoneId.systemDefault()).toInstant());
+//        });
     }
 }

+ 25 - 33
src/main/java/com/izouma/nineth/service/scheduledTask/PublishSubscribedTask.java

@@ -31,38 +31,30 @@ public class PublishSubscribedTask implements SchedulingConfigurer {
 
     @Override
     public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
-        taskRegistrar.addTriggerTask(() -> {
-            b:
-            if (Collections.isEmpty(collections)){
-                return;
-            }else {
-                for (Collection collection : collections) {
-                    if (collection.getPublishTime().isBefore(LocalDateTime.now()) && collection.getPublishTime().isBefore(LocalDateTime.now().minusSeconds(2))){
-                        collectionRepo.setState(collection.getId(),"PUBLISH");
-                        collections.remove(0);
-                        break b;
-                    }
-                }
-            }
-        }, triggerContext -> {
-            if (Collections.isEmpty(collections)){
-                //首先获取当前时间
-                LocalDateTime startTime = LocalDateTime.now();
-                //获取四分钟以后的时间
-                LocalDateTime endTime = startTime.plusMinutes(4);
-                //根据开始时间和结束时间去数据库把这两个时间段中 符合的预约发布时间中获取藏品
-                List<Collection> collectionList = collectionRepo.findAllByEndTimeLessThanEqualAndPublishTimeGreaterThanEqual(startTime, endTime);
-                //判断获取的藏品列表是否为空
-                if (Collections.isEmpty(collectionList)) {
-//                    return new CronTrigger("0 0/10 * * * ?").nextExecutionTime(triggerContext);
-                    //藏品列表为空 则设置下次的时间查询为结束时间的前30秒
-                    return Date.from(endTime.minusSeconds(5).atZone(ZoneId.systemDefault()).toInstant());
-                }
-                //藏品列表不为空则将取出来的藏品列表根据预约发布的时间排序放入等待的藏品列表
-                collections = collectionList.stream().sorted(Comparator.comparing(Collection::getPublishTime)).collect(Collectors.toList());
-            }
-            //取第一个藏品的预约发布时间的前10秒做下次定时任务的时间
-            return Date.from(collections.get(0).getPublishTime().minusSeconds(2).atZone(ZoneId.systemDefault()).toInstant());
-        });
+//        taskRegistrar.addTriggerTask(() -> {
+//            b:
+//            if (Collections.isEmpty(collections)){
+//                return;
+//            }else {
+//                for (Collection collection : collections) {
+//                    if (collection.getPublishTime().isBefore(LocalDateTime.now()) && collection.getPublishTime().isBefore(LocalDateTime.now().minusSeconds(2))){
+//                        collectionRepo.setState(collection.getId(),"PUBLISH");
+//                        collections.remove(0);
+//                        break b;
+//                    }
+//                }
+//            }
+//        }, triggerContext -> {
+//            if (Collections.isEmpty(collections)){
+//                LocalDateTime startTime = LocalDateTime.now();
+//                LocalDateTime endTime = startTime.plusMinutes(4);
+//                List<Collection> collectionList = collectionRepo.findAllByEndTimeLessThanEqualAndPublishTimeGreaterThanEqual(startTime, endTime);
+//                if (Collections.isEmpty(collectionList)) {
+//                    return Date.from(endTime.minusSeconds(5).atZone(ZoneId.systemDefault()).toInstant());
+//                }
+//                collections = collectionList.stream().sorted(Comparator.comparing(Collection::getPublishTime)).collect(Collectors.toList());
+//            }
+//            return Date.from(collections.get(0).getPublishTime().minusSeconds(2).atZone(ZoneId.systemDefault()).toInstant());
+//        });
     }
 }

+ 25 - 33
src/main/java/com/izouma/nineth/service/scheduledTask/PurchaseSubscribedTask.java

@@ -31,38 +31,30 @@ public class PurchaseSubscribedTask implements SchedulingConfigurer {
 
     @Override
     public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
-        taskRegistrar.addTriggerTask(() -> {
-            b:
-            if (Collections.isEmpty(collections)){
-                return;
-            }else {
-                for (Collection collection : collections) {
-                    if (collection.getPurchaseTime().isBefore(LocalDateTime.now()) && collection.getPurchaseTime().isBefore(LocalDateTime.now().minusSeconds(2))){
-                        collectionRepo.setState(collection.getId(),"PURCHASE");
-                        collections.remove(0);
-                        break b;
-                    }
-                }
-            }
-        }, triggerContext -> {
-            if (Collections.isEmpty(collections)){
-                //首先获取当前时间
-                LocalDateTime startTime = LocalDateTime.now();
-                //获取四分钟以后的时间
-                LocalDateTime endTime = startTime.plusMinutes(4);
-                //根据开始时间和结束时间去数据库把这两个时间段中 符合的预约发布时间中获取藏品
-                List<Collection> collectionList = collectionRepo.findAllByPublishTimeLessThanEqualAndPurchaseTimeGreaterThanEqual(startTime, endTime);
-                //判断获取的藏品列表是否为空
-                if (Collections.isEmpty(collectionList)) {
-//                    return new CronTrigger("0 0/10 * * * ?").nextExecutionTime(triggerContext);
-                    //藏品列表为空 则设置下次的时间查询为结束时间的前30秒
-                    return Date.from(endTime.minusSeconds(5).atZone(ZoneId.systemDefault()).toInstant());
-                }
-                //藏品列表不为空则将取出来的藏品列表根据预约发布的时间排序放入等待的藏品列表
-                collections = collectionList.stream().sorted(Comparator.comparing(Collection::getPurchaseTime)).collect(Collectors.toList());
-            }
-            //取第一个藏品的预约发布时间的前10秒做下次定时任务的时间
-            return Date.from(collections.get(0).getPurchaseTime().minusSeconds(2).atZone(ZoneId.systemDefault()).toInstant());
-        });
+//        taskRegistrar.addTriggerTask(() -> {
+//            b:
+//            if (Collections.isEmpty(collections)){
+//                return;
+//            }else {
+//                for (Collection collection : collections) {
+//                    if (collection.getPurchaseTime().isBefore(LocalDateTime.now()) && collection.getPurchaseTime().isBefore(LocalDateTime.now().minusSeconds(2))){
+//                        collectionRepo.setState(collection.getId(),"PURCHASE");
+//                        collections.remove(0);
+//                        break b;
+//                    }
+//                }
+//            }
+//        }, triggerContext -> {
+//            if (Collections.isEmpty(collections)){
+//                LocalDateTime startTime = LocalDateTime.now();
+//                LocalDateTime endTime = startTime.plusMinutes(4);
+//                List<Collection> collectionList = collectionRepo.findAllByPublishTimeLessThanEqualAndPurchaseTimeGreaterThanEqual(startTime, endTime);
+//                if (Collections.isEmpty(collectionList)) {
+//                    return Date.from(endTime.minusSeconds(5).atZone(ZoneId.systemDefault()).toInstant());
+//                }
+//                collections = collectionList.stream().sorted(Comparator.comparing(Collection::getPurchaseTime)).collect(Collectors.toList());
+//            }
+//            return Date.from(collections.get(0).getPurchaseTime().minusSeconds(2).atZone(ZoneId.systemDefault()).toInstant());
+//        });
     }
 }