Commit 13d1f3bc authored by tangwei's avatar tangwei

单体模式下,不初始化锁

parent 75904892
......@@ -39,7 +39,6 @@ public class RedissonConfig {
}
Config config = new Config();
ClusterServersConfig clusterServersConfig = config.useClusterServers()
.setScanInterval(2000) // 集群状态扫描间隔时间,单位是毫秒
.addNodeAddress(clusterNodes.toArray(new String[clusterNodes.size()]));
......@@ -56,19 +55,19 @@ public class RedissonConfig {
* 单机模式 redisson 客户端
*/
@Bean
@ConditionalOnProperty(name = "spring.redis.mode", havingValue = "single")
RedissonClient redissonSingle() {
Config config = new Config();
String node = redisConfigProperties.getRedissonUrl();
node = node.startsWith("redis://") ? node : "redis://" + node;
SingleServerConfig serverConfig = config.useSingleServer()
.setAddress(node);
if (StringUtils.isNotBlank(redisConfigProperties.getPassword())) {
serverConfig.setPassword(redisConfigProperties.getPassword());
}
return Redisson.create(config);
}
// @Bean
// @ConditionalOnProperty(name = "spring.redis.mode", havingValue = "single")
// RedissonClient redissonSingle() {
// Config config = new Config();
// String node = redisConfigProperties.getRedissonUrl();
// node = node.startsWith("redis://") ? node : "redis://" + node;
// SingleServerConfig serverConfig = config.useSingleServer()
// .setAddress(node);
// if (StringUtils.isNotBlank(redisConfigProperties.getPassword())) {
// serverConfig.setPassword(redisConfigProperties.getPassword());
// }
// return Redisson.create(config);
// }
}
\ No newline at end of file
......@@ -6,6 +6,7 @@ import com.yeejoin.equipmanage.common.utils.DateUtils;
import com.yeejoin.equipmanage.service.IAnalysisReportLogService;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
......@@ -25,43 +26,61 @@ public class AnalysisReportSchedulerJob {
@Autowired
private IAnalysisReportLogService iAnalysisReportLogService;
@Autowired
@Autowired(required = false)
org.redisson.api.RedissonClient redisson;
@Autowired
private RedisUtils redisUtils;
@Value("${spring.redis.mode}")
private String cluster;
/**
* 每天凌晨0点-日报生成
*/
@Scheduled(cron = "0 0 0 * * ?")
public void dayReport() throws ParseException {
if(cluster.equals("cluster")){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time=format.format(new Date());
String jobName = "dayReport" + time;
//为了便于区分key,增加后缀_redisson
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
//为了防止重复
if (!redisUtils.hasKey(jobName + "_dayReport_key")) {
redisUtils.set(jobName + "_dayReport_key", "1");//增加标识
Date beginDate = DateUtils.dateAdd(new Date(), -1, false);
Date endDate = DateUtils.dateAdd(new Date(), -1, false);
iAnalysisReportLogService.generateReport(AnalysisReportEnum.DAY_REPORT, beginDate, endDate);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
lock.unlock(); //释放锁
}
}else{
try {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time=format.format(new Date());
String jobName = "dayReport" + time;
//为了便于区分key,增加后缀_redisson
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
//为了防止重复
if (!redisUtils.hasKey(jobName + "_dayReport_key")) {
redisUtils.set(jobName + "_dayReport_key", "1");//增加标识
Date beginDate = DateUtils.dateAdd(new Date(), -1, false);
Date endDate = DateUtils.dateAdd(new Date(), -1, false);
iAnalysisReportLogService.generateReport(AnalysisReportEnum.DAY_REPORT, beginDate, endDate);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}
}
/**
......@@ -69,30 +88,40 @@ public class AnalysisReportSchedulerJob {
*/
@Scheduled(cron = "0 0 0 ? * 1")
public void weekReport() throws ParseException {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time=format.format(new Date());
String jobName = "weekReport" + time;
//为了便于区分key,增加后缀_redisson
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
//为了防止重复
if (!redisUtils.hasKey(jobName + "_weekReport_key")) {
redisUtils.set(jobName + "_weekReport_key", "1");//增加标识
Date yestDay = DateUtils.dateAdd(new Date(), -1, false);
Date beginDate = DateUtils.getFirstDayOfWeek(yestDay);
Date endDate = DateUtils.getLastDayOfWeek(yestDay);
iAnalysisReportLogService.generateReport(AnalysisReportEnum.WEEK_REPORT, beginDate, endDate);
if(cluster.equals("cluster")) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time = format.format(new Date());
String jobName = "weekReport" + time;
//为了便于区分key,增加后缀_redisson
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
//为了防止重复
if (!redisUtils.hasKey(jobName + "_weekReport_key")) {
redisUtils.set(jobName + "_weekReport_key", "1");//增加标识
Date yestDay = DateUtils.dateAdd(new Date(), -1, false);
Date beginDate = DateUtils.getFirstDayOfWeek(yestDay);
Date endDate = DateUtils.getLastDayOfWeek(yestDay);
iAnalysisReportLogService.generateReport(AnalysisReportEnum.WEEK_REPORT, beginDate, endDate);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}
}else{
Date yestDay = DateUtils.dateAdd(new Date(), -1, false);
Date beginDate = DateUtils.getFirstDayOfWeek(yestDay);
Date endDate = DateUtils.getLastDayOfWeek(yestDay);
iAnalysisReportLogService.generateReport(AnalysisReportEnum.WEEK_REPORT, beginDate, endDate);
}
}
/**
......@@ -100,28 +129,35 @@ public class AnalysisReportSchedulerJob {
*/
@Scheduled(cron="0 0 0 1 * ?")
public void monthReport() throws ParseException {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time=format.format(new Date());
String jobName = "monthReport" + time;
//为了便于区分key,增加后缀_redisson
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
//为了防止重复
if (!redisUtils.hasKey(jobName + "_monthReport_key")) {
redisUtils.set(jobName + "_monthReport_key", "1");//增加标识
Date yestDay = DateUtils.dateAdd(new Date(), -1, false);
Date beginDate = DateUtils.getFirstDayOfMonth(yestDay);
Date endDate = DateUtils.getLastDayOfMonth(yestDay);
iAnalysisReportLogService.generateMonthReport(AnalysisReportEnum.MONTH_REPORT, beginDate, endDate);
if(cluster.equals("cluster")) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time = format.format(new Date());
String jobName = "monthReport" + time;
//为了便于区分key,增加后缀_redisson
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
//为了防止重复
if (!redisUtils.hasKey(jobName + "_monthReport_key")) {
redisUtils.set(jobName + "_monthReport_key", "1");//增加标识
Date yestDay = DateUtils.dateAdd(new Date(), -1, false);
Date beginDate = DateUtils.getFirstDayOfMonth(yestDay);
Date endDate = DateUtils.getLastDayOfMonth(yestDay);
iAnalysisReportLogService.generateMonthReport(AnalysisReportEnum.MONTH_REPORT, beginDate, endDate);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}
}else{
Date yestDay = DateUtils.dateAdd(new Date(), -1, false);
Date beginDate = DateUtils.getFirstDayOfMonth(yestDay);
Date endDate = DateUtils.getLastDayOfMonth(yestDay);
iAnalysisReportLogService.generateMonthReport(AnalysisReportEnum.MONTH_REPORT, beginDate, endDate);
}
}
}
......@@ -13,6 +13,7 @@ import java.util.List;
import com.yeejoin.amos.boot.biz.common.utils.RedisUtils;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -49,8 +50,11 @@ public class View3dController extends AbstractBaseController {
private IRiskSourceService riskSourceService;
@Autowired
private IView3dService view3dService;
@Autowired
@Autowired(required = false)
org.redisson.api.RedissonClient redisson;
@Value("${spring.redis.mode}")
private String cluster;
@Autowired
private RedisUtils redisUtils;
@TycloudOperation(ApiLevel = UserType.AGENCY)
......@@ -228,7 +232,7 @@ public class View3dController extends AbstractBaseController {
@Scheduled(cron = "${param.safetyIndexChange.cron}")
public CommonResponse safetyIndexLog(){
if("cluster".equals(cluster)){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time=format.format(new Date());
String jobName = "fas-2" + time;
......@@ -252,6 +256,12 @@ public class View3dController extends AbstractBaseController {
} finally {
lock.unlock(); //释放锁
}
}else{
view3dService.safetyIndexLogGenJob(null);
}
return CommonResponseUtil.success();
}
......
......@@ -12,6 +12,7 @@ import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
......@@ -25,15 +26,17 @@ public class FireScheduled {
@Autowired
IContingencyInstance iContingencyInstance;
@Autowired
@Autowired(required = false)
RedissonClient redisson;
@Autowired
private RedisUtils redisUtils;
@Value("${spring.redis.mode}")
private String cluster;
@Scheduled(cron = "*/2 * * * * ?")
public void runFireQueue() throws Exception {
if("cluster".equals(cluster)){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time=format.format(new Date());
......@@ -80,6 +83,31 @@ public class FireScheduled {
lock.unlock(); //释放锁
}
}else{
if (fireQueue.size() == 0)
return;
Map<String, String> map = fireQueue.getFirst();
String batchNo = map.get("batchNo");
String stepCode = map.get("stepCode");
String buttonCode = map.get("buttonCode");
String confirm = map.get("confirm");
String contingencyPlanId = map.get("contingencyPlanId");
String stepState = map.get("stepState");
// String token = map.get("token");
// String product = map.get("product");
log.info("fireQueue-size:" + fireQueue.size());
log.info("stepCode:" + map.get("stepCode"));
log.info("buttonCode:" + map.get("buttonCode"));
log.info("confirm:" + map.get("confirm"));
log.info("stepState:" + map.get("stepState"));
// RequestContext.setToken(token);
// RequestContext.setProduct(product);
iContingencyInstance.setButtonExecuted(batchNo, contingencyPlanId, buttonCode, confirm);
iContingencyInstance.fire(batchNo, stepCode, contingencyPlanId, buttonCode, confirm, stepState);
}
}
public static LinkedList<Map<String, String>> getFireQueue() {
return fireQueue;
......
......@@ -6,6 +6,7 @@ import com.yeejoin.amos.knowledgebase.face.service.DocAuditService;
import com.yeejoin.amos.knowledgebase.face.service.InteractionCountService;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
......@@ -37,8 +38,12 @@ public class QuoteCountFlushTiming {
@Autowired
private ConfigLoader configLoader;
@Autowired
@Autowired(required = false)
org.redisson.api.RedissonClient redisson;
@Value("${spring.redis.mode}")
private String cluster;
@Autowired
private RedisUtils redisUtils;
......@@ -55,6 +60,7 @@ public class QuoteCountFlushTiming {
@Transactional(rollbackFor = {Exception.class, BaseException.class})
public void flushTagQuoteCount() {
if("cluster".equals(cluster)){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time=format.format(new Date());
String jobName = "knowledgeBase-1" +time;
......@@ -85,40 +91,57 @@ public class QuoteCountFlushTiming {
} finally {
lock.unlock(); //释放锁
}
}else{
if (needFlush.get()) {
needFlush.set(false);
interactionCountService.deleteAllTagQuote();
List<KnowledgeInteractionCount> allTagQuoteRecords = interactionCountService.getAllTagQuoteRecords();
if (!allTagQuoteRecords.isEmpty()) {
allTagQuoteRecords.forEach(e -> e.setAgencyCode(configLoader.getAgencyCode()));
boolean finished = interactionCountService.saveBatch(allTagQuoteRecords);
System.out.println("定时-刷新标签引用数-" + (finished ? "完成" : "失败"));
}
}
}
}
@Scheduled(fixedRate = 3 * 1000)
@Transactional(rollbackFor = {Exception.class, BaseException.class})
public void pushDocs2Rule() {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time=format.format(new Date());
String jobName = "knowledgeBase-2" + time;
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
System.out.println("获取锁成功============");
//为了防止重复
if (!redisUtils.hasKey(jobName + "_redisson_key")) {
redisUtils.set(jobName + "_redisson_key", "1");//
if (needPush2Rule.get()) {
needPush2Rule.set(false);
docAuditService.pushDocs2RuleByMQ();
System.out.println("定时-同步规则-完成");
}
if("cluster".equals(cluster)) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time = format.format(new Date());
String jobName = "knowledgeBase-2" + time;
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
System.out.println("获取锁成功============");
//为了防止重复
if (!redisUtils.hasKey(jobName + "_redisson_key")) {
redisUtils.set(jobName + "_redisson_key", "1");//
if (needPush2Rule.get()) {
needPush2Rule.set(false);
docAuditService.pushDocs2RuleByMQ();
System.out.println("定时-同步规则-完成");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}
}else{
if (needPush2Rule.get()) {
needPush2Rule.set(false);
docAuditService.pushDocs2RuleByMQ();
System.out.println("定时-同步规则-完成");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}
}
}
......@@ -77,8 +77,13 @@ public class JobService implements IJobService {
private String patrolTopic;
@Autowired
@Autowired(required = false)
org.redisson.api.RedissonClient redisson;
@Value("${spring.redis.mode}")
private String cluster;
@Autowired
private RedisUtils redisUtils;
......@@ -304,6 +309,9 @@ public class JobService implements IJobService {
@Transactional
public void taskJobPerform(long taskId, String jobType, String jobName) {
if("cluster".equals(cluster)){
//为了便于区分key,增加后缀_redisson
RLock lock = redisson.getLock(jobName);
try {
......@@ -349,7 +357,38 @@ public class JobService implements IJobService {
} finally {
lock.unlock(); //释放锁
}
}else{
if (iTaskDao.existsById(taskId)) {
Task task = iTaskDao.findById(taskId).get();
Toke toke = remoteSecurityService.getServerToken();
if (XJConstant.STATUS_MONITOR_END.equals(jobType)) {
if (TaskStatusEnum.UNDERWAY.getValue() == task.getStatus()) {
task.setStatus(TaskStatusEnum.OVERTIME.getValue());
iTaskDao.saveAndFlush(task);
TaskFeedback taskFeedback = new TaskFeedback();
taskFeedback.setUserId(task.getExecutorId());
taskFeedback.setMessage("该任务在规定时间内未完成,请核实信息!任务名称: " + task.getTitle() + " 要求完成时间: "
+ DateUtil.getDateFormat(task.getFinishTime(), "yyyy-MM-dd HH:mm:ss") + " 发起人:"
+ task.getPublisherName() + " 执行人:" + task.getExecutor());
taskFeedback.setCreateDate(new Date());
taskFeedback.setUserName(task.getExecutor());
taskFeedback.setFeedbackTime(new Date());
taskFeedback.setOrgCode(task.getOrgCode());
taskFeedback.setTaskId(task.getId());
taskFeedback.setMessageType(TaskStatusEnum.OVERTIME.getName());
taskFeedback = taskFeedbackDao.save(taskFeedback);
try {
asyncTask.pushTaskDetailInfoTo3D(toke.getToke(), toke.getProduct(), toke.getAppKey(), taskId); // 超时任务向3D推送
} catch (InterruptedException e) {
log.error(e.getMessage(), e);
}
}
} else {
messageService.pushTaskMessage(toke.getToke(), toke.getProduct(), toke.getAppKey(), task);
}
}
removeJob(jobName);
}
}
@Override
......@@ -407,13 +446,29 @@ public class JobService implements IJobService {
@Override
public void msgJobPerform(long msgId, String jobType, String jobName) {
//为了便于区分key,增加后缀_redisson
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
//为了防止重复
if("cluster".equals(cluster)) {
//为了便于区分key,增加后缀_redisson
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
//为了防止重复
if (!redisUtils.hasKey(jobName + "_redisson_key")) {
redisUtils.set(jobName + "_redisson_key", "1");//增加标识
if (iMsgDao.existsById(msgId)) {
Msg msg = iMsgDao.findById(msgId).get();
Toke toke = remoteSecurityService.getServerToken();
messageService.pushMsg(toke.getToke(), toke.getProduct(), toke.getAppKey(), msg);
}
removeJob(jobName);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}
}else{
if (!redisUtils.hasKey(jobName + "_redisson_key")) {
redisUtils.set(jobName + "_redisson_key", "1");//增加标识
......@@ -424,10 +479,6 @@ public class JobService implements IJobService {
}
removeJob(jobName);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}
}
}
......@@ -4,6 +4,7 @@ import com.yeejoin.amos.boot.biz.common.utils.RedisUtils;
import com.yeejoin.amos.maintenance.business.service.intfc.IPlanTaskService;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
......@@ -20,8 +21,11 @@ import java.util.concurrent.TimeUnit;
@EnableScheduling
public class PlanTaskJob {
@Autowired
@Autowired(required = false)
org.redisson.api.RedissonClient redisson;
@Value("${spring.redis.mode}")
private String cluster;
@Autowired
private RedisUtils redisUtils;
......@@ -34,7 +38,7 @@ public class PlanTaskJob {
@Scheduled(cron = "${jobs.cron}")
public void scheduleJob() {
if("cluster".equals(cluster)){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time=format.format(new Date());
String jobName = "maintenance-1" + time;
......@@ -55,7 +59,11 @@ public class PlanTaskJob {
} finally {
lock.unlock(); //释放锁
}
}else{
planTaskService.taskExecution(null);
}
}
......@@ -65,28 +73,30 @@ public class PlanTaskJob {
*/
@Scheduled(cron = "${jobs.cron}")
public void taskMessage() {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time=format.format(new Date());
String jobName = "maintenance-2" + time;
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
System.out.println("获取锁成功============");
//为了防止重复
if (!redisUtils.hasKey(jobName + "_redisson_key")) {
redisUtils.set(jobName + "_redisson_key", "1");//
planTaskService.taskMessage(null);
if("cluster".equals(cluster)) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time = format.format(new Date());
String jobName = "maintenance-2" + time;
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
System.out.println("获取锁成功============");
//为了防止重复
if (!redisUtils.hasKey(jobName + "_redisson_key")) {
redisUtils.set(jobName + "_redisson_key", "1");//
planTaskService.taskMessage(null);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}else{
planTaskService.taskMessage(null);
}
}
}
......@@ -30,6 +30,7 @@ import org.redisson.api.RLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.ObjectUtils;
......@@ -71,9 +72,12 @@ public class PlanTaskController extends AbstractBaseController {
/* @Autowired
private IUserService userService;*/
@Autowired
@Autowired(required = false)
org.redisson.api.RedissonClient redisson;
@Value("${spring.redis.mode}")
private String cluster;
@Autowired
private RedisUtils redisUtils;
......@@ -418,7 +422,7 @@ public class PlanTaskController extends AbstractBaseController {
@RequestMapping(value = "/queryOmission", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public CommonResponse pushCarData() {
if("cluster".equals(cluster)){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time=format.format(new Date());
String jobName = "pushCarData" +time;
......@@ -446,6 +450,11 @@ public class PlanTaskController extends AbstractBaseController {
}finally {
lock.unlock(); //释放锁
}
}else{
RequestAttributes reqs = RequestContextHolder.getRequestAttributes();
RequestContextHolder.setRequestAttributes(reqs, true);
planTaskService.taskExecution(null);
}
return CommonResponseUtil.success();
}
......@@ -454,26 +463,32 @@ public class PlanTaskController extends AbstractBaseController {
*/
@Scheduled(cron = "${jobs.cron}")
public void taskMessage() {
String jobName = "taskMessage" + System.currentTimeMillis();
//为了便于区分key,增加后缀_redisson
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
//为了防止重复
if (!redisUtils.hasKey(jobName + "_taskMessage_key")) {
redisUtils.set(jobName + "_taskMessage_key", "1");//增加标识
RequestAttributes reqs = RequestContextHolder.getRequestAttributes();
RequestContextHolder.setRequestAttributes(reqs, true);
planTaskService.taskMessage(null);
if("cluster".equals(cluster)) {
String jobName = "taskMessage" + System.currentTimeMillis();
//为了便于区分key,增加后缀_redisson
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
//为了防止重复
if (!redisUtils.hasKey(jobName + "_taskMessage_key")) {
redisUtils.set(jobName + "_taskMessage_key", "1");//增加标识
RequestAttributes reqs = RequestContextHolder.getRequestAttributes();
RequestContextHolder.setRequestAttributes(reqs, true);
planTaskService.taskMessage(null);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}else{
RequestAttributes reqs = RequestContextHolder.getRequestAttributes();
RequestContextHolder.setRequestAttributes(reqs, true);
planTaskService.taskMessage(null);
}
}
/**
......
......@@ -96,8 +96,11 @@ public class JobService implements IJobService {
@Autowired
JcsFeignClient jcsFeignClient;
@Autowired
@Autowired(required = false)
org.redisson.api.RedissonClient redisson;
@Value("${spring.redis.mode}")
private String cluster;
@Autowired
private RedisUtils redisUtils;
......@@ -658,6 +661,8 @@ public class JobService implements IJobService {
@Override
@Transactional
public void taskJobPerform(long taskId, String jobType, String jobName) {
if("cluster".equals(cluster)){
// TODO Auto-generated method stub
//为了便于区分key,增加后缀_redisson
RLock lock = redisson.getLock(jobName);
......@@ -707,6 +712,40 @@ public class JobService implements IJobService {
} finally {
lock.unlock(); //释放锁
}
}else{
if (iTaskDao.existsById(taskId)) {
Task task = iTaskDao.findById(taskId).get();
Toke toke = remoteSecurityService.getServerToken();
if (XJConstant.STATUS_MONITOR_END.equals(jobType)) {
if (TaskStatusEnum.UNDERWAY.getValue() == task.getStatus()) {
task.setStatus(TaskStatusEnum.OVERTIME.getValue());
iTaskDao.saveAndFlush(task);
TaskFeedback taskFeedback = new TaskFeedback();
taskFeedback.setUserId(task.getExecutorId());
taskFeedback.setMessage("该任务在规定时间内未完成,请核实信息!任务名称: " + task.getTitle() + " 要求完成时间: "
+ DateUtil.getDateFormat(task.getFinishTime(), "yyyy-MM-dd HH:mm:ss") + " 发起人:"
+ task.getPublisherName() + " 执行人:" + task.getExecutor());
taskFeedback.setCreateDate(new Date());
taskFeedback.setUserName(task.getExecutor());
taskFeedback.setFeedbackTime(new Date());
taskFeedback.setOrgCode(task.getOrgCode());
taskFeedback.setTaskId(task.getId());
taskFeedback.setMessageType(TaskStatusEnum.OVERTIME.getName());
taskFeedback = taskFeedbackDao.save(taskFeedback);
try {
asyncTask.pushTaskDetailInfoTo3D(toke.getToke(), toke.getProduct(), toke.getAppKey(), taskId); // 超时任务向3D推送
} catch (InterruptedException e) {
// TODO Auto-generated catch block
log.error(e.getMessage(), e);
e.printStackTrace();
}
}
} else {
messageService.pushTaskMessage(toke.getToke(), toke.getProduct(), toke.getAppKey(), task);
}
}
removeJob(jobName);
}
}
......@@ -765,38 +804,58 @@ public class JobService implements IJobService {
@Override
@Transactional
public void planTaskJobPerform(long planTaskId, String jobType, String jobName) {
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
System.out.println("获取锁成功============");
if(!redisUtils.hasKey(jobName+"_redisson_key")){
redisUtils.set(jobName+"_redisson_key","1");//增加标识
if (iPlanTaskDao.existsById(planTaskId)) {
PlanTask planTask = iPlanTaskDao.findById(planTaskId).get();
if (XJConstant.STATUS_MONITOR_START.equals(jobType)) {
if (PlanTaskFinishStatusEnum.NOTSTARTED.getValue() == planTask.getFinishStatus()) {
planTask.setFinishStatus(PlanTaskFinishStatusEnum.UNDERWAY.getValue());
iPlanTaskDao.save(planTask);
}
} else if (XJConstant.STATUS_MONITOR_END.equals(jobType)) {
if (PlanTaskFinishStatusEnum.UNDERWAY.getValue() == planTask.getFinishStatus()) {
updatePlanTaskStatus(planTask, PlanTaskFinishStatusEnum.OVERTIME.getValue());
if("cluster".equals(cluster)) {
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
System.out.println("获取锁成功============");
if (!redisUtils.hasKey(jobName + "_redisson_key")) {
redisUtils.set(jobName + "_redisson_key", "1");//增加标识
if (iPlanTaskDao.existsById(planTaskId)) {
PlanTask planTask = iPlanTaskDao.findById(planTaskId).get();
if (XJConstant.STATUS_MONITOR_START.equals(jobType)) {
if (PlanTaskFinishStatusEnum.NOTSTARTED.getValue() == planTask.getFinishStatus()) {
planTask.setFinishStatus(PlanTaskFinishStatusEnum.UNDERWAY.getValue());
iPlanTaskDao.save(planTask);
}
} else if (XJConstant.STATUS_MONITOR_END.equals(jobType)) {
if (PlanTaskFinishStatusEnum.UNDERWAY.getValue() == planTask.getFinishStatus()) {
updatePlanTaskStatus(planTask, PlanTaskFinishStatusEnum.OVERTIME.getValue());
}
} else {
Toke toke = remoteSecurityService.getServerToken();
messageService.pushPlanTaskMessage(toke.getToke(), toke.getProduct(), toke.getAppKey(), planTask, jobType);
}
}
removeJob(jobName);
}
} else {
Toke toke= remoteSecurityService.getServerToken();
messageService.pushPlanTaskMessage(toke.getToke(), toke.getProduct(), toke.getAppKey(),planTask, jobType);
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}
}
removeJob(jobName);
}else{
if (iPlanTaskDao.existsById(planTaskId)) {
PlanTask planTask = iPlanTaskDao.findById(planTaskId).get();
if (XJConstant.STATUS_MONITOR_START.equals(jobType)) {
if (PlanTaskFinishStatusEnum.NOTSTARTED.getValue() == planTask.getFinishStatus()) {
planTask.setFinishStatus(PlanTaskFinishStatusEnum.UNDERWAY.getValue());
iPlanTaskDao.save(planTask);
}
} else if (XJConstant.STATUS_MONITOR_END.equals(jobType)) {
if (PlanTaskFinishStatusEnum.UNDERWAY.getValue() == planTask.getFinishStatus()) {
updatePlanTaskStatus(planTask, PlanTaskFinishStatusEnum.OVERTIME.getValue());
}
} else {
Toke toke = remoteSecurityService.getServerToken();
messageService.pushPlanTaskMessage(toke.getToke(), toke.getProduct(), toke.getAppKey(), planTask, jobType);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
removeJob(jobName);
}
}
@Override
......@@ -806,27 +865,35 @@ public class JobService implements IJobService {
@Override
public void msgJobPerform(long msgId, String jobType, String jobName) {
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
//为了防止重复
if(!redisUtils.hasKey(jobName+"_redisson_key")){
redisUtils.set(jobName+"_redisson_key","1");//增加标识
if (iMsgDao.existsById(msgId)) {
Msg msg = iMsgDao.findById(msgId).get();
Toke toke= remoteSecurityService.getServerToken();
messageService.pushMsgAndSave(toke.getToke(), toke.getProduct(), toke.getAppKey(),msg);
}
removeJob(jobName);
if("cluster".equals(cluster)) {
RLock lock = redisson.getLock(jobName);
try {
//拿锁失败10停止尝试, 任务执行超时默认续30s 每个10秒续到30
lock.tryLock(10, TimeUnit.SECONDS);
//为了防止重复
if (!redisUtils.hasKey(jobName + "_redisson_key")) {
redisUtils.set(jobName + "_redisson_key", "1");//增加标识
if (iMsgDao.existsById(msgId)) {
Msg msg = iMsgDao.findById(msgId).get();
Toke toke = remoteSecurityService.getServerToken();
messageService.pushMsgAndSave(toke.getToke(), toke.getProduct(), toke.getAppKey(), msg);
}
removeJob(jobName);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}else{
if (iMsgDao.existsById(msgId)) {
Msg msg = iMsgDao.findById(msgId).get();
Toke toke = remoteSecurityService.getServerToken();
messageService.pushMsgAndSave(toke.getToke(), toke.getProduct(), toke.getAppKey(), msg);
}
removeJob(jobName);
}
}
}
......@@ -3,6 +3,7 @@ import com.yeejoin.amos.boot.biz.common.utils.RedisUtils;
import com.yeejoin.amos.patrol.business.service.intfc.ILatentDangerService;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
......@@ -21,9 +22,12 @@ public class LatentDanerScheduled {
@Autowired
private ILatentDangerService iLatentDangerService;
@Autowired
@Autowired(required = false)
org.redisson.api.RedissonClient redisson;
@Value("${spring.redis.mode}")
private String cluster;
@Autowired
private RedisUtils redisUtils;
......@@ -34,6 +38,7 @@ public class LatentDanerScheduled {
@Scheduled(cron = "0 0/1 * * * ?")
public void updateDangerStateOfOvertime() {
if("cluster".equals(cluster)){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time=format.format(new Date());
String jobName = "updateDangerStateOfOvertime" +time;
......@@ -55,7 +60,9 @@ public class LatentDanerScheduled {
} finally {
lock.unlock(); //释放锁
}
}else{
iLatentDangerService.updateDangerStateOfOvertime();
}
......
......@@ -4,6 +4,7 @@ import com.yeejoin.amos.boot.biz.common.utils.RedisUtils;
import com.yeejoin.amos.supervision.business.service.intfc.IPlanTaskService;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
......@@ -20,8 +21,11 @@ import java.util.concurrent.TimeUnit;
@EnableScheduling
public class PlanTaskJob {
@Autowired
@Autowired(required = false)
org.redisson.api.RedissonClient redisson;
@Value("${spring.redis.mode}")
private String cluster;
@Autowired
private RedisUtils redisUtils;
......@@ -34,7 +38,7 @@ public class PlanTaskJob {
@Scheduled(cron = "${jobs.cron}")
public void scheduleJob() {
if("cluster".equals(cluster)){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time=format.format(new Date());
String jobName = "supervision-1" +time;
......@@ -56,5 +60,8 @@ public class PlanTaskJob {
lock.unlock(); //释放锁
}
}else{
planTaskService.taskExecution(null);
}
}
}
......@@ -186,12 +186,11 @@ spring.redis.database=1
spring.redis.timeout=10000
spring.redis.password=yeejoin@2020
spring.redis.expire.time=300
#单机集群判断 cluster集群 single单机
#单机集群判断 cluster
spring.redis.mode=single
#redis 单机配置
spring.redis.host=172.16.10.211
spring.redis.port=6379
spring.redis.redissonUrl=172.16.10.211:6379
......
......@@ -175,7 +175,6 @@ spring.redis.mode=single
#redis 单机配置
spring.redis.host=172.16.11.20
spring.redis.port=6379
spring.redis.redissonUrl=172.16.11.20:6379
spring.data.elasticsearch.repositories.enabled=true
......
......@@ -81,7 +81,6 @@ spring.redis.mode=single
#redis 单机配置
spring.redis.host=172.16.11.20
spring.redis.port=6379
spring.redis.redissonUrl=172.16.11.20:6379
#注册中心地址
eureka.client.service-url.defaultZone =http://172.16.10.72:10001/eureka/
......
......@@ -146,7 +146,6 @@ spring.redis.mode=single
#redis 单机配置
spring.redis.host=172.16.11.20
spring.redis.port=6379
spring.redis.redissonUrl=172.16.11.20:6379
......
......@@ -124,7 +124,6 @@ spring.redis.mode=single
#redis 单机配置
spring.redis.host=172.16.11.20
spring.redis.port=6379
spring.redis.redissonUrl=172.16.11.20:6379
......
......@@ -151,7 +151,6 @@ spring.redis.mode=single
#redis 单机配置
spring.redis.host=172.16.11.20
spring.redis.port=6379
spring.redis.redissonUrl=172.16.11.20:6379
......
......@@ -129,7 +129,6 @@ spring.redis.mode=single
#redis 单机配置
spring.redis.host=172.16.10.211
spring.redis.port=6379
spring.redis.redissonUrl=172.16.10.211:6379
......
......@@ -85,7 +85,6 @@ spring.redis.jedis.pool.min-idle=1
#redis 单机配置
spring.redis.host=172.16.11.20
spring.redis.port=6379
spring.redis.redissonUrl=172.16.11.20:6379
spring.data.elasticsearch.repositories.enabled=true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment