QuestionImportJob.java 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package cn.com.qmth.am.task;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.scheduling.annotation.Scheduled;
  4. import org.springframework.stereotype.Service;
  5. import com.qmth.boot.core.concurrent.service.ConcurrentService;
  6. import cn.com.qmth.am.enums.LockType;
  7. import cn.com.qmth.am.service.QuestionService;
  8. @Service
  9. public class QuestionImportJob {
  10. @Autowired
  11. private ConcurrentService concurrentService;
  12. @Autowired
  13. private QuestionService questionService;
  14. @Scheduled(fixedDelay = 5 * 1000, initialDelay = 5 * 1000)
  15. public void doJob() {
  16. boolean lock = concurrentService.getReadWriteLock(LockType.QUESTION_IMPORT.name()).writeLock().tryLock();
  17. try {
  18. if (!lock) {
  19. return;
  20. }
  21. questionService.importQuestion();
  22. } finally {
  23. if (lock) {
  24. concurrentService.getReadWriteLock(LockType.QUESTION_IMPORT.name()).writeLock().unlock();
  25. }
  26. }
  27. }
  28. }