5bbaf0463aaf0c235d68c98e4640eaef262c384e.svn-base 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package org.jeecg.boot.starter.lock.test;
  2. import org.jeecg.boot.starter.lock.annotation.JLock;
  3. import org.jeecg.boot.starter.lock.annotation.JRepeat;
  4. import org.jeecg.boot.starter.lock.annotation.LockConstant;
  5. import org.jeecg.boot.starter.lock.client.RedissonLockClient;
  6. import org.springframework.stereotype.Service;
  7. import javax.annotation.Resource;
  8. @Service
  9. public class LockService {
  10. @Resource
  11. private RedissonLockClient redissonLockClient;
  12. int n = 10;
  13. /**
  14. * 模拟秒杀(注解方式)
  15. */
  16. @JLock(lockKey = "#productId", expireSeconds = 5000)
  17. public void seckill(String productId) {
  18. if (n <= 0) {
  19. System.out.println("活动已结束,请下次再来");
  20. return;
  21. }
  22. System.out.println(Thread.currentThread().getName() + ":秒杀到了商品");
  23. System.out.println(--n);
  24. }
  25. /**
  26. * 模拟秒杀(编程方式)
  27. */
  28. public void seckill2(String productId) {
  29. redissonLockClient.tryLock(productId, 5000);
  30. if (n <= 0) {
  31. System.out.println("活动已结束,请下次再来");
  32. return;
  33. }
  34. System.out.println(Thread.currentThread().getName() + ":秒杀到了商品");
  35. System.out.println(--n);
  36. redissonLockClient.unlock(productId);
  37. }
  38. /**
  39. * 测试重复提交
  40. */
  41. @JRepeat(lockKey = "#name", lockTime = 5)
  42. public void reSubmit(String name) {
  43. try {
  44. Thread.sleep(1500);
  45. } catch (InterruptedException e) {
  46. e.printStackTrace();
  47. }
  48. System.out.println("提交成功" + name);
  49. }
  50. }