Commit 6f8d4f95 by 王祖波

动态调整线程池

parent b3e11c5c
package com.gic.haoban.manage.service.context.combined; package com.gic.haoban.manage.service.context.combined;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.gic.haoban.manage.api.qdto.combined.CombinedQDTO; import com.gic.haoban.manage.api.qdto.combined.CombinedQDTO;
import com.gic.haoban.manage.service.util.ApolloUtils;
import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.Caffeine;
import net.coobird.thumbnailator.Thumbnails; import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -27,6 +33,8 @@ import java.util.concurrent.TimeUnit; ...@@ -27,6 +33,8 @@ import java.util.concurrent.TimeUnit;
*/ */
public class ImageCombined { public class ImageCombined {
private static final String APPLICATION = "application";
private static final Logger logger = LoggerFactory.getLogger(ImageCombined.class); private static final Logger logger = LoggerFactory.getLogger(ImageCombined.class);
private static final Cache<String, BufferedImage> imageCache = Caffeine.newBuilder() private static final Cache<String, BufferedImage> imageCache = Caffeine.newBuilder()
...@@ -44,6 +52,27 @@ public class ImageCombined { ...@@ -44,6 +52,27 @@ public class ImageCombined {
new ThreadPoolExecutor.AbortPolicy() new ThreadPoolExecutor.AbortPolicy()
); );
private ImageCombined() {
Config config = ConfigService.getAppConfig();
config.addChangeListener(this::refreshThreadPool);
}
private void refreshThreadPool(ConfigChangeEvent changeEvent) {
if (!StringUtils.equals(changeEvent.getNamespace(), APPLICATION)) {
return;
}
ConfigChange combinedCorePoolSizeChange = changeEvent.getChange("combinedCorePoolSize");
ConfigChange combinedMaximumPoolSizeChange = changeEvent.getChange("combinedMaximumPoolSize");
if ((combinedCorePoolSizeChange != null && combinedCorePoolSizeChange.getNewValue() != null)
|| (combinedMaximumPoolSizeChange != null && combinedMaximumPoolSizeChange.getNewValue() != null)) {
Integer corePoolSize = ApolloUtils.combinedCorePoolSize();
Integer maximumPoolSize = ApolloUtils.combinedMaximumPoolSize();
logger.info("动态调整线程池corePoolSize:{},maximumPoolSize:{}", corePoolSize, maximumPoolSize);
EXECUTOR.setCorePoolSize(corePoolSize);
EXECUTOR.setMaximumPoolSize(maximumPoolSize);
}
}
public static final String IMAGE_REDIS_KEY = "haoban-manage3-service:combined_image:"; public static final String IMAGE_REDIS_KEY = "haoban-manage3-service:combined_image:";
private static final int MAX_HEIGHT_NO_WHITE_SPACE = 1500; private static final int MAX_HEIGHT_NO_WHITE_SPACE = 1500;
......
...@@ -3,6 +3,8 @@ package com.gic.haoban.manage.service.util; ...@@ -3,6 +3,8 @@ package com.gic.haoban.manage.service.util;
import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService; import com.ctrip.framework.apollo.ConfigService;
import java.util.Map;
public class ApolloUtils { public class ApolloUtils {
/** /**
...@@ -14,4 +16,24 @@ public class ApolloUtils { ...@@ -14,4 +16,24 @@ public class ApolloUtils {
String messageCenterSwitch = config.getProperty("messageCenterSwitch", "0"); String messageCenterSwitch = config.getProperty("messageCenterSwitch", "0");
return "1".equals(messageCenterSwitch); return "1".equals(messageCenterSwitch);
} }
/**
* 线程池核心线程数
* @return
*/
public static Integer combinedCorePoolSize() {
Config config = ConfigService.getAppConfig();
String corePoolSize = config.getProperty("combinedCorePoolSize", "20");
return Integer.parseInt(corePoolSize);
}
/**
* 线程池最大线程数
* @return
*/
public static Integer combinedMaximumPoolSize() {
Config config = ConfigService.getAppConfig();
String maximumPoolSize = config.getProperty("combinedMaximumPoolSize", "40");
return Integer.parseInt(maximumPoolSize);
}
} }
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