|
|
@ -4,8 +4,11 @@ import com.inspect.analysis.constant.AnalyseConstants; |
|
|
import com.inspect.base.redis.service.RedisService; |
|
|
import com.inspect.base.redis.service.RedisService; |
|
|
import com.inspect.partrolresult.domain.AnalyseRequest; |
|
|
import com.inspect.partrolresult.domain.AnalyseRequest; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
import org.redisson.RedissonShutdownException; |
|
|
import org.redisson.api.RBlockingQueue; |
|
|
import org.redisson.api.RBlockingQueue; |
|
|
import org.redisson.api.RedissonClient; |
|
|
import org.redisson.api.RedissonClient; |
|
|
|
|
|
import org.redisson.client.RedisException; |
|
|
|
|
|
import org.springframework.beans.factory.DisposableBean; |
|
|
import org.springframework.beans.factory.InitializingBean; |
|
|
import org.springframework.beans.factory.InitializingBean; |
|
|
import org.springframework.stereotype.Component; |
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
@ -13,8 +16,8 @@ import javax.annotation.Resource; |
|
|
|
|
|
|
|
|
@Slf4j |
|
|
@Slf4j |
|
|
@Component |
|
|
@Component |
|
|
public class AnalyseRequestDelayQueueListener implements InitializingBean { |
|
|
|
|
|
private final String ListenerName = "AnalyseRequest-DelayQueue-Listener"; |
|
|
|
|
|
|
|
|
public class AnalyseRequestDelayQueueListener implements InitializingBean, DisposableBean { |
|
|
|
|
|
private final String THREAD_NAME = "AnalyseRequest-DelayQueue-Listener"; |
|
|
|
|
|
|
|
|
@Resource |
|
|
@Resource |
|
|
private RedissonClient redissonClient; |
|
|
private RedissonClient redissonClient; |
|
|
@ -25,22 +28,44 @@ public class AnalyseRequestDelayQueueListener implements InitializingBean { |
|
|
@Resource |
|
|
@Resource |
|
|
private IAnalyseRequestService analyseRequestService; |
|
|
private IAnalyseRequestService analyseRequestService; |
|
|
|
|
|
|
|
|
|
|
|
private Thread listenerThread; |
|
|
|
|
|
private volatile boolean running = true; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void afterPropertiesSet() { |
|
|
public void afterPropertiesSet() { |
|
|
// 启动监听线程 |
|
|
// 启动监听线程 |
|
|
new Thread(() -> { |
|
|
|
|
|
RBlockingQueue<AnalyseRequest> blockingQueue = redissonClient.getBlockingQueue(AnalyseConstants.ANALYSE_REQ_DELAY_QUEUE); |
|
|
|
|
|
|
|
|
listenerThread = new Thread(() -> { |
|
|
|
|
|
RBlockingQueue<AnalyseRequest> blockingQueue = redissonClient.getBlockingQueue(AnalyseConstants.ALGORITHM_REQUEST_DELAY_QUEUE); |
|
|
log.info("Listening AnalyseRequest Delay Queue Task Launching"); |
|
|
log.info("Listening AnalyseRequest Delay Queue Task Launching"); |
|
|
|
|
|
|
|
|
while (true) { |
|
|
|
|
|
|
|
|
while (running && !Thread.currentThread().isInterrupted()) { |
|
|
try { |
|
|
try { |
|
|
AnalyseRequest request = blockingQueue.take(); // 阻塞等待任务到期 |
|
|
AnalyseRequest request = blockingQueue.take(); // 阻塞等待任务到期 |
|
|
handleRequest(request); |
|
|
handleRequest(request); |
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
|
} catch (RedissonShutdownException e) { |
|
|
|
|
|
log.warn("Redisson 已关闭,终止监听线程"); |
|
|
|
|
|
break; |
|
|
|
|
|
} catch (InterruptedException e) { |
|
|
|
|
|
Thread.currentThread().interrupt(); // 响应中断 |
|
|
|
|
|
log.info("监听线程被中断,正在关闭..."); |
|
|
|
|
|
break; |
|
|
|
|
|
} catch (RedisException e) { |
|
|
|
|
|
log.error("Redis 连接异常: {}", e.getMessage(), e); |
|
|
|
|
|
try { |
|
|
|
|
|
Thread.sleep(3000); // 避免异常频繁重试 |
|
|
|
|
|
} catch (InterruptedException ie) { |
|
|
|
|
|
Thread.currentThread().interrupt(); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
catch (Exception e) { |
|
|
log.error("Listening AnalyseRequest Queue Exception: ", e); |
|
|
log.error("Listening AnalyseRequest Queue Exception: ", e); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}, ListenerName).start(); |
|
|
|
|
|
|
|
|
}, THREAD_NAME); |
|
|
|
|
|
|
|
|
|
|
|
listenerThread.setDaemon(true); // 设置为守护线程,防止主程序不退出 |
|
|
|
|
|
listenerThread.start(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void handleRequest(AnalyseRequest request) { |
|
|
private void handleRequest(AnalyseRequest request) { |
|
|
@ -62,5 +87,14 @@ public class AnalyseRequestDelayQueueListener implements InitializingBean { |
|
|
request.getRequestId()); |
|
|
request.getRequestId()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void destroy() { |
|
|
|
|
|
log.info("正在关闭 AnalyseRequestDelayQueueListener..."); |
|
|
|
|
|
running = false; |
|
|
|
|
|
if (listenerThread != null) { |
|
|
|
|
|
listenerThread.interrupt(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|