Commit 36817078 by 王祖波

Merge branch 'refs/heads/feature-chat-activity' into developer

# Conflicts:
#	haoban-manage3-service/src/main/java/com/gic/haoban/manage/service/util/ApolloUtils.java
parents 58a90b7d 6f8d4f95
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.service.util.ApolloUtils;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -27,6 +33,8 @@ import java.util.concurrent.TimeUnit;
*/
public class ImageCombined {
private static final String APPLICATION = "application";
private static final Logger logger = LoggerFactory.getLogger(ImageCombined.class);
private static final Cache<String, BufferedImage> imageCache = Caffeine.newBuilder()
......@@ -44,6 +52,27 @@ public class ImageCombined {
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:";
private static final int MAX_HEIGHT_NO_WHITE_SPACE = 1500;
......
......@@ -6,6 +6,8 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
public class ApolloUtils {
private static final Logger log = LoggerFactory.getLogger(ApolloUtils.class);
......@@ -45,4 +47,24 @@ public class ApolloUtils {
log.info("导购通知一批次间隔时间:{}", notifyBatchInterval);
return Integer.parseInt(notifyBatchInterval);
}
/**
* 线程池核心线程数
* @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);
}
}
......@@ -55,8 +55,9 @@ public class QWmediaTest {
}
public static void main(String[] args) {
int TOTAL_REQUESTS = 100; // 总请求数
int TOTAL_REQUESTS = 1800; // 总请求数
List<String> list = FileUtil.readLines("/Users/wang/Downloads/image.txt", "UTF-8");
List<String> qrList = FileUtil.readLines("/Users/wang/Downloads/qrcode.txt", "UTF-8");
JSONObject jo = new JSONObject();
jo.put("wxEnterpriseId", "b18ffdc9d0644912865a248859914d80");
jo.put("qrCodeUrl", "https://gicinner-1251519181.cos.ap-shanghai.myqcloud.com/image/material_content-4ffc77073ca1476fb264bf1be9f11383.png");
......@@ -65,8 +66,8 @@ public class QWmediaTest {
jo.put("lineTwo", "宇智222为您推荐");
jo.put("mediaType", 1);
ExecutorService executorService = Executors.newFixedThreadPool(50); // 创建线程池
Semaphore semaphore = new Semaphore(50); // 限制并发请求数为50
ExecutorService executorService = Executors.newFixedThreadPool(30); // 创建线程池
Semaphore semaphore = new Semaphore(30); // 限制并发请求数为30
// 用于统计请求耗时
AtomicLong totalTime = new AtomicLong(0);
......@@ -74,13 +75,15 @@ public class QWmediaTest {
AtomicLong minTime = new AtomicLong(Long.MAX_VALUE);
for (int i = 0; i < TOTAL_REQUESTS; i++) {
final int requestId = i;
final int imageId = i % 9;
final int qrId = i / 9;
executorService.submit(() -> {
long startTime = System.currentTimeMillis();
try {
semaphore.acquire(); // 获取许可,控制并发请求
jo.put("imageUrl", list.get(requestId));
HttpResponse execute = HttpRequest.post("https://www.gicdev.com/haoban-manage3-wx/combined-qw-materialid.json").timeout(1000000).body(jo.toJSONString()).execute();
jo.put("imageUrl", list.get(imageId));
jo.put("qrCodeUrl", qrList.get(qrId));
HttpResponse execute = HttpRequest.post("https://www.gicdev.com/haoban-manage3-wx/combined-qw-materialid.json").timeout(-1).body(jo.toJSONString()).execute();
String body = execute.body();
String headerValue = execute.header("x-request-id");
// String post = HttpUtil.post("https://www.gicdev.com/haoban-manage3-wx/combined-qw-materialid.json", jo.toJSONString());
......@@ -90,6 +93,7 @@ public class QWmediaTest {
System.out.println("异常: " + e.getMessage());
} finally {
long duration = System.currentTimeMillis() - startTime;
System.out.println(duration);
totalTime.addAndGet(duration);
maxTime.updateAndGet(x -> Math.max(x, duration));
minTime.updateAndGet(x -> Math.min(x, duration));
......
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