| | |
| | | */ |
| | | @Override |
| | | public ResultUtil saveData(Reassign reassign, Integer uid, Integer type) throws Exception { |
| | | boolean lock = redisUtil.lock(reassign.getOrderId() + "_reassign", 10); |
| | | boolean lock = redisUtil.lock(reassign.getOrderId() + "_reassign", 10, 30); |
| | | if(!lock){ |
| | | int num1 = 1; |
| | | while (num1 <= 10){ |
| | | Thread.sleep(3000);//等待3秒 |
| | | lock = redisUtil.lock(reassign.getOrderId() + "_reassign", 10); |
| | | if(lock){ |
| | | break; |
| | | }else{ |
| | | num1++; |
| | | } |
| | | } |
| | | return ResultUtil.error("系统繁忙,请稍后重试"); |
| | | } |
| | | |
| | | ResultUtil resultUtil = ResultUtil.success(); |
| | |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.StringUtils; |
| | | import java.util.*; |
| | | import java.util.concurrent.Executors; |
| | | import java.util.concurrent.ScheduledExecutorService; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | |
| | |
| | | |
| | | @Autowired |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | private final Map<String, ScheduledExecutorService> LOCK_TASK_MAP = new HashMap<>(); |
| | | |
| | | |
| | | |
| | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * redis加锁 |
| | | * 加锁 |
| | | * @param key |
| | | * @param value |
| | | * @param time |
| | | * @return |
| | | */ |
| | | public boolean lock(String key, String value, int time){ |
| | | if(!StringUtils.isEmpty(key)){ |
| | | key += "_lock"; |
| | | return redisTemplate.opsForValue().setIfAbsent(key, value); |
| | | public boolean lock(String key, long keepTime, long time){ |
| | | ScheduledExecutorService scheduledExecutorService = LOCK_TASK_MAP.get(key); |
| | | if(null == scheduledExecutorService || scheduledExecutorService.isTerminated()){ |
| | | unlock(key); |
| | | } |
| | | keepTime *= 1000; |
| | | Boolean ifAbsent = redisTemplate.opsForValue().setIfAbsent(key, UUIDUtil.getRandomCode()); |
| | | if(ifAbsent){ |
| | | return addLockMap(key, time); |
| | | } |
| | | long timeMillis = System.currentTimeMillis(); |
| | | while (timeMillis + keepTime > System.currentTimeMillis()){ |
| | | ifAbsent = redisTemplate.opsForValue().setIfAbsent(key, UUIDUtil.getRandomCode()); |
| | | if(ifAbsent){ |
| | | return addLockMap(key, time); |
| | | } |
| | | try { |
| | | Thread.sleep(100); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 获取redis锁 |
| | | * @param time |
| | | * @return |
| | | */ |
| | | public boolean lock(int time){ |
| | | String uuid = UUID.randomUUID().toString(); |
| | | return lock("redis", uuid, time); |
| | | |
| | | private boolean addLockMap(String key, long time){ |
| | | ScheduledExecutorService scheduledExecutorService = LOCK_TASK_MAP.get(key); |
| | | if(null == scheduledExecutorService){ |
| | | scheduledExecutorService = Executors.newScheduledThreadPool(1); |
| | | } |
| | | |
| | | |
| | | public boolean lock(String key, int time){ |
| | | String uuid = UUID.randomUUID().toString(); |
| | | return lock(key, uuid, time); |
| | | LOCK_TASK_MAP.put(key, scheduledExecutorService); |
| | | scheduledExecutorService.schedule(()->{ |
| | | redisTemplate.delete(key); |
| | | LOCK_TASK_MAP.remove(key); |
| | | }, time, TimeUnit.SECONDS); |
| | | return true; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * redis释放锁 |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public boolean unlock(String key){ |
| | | if(!StringUtils.isEmpty(key)){ |
| | | key += "_lock"; |
| | | return redisTemplate.delete(key); |
| | | redisTemplate.delete(key); |
| | | ScheduledExecutorService scheduledExecutorService = LOCK_TASK_MAP.get(key); |
| | | if(null != scheduledExecutorService){ |
| | | scheduledExecutorService.shutdownNow(); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 删除锁 |
| | | * @return |
| | | */ |
| | | public boolean unlock(){ |
| | | return unlock("redis"); |
| | | LOCK_TASK_MAP.remove(key); |
| | | return true; |
| | | } |
| | | |
| | | |
| | |
| | | public synchronized ResultUtil<BaseWarpper> saveOrderPrivateCar(Integer serverCarModelId, Date travelTime, Integer reservation, Integer instead, String placementLon, String placementLat, String startLon, String startLat, |
| | | String startAddress, String endLon, String endLat, String endAddress, String name, String phone, Integer orderSource, Integer driverId, Integer type, Integer uid, |
| | | BigDecimal estimatedPrice,String cityCode,Integer carIndex) throws Exception { |
| | | |
| | | long timeMillis = System.currentTimeMillis(); |
| | | //如果出行时间大于当前10分钟则默认为预约单 |
| | | if(travelTime.getTime() > (System.currentTimeMillis() + 600000)){ |
| | | reservation = 2; |
| | |
| | | } |
| | | AssignOrder assignOrder = assignOrderService.selectOne(new EntityWrapper<AssignOrder>().eq("companyId", query.getId()).eq("type", 1)); |
| | | if(null != assignOrder){//配置了指派规则才处理 |
| | | boolean lock = redisUtil.lock("order_lock", 10); |
| | | boolean lock = redisUtil.lock("order_lock", 10, 30); |
| | | if(!lock){ |
| | | int num1 = 1; |
| | | while (num1 <= 10){ |
| | | Thread.sleep(3000);//等待3秒 |
| | | lock = redisUtil.lock("order_lock", 10); |
| | | if(lock){ |
| | | break; |
| | | }else{ |
| | | num1++; |
| | | } |
| | | } |
| | | return ResultUtil.error("系统繁忙,请稍后重试"); |
| | | } |
| | | try { |
| | | //获取空闲司机 |
| | |
| | | */ |
| | | @Override |
| | | public ResultUtil cancleOrderPrivateCar(Integer id, Integer payType, Integer cancleId, Integer type) throws Exception { |
| | | boolean lock = redisUtil.lock(id + "_cancleOrder", 10); |
| | | boolean lock = redisUtil.lock(id + "_cancleOrder", 10, 30); |
| | | if(!lock){ |
| | | int num1 = 1; |
| | | while (num1 <= 10){ |
| | | Thread.sleep(3000);//等待3秒 |
| | | lock = redisUtil.lock(id + "_cancleOrder", 10); |
| | | if(lock){ |
| | | break; |
| | | }else{ |
| | | num1++; |
| | | } |
| | | } |
| | | return ResultUtil.error("系统繁忙,请稍后重试"); |
| | | } |
| | | ResultUtil resultUtil = ResultUtil.success(""); |
| | | try { |
| | |
| | | |
| | | @Override |
| | | public ResultUtil payPrivateCarOrder(Integer payType, Integer orderId, Integer couponId, Integer type) throws Exception { |
| | | boolean lock = redisUtil.lock(orderId + "_payment", 10); |
| | | boolean lock = redisUtil.lock(orderId + "_payment", 10, 30); |
| | | if(!lock){ |
| | | int num1 = 1; |
| | | while (num1 <= 10){ |
| | | Thread.sleep(3000);//等待3秒 |
| | | lock = redisUtil.lock(orderId + "_payment", 10); |
| | | if(lock){ |
| | | break; |
| | | }else{ |
| | | num1++; |
| | | } |
| | | } |
| | | return ResultUtil.error("系统繁忙,请稍后重试"); |
| | | } |
| | | try { |
| | | OrderPrivateCar orderPrivateCar = this.selectById(orderId); |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public synchronized void payOrderPrivateCarCallback(Integer id, String order_id, Integer type) throws Exception { |
| | | boolean lock = redisUtil.lock(order_id + "_paymentCallback", 10); |
| | | boolean lock = redisUtil.lock(order_id + "_paymentCallback", 10, 30); |
| | | if(!lock){ |
| | | int num1 = 1; |
| | | while (num1 <= 10){ |
| | | Thread.sleep(3000);//等待3秒 |
| | | lock = redisUtil.lock(order_id + "_paymentCallback", 10); |
| | | if(lock){ |
| | | break; |
| | | }else{ |
| | | num1++; |
| | | return; |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | try { |
| | | PaymentRecord query = paymentRecordService.query(1, null, null, Integer.valueOf(id), 1, type, null); |
| | |
| | | package com.stylefeng.guns.modular.system.util; |
| | | |
| | | import com.stylefeng.guns.core.util.ToolUtil; |
| | | import org.apache.tomcat.util.threads.ThreadPoolExecutor; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.geo.Circle; |
| | | import org.springframework.data.geo.GeoResult; |
| | | import org.springframework.data.geo.GeoResults; |
| | | import org.springframework.data.geo.Point; |
| | | import org.springframework.data.redis.connection.RedisGeoCommands; |
| | | import org.springframework.data.redis.core.HashOperations; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.util.*; |
| | | import java.util.concurrent.Executors; |
| | | import java.util.concurrent.LinkedBlockingQueue; |
| | | import java.util.concurrent.ScheduledExecutorService; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | |
| | |
| | | @Autowired |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | |
| | | private final Map<String, ScheduledExecutorService> LOCK_TASK_MAP = new HashMap<>(); |
| | | |
| | | /** |
| | | * 向redis中存储字符串没有过期时间 |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * redis加锁 |
| | | * 加锁 |
| | | * @param key |
| | | * @param value |
| | | * @param time |
| | | * @return |
| | | */ |
| | | public boolean lock(String key, String value, int time){ |
| | | if(!StringUtils.isEmpty(key)){ |
| | | key += "_lock"; |
| | | return redisTemplate.opsForValue().setIfAbsent(key, value); |
| | | public boolean lock(String key, long keepTime, long time){ |
| | | ScheduledExecutorService scheduledExecutorService = LOCK_TASK_MAP.get(key); |
| | | if(null == scheduledExecutorService || scheduledExecutorService.isTerminated()){ |
| | | unlock(key); |
| | | } |
| | | keepTime *= 1000; |
| | | Boolean ifAbsent = redisTemplate.opsForValue().setIfAbsent(key, UUIDUtil.getRandomCode()); |
| | | if(ifAbsent){ |
| | | return addLockMap(key, time); |
| | | } |
| | | long timeMillis = System.currentTimeMillis(); |
| | | while (timeMillis + keepTime > System.currentTimeMillis()){ |
| | | ifAbsent = redisTemplate.opsForValue().setIfAbsent(key, UUIDUtil.getRandomCode()); |
| | | if(ifAbsent){ |
| | | return addLockMap(key, time); |
| | | } |
| | | try { |
| | | Thread.sleep(100); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 获取redis锁 |
| | | * @param time |
| | | * @return |
| | | */ |
| | | public boolean lock(int time){ |
| | | String uuid = UUID.randomUUID().toString(); |
| | | return lock("redis", uuid, time); |
| | | |
| | | private boolean addLockMap(String key, long time){ |
| | | ScheduledExecutorService scheduledExecutorService = LOCK_TASK_MAP.get(key); |
| | | if(null == scheduledExecutorService){ |
| | | scheduledExecutorService = Executors.newScheduledThreadPool(1); |
| | | } |
| | | |
| | | |
| | | public boolean lock(String key, int time){ |
| | | String uuid = UUID.randomUUID().toString(); |
| | | return lock(key, uuid, time); |
| | | LOCK_TASK_MAP.put(key, scheduledExecutorService); |
| | | scheduledExecutorService.schedule(()->{ |
| | | redisTemplate.delete(key); |
| | | LOCK_TASK_MAP.remove(key); |
| | | }, time, TimeUnit.SECONDS); |
| | | return true; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * redis释放锁 |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public boolean unlock(String key){ |
| | | if(!StringUtils.isEmpty(key)){ |
| | | key += "_lock"; |
| | | return redisTemplate.delete(key); |
| | | redisTemplate.delete(key); |
| | | ScheduledExecutorService scheduledExecutorService = LOCK_TASK_MAP.get(key); |
| | | if(null != scheduledExecutorService){ |
| | | scheduledExecutorService.shutdownNow(); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 删除锁 |
| | | * @return |
| | | */ |
| | | public boolean unlock(){ |
| | | return unlock("redis"); |
| | | LOCK_TASK_MAP.remove(key); |
| | | return true; |
| | | } |
| | | |
| | | |
| | |
| | | host: 127.0.0.1 |
| | | port: 16379 |
| | | password: mPMHThYzlT8DWgl8HLqwPEyPOiHDPPB5 |
| | | # port: 6379 |
| | | # password: 123456 |
| | | |
| | | mybatis-plus: |
| | | typeAliasesPackage: com.stylefeng.guns.modular |
| | |
| | | # password: 123456 |
| | | db-name: guns #用来搜集数据库的所有表 |
| | | filters: wall,mergeStat |
| | | |
| | | #多数据源情况的配置 |
| | | guns: |
| | | muti-datasource: |
| | | open: false |
| | | url: jdbc:mysql://127.0.0.1:3306/guns_test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=Asia/Shanghai |
| | | username: root |
| | | password: root |
| | | dataSourceNames: |
| | | - dataSourceGuns |
| | | - dataSourceBiz |
| | | |
| | | |
| | | --- |
| | |
| | | //import com.stylefeng.guns.modular.system.util.RedisUtil; |
| | | //import org.junit.Test; |
| | | //import org.junit.runner.RunWith; |
| | | //import org.springframework.beans.factory.annotation.Autowired; |
| | | //import org.springframework.boot.test.context.SpringBootTest; |
| | | //import org.springframework.data.geo.GeoResult; |
| | | //import org.springframework.data.redis.connection.RedisGeoCommands; |
| | | //import org.springframework.test.context.junit4.SpringRunner; |
| | | // |
| | | //import java.util.List; |
| | | //import javax.annotation.Resource; |
| | | // |
| | | // * @author zhibing.pu |
| | | // * @Date 2024/9/24 9:24 |
| | | // */ |
| | | // |
| | | //@RunWith(SpringRunner.class) |
| | | //@SpringBootTest |
| | | //public class GunsApplicationTest { |
| | | // |
| | | // @Autowired |
| | | // @Resource |
| | | // private RedisUtil redisUtil; |
| | | // |
| | | // |
| | | // @Test |
| | | // public void test(){ |
| | | // List<GeoResult<RedisGeoCommands.GeoLocation<String>>> driverPosition = redisUtil.getNearGeo("DRIVER_POSITION", 104.5615D, 30.456275D, 50000D); |
| | | // System.err.println(driverPosition); |
| | | // boolean lock = redisUtil.lock("key_test", 10, 30); |
| | | // if(lock){ |
| | | // System.err.println("-----------------"); |
| | | // } |
| | | //// redisUtil.unlock("key_test"); |
| | | // lock = redisUtil.lock("key_test", 10, 30); |
| | | // if(lock){ |
| | | // System.err.println("!!!!!!!!!!!!!!!!!!!!"); |
| | | // return; |
| | | // } |
| | | // System.err.println("*****************"); |
| | | // } |
| | | //} |
| | |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.StringUtils; |
| | | import java.util.UUID; |
| | | |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | |
| | |
| | | if(!StringUtils.isEmpty(key)){ |
| | | redisTemplate.delete(key); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * redis加锁 |
| | | * @param key |
| | | * @param value |
| | | * @param time |
| | | * @return |
| | | */ |
| | | public boolean lock(String key, String value, int time){ |
| | | if(!StringUtils.isEmpty(key)){ |
| | | key += "_lock"; |
| | | return redisTemplate.opsForValue().setIfAbsent(key, value); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 获取redis锁 |
| | | * @param time |
| | | * @return |
| | | */ |
| | | public boolean lock(int time){ |
| | | String uuid = UUID.randomUUID().toString(); |
| | | return lock("redis", uuid, time); |
| | | } |
| | | |
| | | |
| | | public boolean lock(String key, int time){ |
| | | String uuid = UUID.randomUUID().toString(); |
| | | return lock(key, uuid, time); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * redis释放锁 |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public boolean unlock(String key){ |
| | | if(!StringUtils.isEmpty(key)){ |
| | | key += "_lock"; |
| | | return redisTemplate.delete(key); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 删除锁 |
| | | * @return |
| | | */ |
| | | public boolean unlock(){ |
| | | return unlock("redis"); |
| | | } |
| | | } |