您的位置:首页 > 科技 > 能源 > 绍兴免费做网站_贪玩网页游戏大全_竞价广告是什么意思_seo关键词优化要多少钱

绍兴免费做网站_贪玩网页游戏大全_竞价广告是什么意思_seo关键词优化要多少钱

2024/9/22 8:11:54 来源:https://blog.csdn.net/csdnxyy/article/details/142366343  浏览:    关键词:绍兴免费做网站_贪玩网页游戏大全_竞价广告是什么意思_seo关键词优化要多少钱
绍兴免费做网站_贪玩网页游戏大全_竞价广告是什么意思_seo关键词优化要多少钱

1.直接在方法上加@Async注解,标明为异步任务:

@Component
public class MyAsyncTask {@Asyncpublic void asyncImportTask(String jsonList){//...具体业务逻辑//从redis缓存中查询数据(使用若依框架自带的redis方法)Data redisData = RedisUtils.getCacheObject(DataEnum.NUMONE.getValue());if(ObjectUtil.isEmpty(redisData)){//将数据查询后存入redis缓存1HRedisUtils.setCacheObject(DataEnum.NUMONE.getValue(), Data, Duration.ofHours(1));}}
}

2.设置默认的Async的线程,且开启异步配置,即配置上@EnableAsync注解:

@EnableAsync(proxyTargetClass = true)
@Configuration
public class AsyncConfig extends AsyncConfigurerSupport {@Autowired@Qualifier("scheduledExecutorService")private ScheduledExecutorService scheduledExecutorService;/*** 自定义 @Async 注解使用系统线程池*/@Overridepublic Executor getAsyncExecutor() {return scheduledExecutorService;}/*** 异步执行异常处理*/@Overridepublic AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {return (throwable, method, objects) -> {throwable.printStackTrace();StringBuilder sb = new StringBuilder();sb.append("Exception message - ").append(throwable.getMessage()).append(", Method name - ").append(method.getName());if (ArrayUtil.isNotEmpty(objects)) {sb.append(", Parameter value - ").append(Arrays.toString(objects));}throw new ServiceException(sb.toString());};}
}

3.使用的线程池相关配置:

//线程池配置
@Configuration
public class ThreadPoolConfig {/*** 核心线程数 = cpu 核心数 + 1*/private final int core = Runtime.getRuntime().availableProcessors() + 1;@Autowiredprivate ThreadPoolProperties threadPoolProperties;@Bean(name = "threadPoolTaskExecutor")@ConditionalOnProperty(prefix = "thread-pool", name = "enabled", havingValue = "true")public ThreadPoolTaskExecutor threadPoolTaskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(core);executor.setMaxPoolSize(core * 2);executor.setQueueCapacity(threadPoolProperties.getQueueCapacity());executor.setKeepAliveSeconds(threadPoolProperties.getKeepAliveSeconds());executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());return executor;}/*** 执行周期性或定时任务*/@Bean(name = "scheduledExecutorService")protected ScheduledExecutorService scheduledExecutorService() {return new ScheduledThreadPoolExecutor(core,new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build(),new ThreadPoolExecutor.CallerRunsPolicy()) {@Overrideprotected void afterExecute(Runnable r, Throwable t) {super.afterExecute(r, t);Threads.printException(r, t);}};}
}

4.线程池配置属性

//线程池配置属性
@Data
@Component
@ConfigurationProperties(prefix = "thread-pool")
public class ThreadPoolProperties {/*** 是否开启线程池*/private boolean enabled;/*** 队列最大长度*/private int queueCapacity;/*** 线程池维护线程所允许的空闲时间*/private int keepAliveSeconds;}

5.系统全局线程池配置

# 全局线程池相关配置
thread-pool:# 是否开启线程池enabled: true# 队列最大长度queueCapacity: 256# 线程池维护线程所允许的空闲时间keepAliveSeconds: 300

注意:调用被@Async注解的方法时,调用方法不能与被调用方法放在同一类中,否则注解不生效

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com