TestTransaction.java
2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.zhaoonline.redis.transaction;
import java.nio.charset.Charset;
import java.util.concurrent.CountDownLatch;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import com.zhaoonline.redis.TestRedisTransaction;
import com.zhaoonline.redis.TestRedisTransaction.InnerThread;
import com.zhaoonline.redis.template.RedisTemplateFactory;
public class TestTransaction {
public class InnerThread implements Runnable{
private Long sleepTime;
private CountDownLatch lacth;
private String id;
private TransactionValueOperation<Integer> tranction;
private String key="trans:test";
InnerThread(String id,Long sleeptime,CountDownLatch lacth,TransactionValueOperation<Integer> tranction){
this.id=id;
this.sleepTime=sleeptime;
this.lacth=lacth;
this.tranction = tranction;
}
@Override
public void run() {
lacth.countDown();
tranction.delete(key);
tranction.setIfAbsent(key,100);
Integer value=tranction.get(key);
System.out.println("id :"+id+" get value,before:"+value);
// tranction.incrementWithTransaction(key, 5);
// tranction.incrementWithTransaction(key, new Long(2L));
// System.out.println("id :"+id+" get increment,after:"+result);
// System.out.println("id :"+id+" get increment,after:"+tranction.increment(key, 5));
// value=(Integer)tranction.get(key);
// System.out.println("id :"+id+" get value,after:"+value);
// tranction.delete(key);
return;
}
}
public static void main(String[] args) {
TestTransaction test=new TestTransaction();
CountDownLatch lacth=new CountDownLatch(2);
RedisTemplateFactory factory=null;
JedisConnectionFactory connectionFactory=null;
connectionFactory=test.createSimpleFactory();
factory=new RedisTemplateFactory();
factory.setConnectionFactory(connectionFactory);
TransactionValueOperation<Integer> trans=new TransactionValueOperation<Integer>(factory);
test.new InnerThread("1",1000L,lacth,trans).run();
// new Thread(test.new InnerThread("1",1000L,lacth,trans)).start();
// new Thread(test.new InnerThread("2",500L,lacth,trans)).start();
}
private JedisConnectionFactory createSimpleFactory(){
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName("192.168.0.188");
factory.setPort(6377);
factory.setUsePool(true);
factory.afterPropertiesSet();
return factory;
}
}