|
|
@@ -3,9 +3,11 @@ package com.fs.quartz.config;
|
|
|
import com.fs.common.constant.ScheduleConstants;
|
|
|
import com.fs.quartz.support.ScheduleJobSyncSubscriber;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
import org.springframework.data.redis.listener.ChannelTopic;
|
|
|
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
|
|
@@ -16,14 +18,15 @@ import java.util.concurrent.TimeUnit;
|
|
|
/**
|
|
|
* 定时任务 Redis 订阅配置:管理端修改 sys_job 后通过频道通知 fs-task 刷新 Quartz。
|
|
|
* <p>
|
|
|
- * 通过 spring.redis.listener.enabled 控制是否启用(默认 false)。
|
|
|
+ * 通过 spring.redis.listener.enabled 控制是否启用(默认启用)。
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Configuration
|
|
|
@ConditionalOnProperty(name = "spring.redis.listener.enabled", havingValue = "true", matchIfMissing = true)
|
|
|
+@ConditionalOnBean(RedisConnectionFactory.class)
|
|
|
public class ScheduleJobRedisConfig {
|
|
|
|
|
|
- @Bean
|
|
|
+ @Bean(destroyMethod = "stop")
|
|
|
public RedisMessageListenerContainer scheduleJobRedisListenerContainer(
|
|
|
RedisConnectionFactory connectionFactory,
|
|
|
ScheduleJobSyncSubscriber scheduleJobSyncSubscriber) {
|
|
|
@@ -31,10 +34,28 @@ public class ScheduleJobRedisConfig {
|
|
|
container.setConnectionFactory(connectionFactory);
|
|
|
container.addMessageListener(scheduleJobSyncSubscriber,
|
|
|
new ChannelTopic(ScheduleConstants.REDIS_CHANNEL_JOB_SYNC));
|
|
|
- // 增加超时时间到30秒,避免启动时 Redis 连接未就绪导致超时
|
|
|
- container.setMaxSubscriptionRegistrationWaitingTime(TimeUnit.SECONDS.toMillis(30));
|
|
|
+ // 设置较长的超时时间,但使用异步启动避免阻塞
|
|
|
+ container.setMaxSubscriptionRegistrationWaitingTime(TimeUnit.SECONDS.toMillis(60));
|
|
|
container.setTaskExecutor(Executors.newFixedThreadPool(2, r -> new Thread(r, "job-sync-listener")));
|
|
|
- log.info("[ScheduleJobRedis] Redis pub/sub 监听器已启用,频道={}", ScheduleConstants.REDIS_CHANNEL_JOB_SYNC);
|
|
|
+ log.info("[ScheduleJobRedis] Redis pub/sub 监听器已配置,频道={}", ScheduleConstants.REDIS_CHANNEL_JOB_SYNC);
|
|
|
+ // 异步启动,不阻塞 Spring 启动流程
|
|
|
+ asyncStart(container);
|
|
|
return container;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 异步启动 Redis 监听器,避免阻塞 Spring 启动
|
|
|
+ */
|
|
|
+ private void asyncStart(RedisMessageListenerContainer container) {
|
|
|
+ Executors.newSingleThreadExecutor(r -> new Thread(r, "redis-listener-starter")).execute(() -> {
|
|
|
+ try {
|
|
|
+ // 延迟 5 秒,等待 Spring 完全启动
|
|
|
+ Thread.sleep(5000);
|
|
|
+ container.start();
|
|
|
+ log.info("[ScheduleJobRedis] Redis pub/sub 监听器启动成功");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("[ScheduleJobRedis] Redis pub/sub 监听器启动失败,应用仍可正常运行: {}", e.getMessage());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|