AiMarkingJob.java 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package cn.com.qmth.am.task;
  2. import java.util.List;
  3. import org.apache.commons.collections4.CollectionUtils;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.scheduling.annotation.Scheduled;
  6. import org.springframework.stereotype.Service;
  7. import com.qmth.boot.core.concurrent.service.ConcurrentService;
  8. import cn.com.qmth.am.config.SysProperty;
  9. import cn.com.qmth.am.entity.StudentScoreEntity;
  10. import cn.com.qmth.am.enums.LockType;
  11. import cn.com.qmth.am.service.StudentScoreService;
  12. @Service
  13. public class AiMarkingJob {
  14. public static Boolean enable=null;
  15. @Autowired
  16. private StudentScoreService studentScoreService;
  17. @Autowired
  18. private ConcurrentService concurrentService;
  19. @Autowired
  20. private SysProperty sysProperty;
  21. @Scheduled(fixedDelay = 5 * 1000, initialDelay = 20 * 1000)
  22. public void doJob() {
  23. if(enable==null) {
  24. enable=sysProperty.getMarkingTaskEnable();
  25. }
  26. if(!enable) {
  27. return;
  28. }
  29. List<StudentScoreEntity> scores = studentScoreService.findAllToAiMarking();
  30. if (CollectionUtils.isEmpty(scores)) {
  31. return;
  32. }
  33. boolean lock = concurrentService.getReadWriteLock(LockType.AI_MARKING.name()).writeLock().tryLock();
  34. try {
  35. if (!lock) {
  36. return;
  37. }
  38. this.dispose(scores);
  39. } finally {
  40. if (lock) {
  41. concurrentService.getReadWriteLock(LockType.AI_MARKING.name()).writeLock().unlock();
  42. }
  43. }
  44. }
  45. private void dispose(List<StudentScoreEntity> scores) {
  46. for (StudentScoreEntity score : scores) {
  47. if(!enable) {
  48. return;
  49. }
  50. studentScoreService.aiMarking(score);
  51. }
  52. }
  53. }