赞
踩
基于Java Redis 和Netty 实现的高性能异步无锁框架
温馨提示:JDK 大于等于1.8
- <!-- 集成并发锁模块 -->
- <dependency>
- <groupId>org.redisson</groupId>
- <artifactId>redisson-spring-boot-starter</artifactId>
- <version>3.13.1</version>
- <exclusions>
- <exclusion>
- <groupId>org.redisson</groupId>
- <artifactId>redisson-spring-data-22</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
-
- <dependency>
- <groupId>org.redisson</groupId>
- <artifactId>redisson-spring-data-18</artifactId>
- <version>3.13.1</version>
- </dependency>

-
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.ImportResource;
- import org.springframework.context.annotation.PropertySource;
-
- @Configuration
- @PropertySource("classpath:redis/redis.properties")
- @ImportResource({"classpath:redisson.xml" })
- public class RedissonConfig {
-
- }
redis.properties
- # redis host å°å
- redis.host = 127.0.0.1
- # redis port 端å£
- redis.port = 6379
- # redis password å¯ç
- redis.password = abc123
- # redis åå¨æ°æ®åº
- redis.database = 0
redisson.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:redisson="http://redisson.org/schema/redisson"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://redisson.org/schema/redisson
- http://redisson.org/schema/redisson/redisson.xsd">
-
- <redisson:client id="redissonClient">
- <redisson:single-server address="redis://${redis.host}:${redis.port}" connection-pool-size="30" password="${redis.password}"/>
- </redisson:client>
-
- </beans>

功能说明:生成调档单号使用分布式锁,防止并发产生重复调档单号。
- @Autowired
- private GlobalLogMessage globalLogMessage;
-
- @Autowired
- private RedissonClient redissonClient;
-
- // 定义常量锁对象
- public static final String APPLICATION_CODE = "APPLICATION_CODE";
-
- @ApiOperation(httpMethod = "POST", value = "利用信息保存")
- @RequestMapping(value = "/insert", method = { RequestMethod.POST }, produces = "application/json;charset=UTF-8")
- @ResponseBody
- public Result insert(HttpServletRequest request,
- @RequestBody @ApiParam(name = "利用对象", value = "json格式对象", required = true) UcasArchiveUseListWrapper entity) {
- String sid = null;
- RLock rlock = redissonClient.getLock(APPLICATION_CODE);//设置锁超时时间,防止异常造成死锁
- rlock.lock(10, TimeUnit.SECONDS);
- try{
- Map<String, Object> paramter = new HashMap<String, Object>();
- paramter.put("currentDt", DateUtils.dateTimeNow("yyyy-MM-dd"));
- Integer total = ucasArchiveUseListService.getSerialNumber(paramter);// 更新对象的状态
- String useNo = null;
- if (total > 0) {
- total++;
- useNo = StringFormatUtil.addZeroForNum(String.valueOf(total), 4);
- } else {
- total = 1;
- useNo = StringFormatUtil.addZeroForNum(String.valueOf(total), 4);
- }
- LocalDateTime currentDate = LocalDateTime.now();
- String dt = currentDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
- entity.setUseNo(dt + useNo);
- // 捕获异常,必要时恢复到原来的不变约束
- // 如果有return语句,放在这里
-
- // status=0申请调阅
- entity.setStatus(ArchiveUseListConstants.APPLY_READING);
- entity.setCreatedDt(new Date());
- UserTool tool = new UserTool(ucasAuthUserInfoService);
- String userPin = tool.getUserPin(request);
- if (!StringUtils.isEmpty(userPin)) {
- entity.setCreatedBy(userPin);
- }
- entity.setInfoSource(ArchiveUseListConstants.ACCESS_MUSEUM_TYPE);
- sid = ucasArchiveUseListService.insertSelective(entity);
- } catch(Exception e){
- log.error("生成调档单号异常:", e.getMessage(), e);
- }finally{
- rlock.unlock();
- }

- <!-- 集成redisson-->
- <dependency>
- <groupId>org.redisson</groupId>
- <artifactId>redisson-spring-boot-starter</artifactId>
- <version>3.13.6</version>
- </dependency>
其他维持不变
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。