opt:定时下发任务优化

This commit is contained in:
DESKTOP-5DIJMF9\admin
2025-08-07 10:20:14 +08:00
parent d999a4a33c
commit 94c9baf2a6
2 changed files with 20 additions and 21 deletions

View File

@@ -16,7 +16,10 @@
package org.nl.config.thread; package org.nl.config.thread;
import org.nl.config.SpringContextHolder; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
@@ -27,16 +30,21 @@ import java.util.concurrent.TimeUnit;
* @author Zheng Jie * @author Zheng Jie
* @date 2019年10月31日18:16:47 * @date 2019年10月31日18:16:47
*/ */
@Configuration
public class ThreadPoolExecutorUtil { public class ThreadPoolExecutorUtil {
public static ThreadPoolExecutor getPoll(){ @Autowired
AsyncTaskProperties properties = SpringContextHolder.getBean(AsyncTaskProperties.class); private AsyncTaskProperties asyncTaskProperties;
@Bean
@Primary
public ThreadPoolExecutor threadPoolExecutor() {
return new ThreadPoolExecutor( return new ThreadPoolExecutor(
properties.getCorePoolSize(), asyncTaskProperties.getCorePoolSize(),
properties.getMaxPoolSize(), asyncTaskProperties.getMaxPoolSize(),
properties.getKeepAliveSeconds(), asyncTaskProperties.getKeepAliveSeconds(),
TimeUnit.SECONDS, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(properties.getQueueCapacity()), new ArrayBlockingQueue<>(asyncTaskProperties.getQueueCapacity()),
new TheadFactoryName() new TheadFactoryName()
); );
} }

View File

@@ -11,13 +11,11 @@ import org.nl.wms.sch_manage.enums.TaskStatus;
import org.nl.wms.sch_manage.service.ISchBaseTaskService; import org.nl.wms.sch_manage.service.ISchBaseTaskService;
import org.nl.wms.sch_manage.service.dao.SchBaseTask; import org.nl.wms.sch_manage.service.dao.SchBaseTask;
import org.nl.wms.warehouse_manage.enums.IOSConstant; import org.nl.wms.warehouse_manage.enums.IOSConstant;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
@Slf4j @Slf4j
@Component @Component
@@ -40,17 +38,9 @@ public class AutoTask {
//定时任务 //定时任务
@SneakyThrows @SneakyThrows
public void run() { public void run() {
RLock lock = redissonClient.getLock(this.getClass().getName()); log.info("定时器正在执行下发任务调度...");
boolean tryLock = lock.tryLock(0, TimeUnit.SECONDS); sendTask();
try { log.info("定时器执行下发任务调度结束...");
if (tryLock) {
sendTask();
}
} catch (Exception e) {
if (tryLock) {
lock.unlock();
}
}
} }
/** /**
@@ -60,7 +50,8 @@ public class AutoTask {
System.out.println("定时下发任务开始--------------------------"+ DateUtil.now()); System.out.println("定时下发任务开始--------------------------"+ DateUtil.now());
List<SchBaseTask> taskList = taskService.list(new LambdaQueryWrapper<SchBaseTask>() List<SchBaseTask> taskList = taskService.list(new LambdaQueryWrapper<SchBaseTask>()
.eq(SchBaseTask::getIs_delete, IOSConstant.IS_DELETE_NO) .eq(SchBaseTask::getIs_delete, IOSConstant.IS_DELETE_NO)
.eq(SchBaseTask::getTask_status, TaskStatus.CREATE.getCode())); .eq(SchBaseTask::getTask_status, TaskStatus.CREATE.getCode())
.orderByAsc(SchBaseTask::getTask_group_seq));
if (ObjectUtil.isEmpty(taskList)) { if (ObjectUtil.isEmpty(taskList)) {
return; return;
} }