优化代码:程序退出后优雅结束程序,退出循环

master
xzxiaoshan 2 months ago
parent 606b1afeb6
commit 1908393aca

@ -9,9 +9,11 @@ import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.core.task.TaskExecutor; import org.springframework.core.task.TaskExecutor;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -67,6 +69,11 @@ public class SensitiveWordsConfig {
@Autowired @Autowired
private ResourceLoader resourceLoader; private ResourceLoader resourceLoader;
/**
* 线
*/
private volatile boolean stop = false;
/** /**
* *
*/ */
@ -120,7 +127,7 @@ public class SensitiveWordsConfig {
* *
*/ */
private void startScheduledUpdate() { private void startScheduledUpdate() {
while (true) { while (!stop) {
try { try {
TimeUnit.SECONDS.sleep(UPDATE_TIME_SECONDS); TimeUnit.SECONDS.sleep(UPDATE_TIME_SECONDS);
log.debug("SensitiveWordConfig#startScheduledUpdate start update..."); log.debug("SensitiveWordConfig#startScheduledUpdate start update...");
@ -128,9 +135,22 @@ public class SensitiveWordsConfig {
storeSensWords(); storeSensWords();
} catch (InterruptedException e) { } catch (InterruptedException e) {
log.error("SensitiveWordConfig#startScheduledUpdate interrupted: {}", e.getMessage()); log.error("SensitiveWordConfig#startScheduledUpdate interrupted: {}", e.getMessage());
Thread.currentThread().interrupt();
break; break;
} }
} }
} }
/**
* onDestroy
*/
@PreDestroy
public void onDestroy() {
stop = true;
if (taskExecutor instanceof ThreadPoolTaskExecutor) {
ThreadPoolTaskExecutor threadPoolTaskExecutor = (ThreadPoolTaskExecutor) taskExecutor;
threadPoolTaskExecutor.shutdown();
}
}
} }

@ -25,10 +25,15 @@ public class MessageReceipt {
@Autowired @Autowired
private List<ReceiptMessageStater> receiptMessageStaterList; private List<ReceiptMessageStater> receiptMessageStaterList;
/**
* 线
*/
private volatile boolean stop = false;
@PostConstruct @PostConstruct
private void init() { private void init() {
SupportThreadPoolConfig.getPendingSingleThreadPool().execute(() -> { SupportThreadPoolConfig.getPendingSingleThreadPool().execute(() -> {
while (true) { while (!stop) {
try { try {
for (ReceiptMessageStater receiptMessageStater : receiptMessageStaterList) { for (ReceiptMessageStater receiptMessageStater : receiptMessageStaterList) {
//receiptMessageStater.start(); //receiptMessageStater.start();
@ -50,6 +55,7 @@ public class MessageReceipt {
*/ */
@PreDestroy @PreDestroy
public void onDestroy() { public void onDestroy() {
this.stop = true;
SupportThreadPoolConfig.getPendingSingleThreadPool().shutdown(); SupportThreadPoolConfig.getPendingSingleThreadPool().shutdown();
} }

@ -48,6 +48,11 @@ public class RedisReceiver implements MessageReceiver {
*/ */
private ScheduledExecutorService scheduler; private ScheduledExecutorService scheduler;
/**
* 线
*/
private volatile boolean stop = false;
/** /**
* 线 * 线
*/ */
@ -92,7 +97,7 @@ public class RedisReceiver implements MessageReceiver {
* @param consumer * @param consumer
*/ */
private void receiveMessage(String topic, Consumer<String> consumer) { private void receiveMessage(String topic, Consumer<String> consumer) {
while (true) { while (!stop) {
try { try {
// 阻塞操作减少CPUIO消耗 // 阻塞操作减少CPUIO消耗
Optional<String> message = Optional.ofNullable( Optional<String> message = Optional.ofNullable(
@ -117,6 +122,7 @@ public class RedisReceiver implements MessageReceiver {
*/ */
@PreDestroy @PreDestroy
public void onDestroy() { public void onDestroy() {
stop = true;
scheduler.shutdown(); scheduler.shutdown();
} }

Loading…
Cancel
Save