ToB企服应用市场:ToB评测及商务社交产业平台
标题:
若依-单呆板多服务器定时使命重复实行问题解决-分布式锁
[打印本页]
作者:
乌市泽哥
时间:
2024-9-18 16:10
标题:
若依-单呆板多服务器定时使命重复实行问题解决-分布式锁
1.定位定时使命所走方法
打方法断点
任意找一个定时使命的方法类,打断点检察实行过程
实行方法
点击立刻实行,实行该方法
定位方法
点击步过,寻找符合的代码位置增长分布式锁
最终看到方法在此处进行返回,我们可以再此处进行加锁操作
2.添加分布式锁
引入redisson依赖,添加配置类
在ruoyi-common pom引入redisson依赖
<!--Redisson redis若依已经引入-->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.17.4</version>
</dependency>
复制代码
添加配置类
package com.ruoyi.common.core.redis;
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;
/**
* @Author Cxt
* @Description redisson配置
* @Date 2023/6/29 15:47
* @Version 1.0
*/
@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 redissonClient() {
Config config = new Config();
// 配置Redisson连接信息
config.useSingleServer().setAddress("redis://" + host + ":" + port).setPassword(null);
return Redisson.create(config);
}
}
复制代码
引入redisson服务
如果开启了ScheduleConfig类 注入方式请改为如下图所示注入
修改原有的方法
在处理好sysJob对象后 根据使命名称来创建锁对象
@Override
public void execute(JobExecutionContext context) throws JobExecutionException
{
SysJob sysJob = new SysJob();
BeanUtils.copyBeanProp(sysJob, context.getMergedJobDataMap().get(ScheduleConstants.TASK_PROPERTIES));
try
{
before(context, sysJob);
if (sysJob != null)
{
//创建锁对象
RLock lock = redissonClient.getLock(ScheduleConstants.TASK_PROPERTIES);
//尝试获取锁
boolean isLock = lock.tryLock();
if (!isLock){
throw new ServiceException("该任务正在执行中!");
}
try{
//真正执行的方法
doExecute(context, sysJob);
}finally {
//释放锁
lock.unlock();
}
}
after(context, sysJob, null);
}
catch (Exception e)
{
log.error("任务执行异常 - :", e);
after(context, sysJob, e);
}
}
复制代码
3.测试
同一时间如果拿不到锁将会直接失败,成功达到效果
特别注意
在拿到锁之后,由于走的是本地缓存,为了克制重复实行,请将使命策略改为放弃实行
官方方法
取消注释类ScheduleConfig即可(注意检察相关表是否存在)
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4