TestTransaction.java 2.41 KB
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;
	}
	
	
}