FaceIdenSchedule.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package com.iden.bms.schedule;
  2. import com.iden.bms.face.FaceIdenService;
  3. import com.iden.common.cache.RedisKeyConstant;
  4. import com.iden.common.cache.RedisUtil;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.scheduling.annotation.Async;
  7. import org.springframework.scheduling.annotation.EnableAsync;
  8. import org.springframework.scheduling.annotation.EnableScheduling;
  9. import org.springframework.scheduling.annotation.Scheduled;
  10. import javax.annotation.Resource;
  11. import java.util.UUID;
  12. /**
  13. * 定时读取目录下图片,调用人脸识别引擎,把特征码入库
  14. */
  15. @Configuration
  16. @EnableScheduling
  17. @EnableAsync
  18. public class FaceIdenSchedule {
  19. @Resource
  20. private FaceIdenService faceIdenService;
  21. @Resource
  22. private RedisUtil redisUtil;
  23. /**
  24. * 处理摄像头上传的图像
  25. */
  26. @Async
  27. @Scheduled(cron = "0 0/5 * * * ?")
  28. public void handleCameraImage() {
  29. String key = RedisKeyConstant.HANDLE_CAMERA_IMAGE;
  30. String requestId = UUID.randomUUID().toString();
  31. boolean result = redisUtil.tryLock(key,requestId,10 * 60);
  32. try {
  33. if (result) {
  34. faceIdenService.handleCameraImage();
  35. }
  36. } catch (Exception e) {
  37. e.getMessage();
  38. } finally {
  39. redisUtil.releaseLock(key,requestId);
  40. }
  41. }
  42. /**
  43. * 处理摄像头上传的视频
  44. */
  45. @Async
  46. @Scheduled(cron = "30 2/5 * * * ?")
  47. public void handleCameraVideo() {
  48. String key = RedisKeyConstant.HANDLE_CAMERA_VIDEO;
  49. String requestId = UUID.randomUUID().toString();
  50. boolean result = redisUtil.tryLock(key,requestId,10 * 60);
  51. try {
  52. if (result) {
  53. faceIdenService.handleCameraVideo();
  54. }
  55. } catch (Exception e) {
  56. e.getMessage();
  57. } finally {
  58. redisUtil.releaseLock(key,requestId);
  59. }
  60. }
  61. }