1. Import the JAR package
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.11.1</version> </dependency> <dependency>Copy the code
2. Application. properties Add the configuration file
Server.port =8998 spring.redis.host=127.0.0.1 #Redis server connection port Spring.redis. port=6379 # redis server connection password (default empty) Spring. Redis. Password = # connection pool maximum number of connections (use a negative value indicates no limit) spring. Redis. Jedis. Pool. The Max - active = 500 spring.redis.jedis.pool.max-idle=1000 spring.redis.jedis.pool.max-wait=6000ms spring.redis.jedis.pool.min-idle=4 Timeout =30000 # redisson lock redisson.address=redis://127.0.0.1:6379 redisson.password=Copy the code
Create a Redisson configuration class
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/** * Redisson class * Created on 2018/6/19 */
@Configuration
public class RedissonConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private String port;
@Value("${spring.redis.password}")
private String password;
@Bean
public RedissonClient getRedisson(a){
Config config = new Config();
config.useSingleServer().setAddress("redis://" + host + ":" + port)
// Heartbeat detection, periodic connection with Redis, can prevent the connection with Redis disconnection after a period of time
.setPingConnectionInterval(1000);// Add the master/slave configuration
// config.useMasterSlaveServers().setMasterAddress("").setPassword("").addSlaveAddress(new String[]{"",""});
returnRedisson.create(config); }}Copy the code
4. The test class
import com.zhipeng.testspringboot.redission.RedissonService;
import org.redisson.api.RLock;
import java.util.concurrent.TimeUnit;
public class TestIt {
static int number =0 ;
public static int testredis(RedissonService redissonService){
RLock lock =null;
try {
lock = redissonService.getRLock("record2Ids");
if(! lock.isLocked()){ lock.lock(30, TimeUnit.MINUTES);
number++;
System.out.println(Thread.currentThread().getName()+"->:"+number);
return 1;
}else{
System.err.println(Thread.currentThread().getName() +"->:"+"Locked in.");
return 0; }}finally {
if (null! = lock && lock.isLocked() && lock.isHeldByCurrentThread()){ lock.unlock(); }}}}Copy the code
An exception occurs when multiple threads call this method
Problem description
When we use Ression in the Lock. The Lock () method, if there is a thread of concurrent common cases, can appear the following exceptions: Java. Lang. IllegalMonitorStateException: attempt to unlock lock, not locked by current thread by node id: 9f178836-f7e1-44fe-a89d-2db52f399c0d thread-id: 22
When Thread-1 is not finished, that is, when Thread-1 is acquiring the lock but not releasing the lock, ‘Thread-2 enters the try code block, exits after failing to obtain the lock, enters the Finally code block, and tries to unlock the lock of Thread-1. So Thread-2 did not acquire the lock and tried to release it.
Problem solving
In the finally add judgment lock. IsHeldByCurrentThread ()
/ / is the currently executing thread lock the if (lock) isHeldByCurrentThread ()) {}Copy the code
The same solution resolves the issue of the lock being automatically released in finally after the current method has timed out.