openlayer 地圖實現圈選框選清楚 選擇地圖區域
阿新 • • 發佈:2020-11-24
1. Maven依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-redis</artifactId> </dependency>
2. RedisConfig配置
package com.surfilter.rws.device.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.integration.redis.util.RedisLockRegistry;/** * @description: TODO * @author: ruphie * @date: Create in 2020/11/24 10:42 * @company: ruhuanxingyun */ @Configuration public class RedisConfig { @Bean(destroyMethod = "destroy") public RedisLockRegistry redisLockRegistry(RedisConnectionFactory redisConnectionFactory) { return newRedisLockRegistry(redisConnectionFactory, "lock"); } }
3. 相關程式碼
package com.surfilter.rws.device.web.rest; import com.surfilter.rws.device.service.RedisLockService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * @description: TODO * @author: ruphie * @date: Create in 2020/11/24 10:46 * @company: ruhuanxingyun */ @RestController @RequestMapping("/api") public class RedisLockController { @Autowired private RedisLockService redisLockService; @RequestMapping("/redis/lock") public void lock(@RequestParam("key") String key) { for (int i = 0; i < 10; i++) { new Thread(() -> { redisLockService.lock(key); System.out.println("執行緒名" + Thread.currentThread().getName() + "獲取到鎖"); try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } redisLockService.unlock(key); System.out.println("執行緒名" + Thread.currentThread().getName() + "釋放鎖"); }).start(); } } }
package com.surfilter.rws.device.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.integration.redis.util.RedisLockRegistry; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; /** * @description: TODO * @author: ruphie * @date: Create in 2020/11/24 10:51 * @company: ruhuanxingyun */ @Service public class RedisLockService { private static final long DEFAULT_EXPIRE_UNUSED = 60000L; @Autowired private RedisLockRegistry redisLockRegistry; /** * 獲取鎖,如果鎖不可用則執行緒一直等待 * * @param lockKey */ public void lock(String lockKey) { Lock lock = redisLockRegistry.obtain(lockKey); lock.lock(); } /** * 嘗試獲取鎖,獲取失敗直接返回false * * @param lockKey * @return */ public boolean tryLock(String lockKey) { Lock lock = redisLockRegistry.obtain(lockKey); return lock.tryLock(); } /** * 可超時獲取鎖,指定超時時間結束返回false * * @param lockKey * @param seconds 超時時間 * @return */ public boolean tryLock(String lockKey, long seconds) { Lock lock = redisLockRegistry.obtain(lockKey); try { return lock.tryLock(seconds, TimeUnit.SECONDS); } catch (InterruptedException e) { e.printStackTrace(); return false; } } /** * 釋放鎖 * * @param lockKey */ public void unlock(String lockKey) { Lock lock = redisLockRegistry.obtain(lockKey); lock.unlock(); redisLockRegistry.expireUnusedOlderThan(DEFAULT_EXPIRE_UNUSED); } }
可參考:分散式鎖的實現與原理解析