Normal way
Look directly at the code
package com.whereta.jedis;import org.springframework.stereotype.component;import redis.clients.jedis.jedis;import redis.clients.jedis.jedispool;import javax.annotation.resource; import java.util.calendar;import java.util.date;import java.util.concurrent.timeunit;/** * created by vincent on 15-10-14. */@Component ("Jedislock") public class JedisLock { public class CacheName { public static final string add_bean_after_check_lock_key = "Addbeanafterchecklock"; } @Resource private jedispool jedispool; public void addbeanaftercheck ( Int userid) { Jedis jedis = Jedispool.getresource (); calendar calendar = calendar.getinstance (); calendar.set (calendar.millisecond, 0); Date calendartime = calendar.gettime (); long time = calendartime.gettime (); //with a user ID of key, Different users call the same method without locking string key = cachename.add_bean_ after_check_lock_key + ":" + userid; try { if (jedis != NULL) { Long lock = jedis.setnx (key, time + ""); while (lock == 0) { timeunit.milliseconds.sleep (50); lock = jedis.setnx (key, time + ""); } //Set timeout time jedis.expire (key, 3); } // Own business logic processing } catch (exception e) { &nbSp; throw new runtimeexception (e); } finally { if (jedis != null) { jedis.del (Key); jedis.close (); } } }}
In the example, the user ID is key locking: When different users access the method, they do not reject the wait
The implementation of the lock mechanism takes advantage of the Redis Setnx method: If a key exists in the database, it does not store the data and returns 0
Redis's approach is thread-safe and can be used with confidence
AOP approach
Sample code
package com.whereta.jedis;import org.aspectj.lang.proceedingjoinpoint;import org.aspectj.lang.annotation.around;import org.aspectj.lang.annotation.aspect;import org.springframework.stereotype.component;import redis.clients.jedis.jedis;import Redis.clients.jedis.jedispool;import javax.annotation.resource;import java.util.calendar;import java.util.date;import java.util.concurrent.timeunit;/** * created by vincent on 15-10-14. */@Aspect @componentpublic class jedislockaop { @Resource private JedisPool jedisPool; @Around (" Execution (Public * com.heli.core.pay.soaservice.impl.*.* (..)) ")     PUBLIC OBJECT SERVICEAOP (Proceedingjoinpoint point) { object proceed = null; & NBsp; jedis jedis = jedispool.getresource (); calendar calendar = calendar.getinstance (); calendar.set (calendar.millisecond, 0); date Calendartime = calendar.gettime (); long time = calendartime.gettime (); //request parameter Array object[] args = point.getargs (); //key can be customized String key = jedislock.cachename.add_bean_after_check_lock_key + ":" + args[0]; try { if (Jedis != null) { long Lock = jedis.setnx (key, time + ""); while (lock == 0) { TimeUnit.MILLISECONDS.sleep (; ) lock = jedis.setnx (key, time + ""); } //Set timeout time jedis.expire (key, 3); } //User Business Processing proceed = point.proceed (); return proceed; } catch (throwable throwable) { throw new runtimeexception (Throwable); } finally { if (jedis != null) { jedis.del (Key); jedis.close (); &nbSP;} } }}
Wrapping method using Spring AOP
Specific key values can be customized to include, for example: IP, TIME, etc.
Redis Implements distributed locks