package com.meritdata.cloud.cosp.proflieboard.utils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@EnableAsync
@Configuration
public class ThreadConfig {
@Bean("taskExecutor")
public ThreadPoolTaskExecutor asyncServiceExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(20);
executor.setQueueCapacity(Integer.MAX_VALUE);
executor.setKeepAliveSeconds(60);
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.initialize();
return executor;
}
}
void excuteGroupProfile(String id);
@Async("taskExecutor")
@Override
public void excuteGroupProfile(String id) {
Map groupInfo = null;
try {
groupInfo = this.getByGroupId(id);
redisTemplate.opsForValue().set(id,groupInfo,1, TimeUnit.DAYS);
} catch (Exception e) {
log.error("查询客群信息失败",e);
}
log.debug("客群id:{},查询客群数据:{}", id, groupInfo);
}
@ApiOperation("根据客群id获取特征数据")
@GetMapping("get-by-id")
public ResultBody getByGroupId(String id) {
try {
Object value = redisTemplate.opsForValue().get(id);
if (value != null) {
return ResultBody.success(value);
} else {
groupProfileService.excuteGroupProfile(id);
return ResultBody.success("正在查询中,请稍后查看");
}
} catch (Exception e) {
log.error("查询客群画像失败!", e);
return ResultBody.failure("查询客群画像失败!");
}
}
ThreadPoolTaskExecutor cachedThreadPool = threadConfig.asyncServiceExecutor();
cachedThreadPool.execute(new Runnable() {
@Override
public void run() {
iRabbitmqMoneyService.process(data);
}
});
cachedThreadPool.execute(new Runnable() {
@Override
public void run() {
iRabbitCustGroupService.process(data);
}
});