File tree Expand file tree Collapse file tree
src/test/java/com/distributed Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -15,8 +15,6 @@ public class AccessSpeedLimitTest {
1515 @ Test
1616 public void test1 () throws InterruptedException {
1717 JedisPool jp =new JedisPool ("127.0.0.1" ,6379 );
18- final RedisDistributedLockTemplate template =new RedisDistributedLockTemplate (jp );
19-
2018 AccessSpeedLimit accessSpeedLimit =new AccessSpeedLimit (jp );
2119 SimpleDateFormat sdf =new SimpleDateFormat (" mm:ss" );
2220 while (true ){
Original file line number Diff line number Diff line change @@ -56,4 +56,22 @@ public Object onTimeout() throws InterruptedException {
5656 startCountDownLatch .countDown ();
5757 endDownLatch .await ();
5858 }
59+
60+ public static void main (String [] args ){
61+ JedisPool jedisPool =new JedisPool ("127.0.0.1" ,6379 );//实际应用时可通过spring注入
62+ final RedisDistributedLockTemplate template =new RedisDistributedLockTemplate (jedisPool );//本类多线程安全,可通过spring注入
63+ template .execute ("订单流水号" , 5000 , new Callback () {
64+ @ Override
65+ public Object onGetLock () throws InterruptedException {
66+ //TODO 获得锁后要做的事
67+ return null ;
68+ }
69+
70+ @ Override
71+ public Object onTimeout () throws InterruptedException {
72+ //TODO 获得锁超时后要做的事
73+ return null ;
74+ }
75+ });
76+ }
5977}
Original file line number Diff line number Diff line change 1+ package com .distributed .lock .redis ;
2+
3+ import com .distributed .lock .Callback ;
4+ import org .junit .Test ;
5+ import redis .clients .jedis .JedisPool ;
6+
7+ import java .util .concurrent .CountDownLatch ;
8+ import java .util .concurrent .ThreadLocalRandom ;
9+ import java .util .concurrent .TimeUnit ;
10+ import java .util .concurrent .locks .ReentrantLock ;
11+
12+ /**
13+ * Created by sunyujia@aliyun.com on 2016/2/24.
14+ */
15+ public class SimpleTest {
16+
17+
18+
19+ public static void main (String [] args ) throws Exception {
20+ JedisPool jedisPool =new JedisPool ("127.0.0.1" ,6379 );//实际应用时可通过spring注入
21+ RedisReentrantLock lock =new RedisReentrantLock (jedisPool ,"订单流水号" );
22+ try {
23+ if (lock .tryLock (5000L , TimeUnit .MILLISECONDS )) {
24+ //TODO 获得锁后要做的事
25+ }else {
26+ //TODO 获得锁超时后要做的事
27+ }
28+ }finally {
29+ lock .unlock ();
30+ }
31+ }
32+ }
Original file line number Diff line number Diff line change 11package com .distributed .lock .zk ;
22
33import com .distributed .lock .Callback ;
4+ import com .distributed .lock .redis .RedisDistributedLockTemplate ;
45import org .apache .curator .RetryPolicy ;
56import org .apache .curator .framework .CuratorFramework ;
67import org .apache .curator .framework .CuratorFrameworkFactory ;
78import org .apache .curator .retry .ExponentialBackoffRetry ;
89import org .junit .Test ;
10+ import redis .clients .jedis .JedisPool ;
911
1012import java .util .concurrent .*;
1113
@@ -55,4 +57,25 @@ public Object onTimeout() throws InterruptedException {
5557 startCountDownLatch .countDown ();
5658 endDownLatch .await ();
5759 }
60+
61+ public static void main (String [] args ){
62+ RetryPolicy retryPolicy = new ExponentialBackoffRetry (1000 , 3 );
63+ CuratorFramework client = CuratorFrameworkFactory .newClient ("127.0.0.1:2181" , retryPolicy );
64+ client .start ();
65+
66+ final ZkDistributedLockTemplate template =new ZkDistributedLockTemplate (client );//本类多线程安全,可通过spring注入
67+ template .execute ("订单流水号" , 5000 , new Callback () {
68+ @ Override
69+ public Object onGetLock () throws InterruptedException {
70+ //TODO 获得锁后要做的事
71+ return null ;
72+ }
73+
74+ @ Override
75+ public Object onTimeout () throws InterruptedException {
76+ //TODO 获得锁超时后要做的事
77+ return null ;
78+ }
79+ });
80+ }
5881}
You can’t perform that action at this time.
0 commit comments