212b7e122bb6a692700a8c212ea9dbd704a4eb02.svn-base 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package org.jeecg.modules.cloud.lock;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.jeecg.boot.starter.lock.annotation.JLock;
  4. import org.jeecg.boot.starter.lock.client.RedissonLockClient;
  5. import org.jeecg.boot.starter.rabbitmq.client.RabbitMqClient;
  6. import org.jeecg.common.base.BaseMap;
  7. import org.jeecg.modules.cloud.constant.CloudConstant;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.scheduling.annotation.Scheduled;
  10. import org.springframework.stereotype.Component;
  11. import java.util.Map;
  12. /**
  13. * 分布式锁测试demo
  14. */
  15. @Slf4j
  16. @Component
  17. public class DemoLockTest {
  18. @Autowired
  19. RedissonLockClient redissonLock;
  20. @Autowired
  21. RabbitMqClient rabbitMqClient;
  22. /**
  23. * 测试分布式锁【注解方式】
  24. */
  25. @Scheduled(cron = "0/5 * * * * ?")
  26. @JLock(lockKey = CloudConstant.REDISSON_DEMO_LOCK_KEY1)
  27. public void execute() throws InterruptedException {
  28. log.info("执行execute任务开始,休眠三秒");
  29. Thread.sleep(3000);
  30. System.out.println("=======================业务逻辑1=============================");
  31. Map map = new BaseMap();
  32. map.put("orderId", "BJ0001");
  33. rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map);
  34. //延迟10秒发送
  35. map.put("orderId", "NJ0002");
  36. rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map, 10000);
  37. log.info("execute任务结束,休眠三秒");
  38. }
  39. public DemoLockTest() {
  40. }
  41. /**
  42. * 测试分布式锁【编码方式】
  43. */
  44. //@Scheduled(cron = "0/5 * * * * ?")
  45. public void execute2() throws InterruptedException {
  46. if (redissonLock.tryLock(CloudConstant.REDISSON_DEMO_LOCK_KEY2, -1, 6000)) {
  47. log.info("执行任务execute2开始,休眠十秒");
  48. Thread.sleep(10000);
  49. System.out.println("=======================业务逻辑2=============================");
  50. log.info("定时execute2结束,休眠十秒");
  51. redissonLock.unlock(CloudConstant.REDISSON_DEMO_LOCK_KEY2);
  52. } else {
  53. log.info("execute2获取锁失败");
  54. }
  55. }
  56. }