package com. graborderboot. order. config;
import com. graborderboot. order. config. thread. IntegralEventExecutorRejectHandler;
import com. graborderboot. order. config. thread. ThreadExceptionHandle;
import org. springframework. beans. factory. annotation. Qualifier;
import org. springframework. context. annotation. Bean;
import org. springframework. context. annotation. Configuration;
import org. springframework. context. annotation. Primary;
import org. springframework. scheduling. annotation. EnableAsync;
import org. springframework. scheduling. concurrent. ThreadPoolTaskExecutor;
import java. util. concurrent. Executor;
import java. util. concurrent. Executors;
@Configuration
@EnableAsync
public class ExecutorConfig {
static {
Thread. setDefaultUncaughtExceptionHandler ( ThreadExceptionHandle. INSTANCE) ;
}
@Bean
@Primary
@Qualifier ( "defaultExecutor" )
public Executor defaultExecutor ( ) {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor ( ) ;
executor. setCorePoolSize ( 4 ) ;
executor. setMaxPoolSize ( 8 ) ;
executor. setQueueCapacity ( 30000 ) ;
executor. setKeepAliveSeconds ( 60 ) ;
executor. setAllowCoreThreadTimeOut ( true ) ;
executor. setThreadNamePrefix ( "defaultExecutor-" ) ;
executor. setAwaitTerminationSeconds ( 5 ) ;
executor. setWaitForTasksToCompleteOnShutdown ( true ) ;
executor. initialize ( ) ;
return executor;
}
@Bean
@Qualifier ( "fixedRateExecutor" )
public Executor fixedRateExecutor ( ) {
return Executors. newScheduledThreadPool ( 100 ) ;
}
@Bean
@Qualifier ( "integralEventExecutorLog" )
public Executor tradeEventExecutorLog ( ) {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor ( ) ;
executor. setCorePoolSize ( 7 ) ;
executor. setMaxPoolSize ( 7 ) ;
executor. setQueueCapacity ( 7000 ) ;
executor. setKeepAliveSeconds ( 10 ) ;
executor. setAllowCoreThreadTimeOut ( false ) ;
executor. setThreadNamePrefix ( "integralEventExecutorLog-" ) ;
executor. setRejectedExecutionHandler ( new IntegralEventExecutorRejectHandler ( ) ) ;
executor. setWaitForTasksToCompleteOnShutdown ( true ) ;
executor. setAwaitTerminationSeconds ( 5 ) ;
executor. initialize ( ) ;
return executor;
}
}
package com. graborderboot. order. schedule;
import cn. hutool. core. collection. CollUtil;
import com. alibaba. fastjson. JSON;
import com. graborderboot. order. chche. OrderScheduleCache;
import com. graborderboot. order. entity. UserModel;
import com. graborderboot. order. service. OrderService;
import lombok. extern. slf4j. Slf4j;
import org. apache. commons. lang3. StringUtils;
import org. joda. time. LocalDateTime;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. beans. factory. annotation. Qualifier;
import org. springframework. context. annotation. Configuration;
import org. springframework. scheduling. annotation. EnableScheduling;
import org. springframework. scheduling. annotation. Scheduled;
import org. springframework. scheduling. annotation. SchedulingConfigurer;
import org. springframework. scheduling. config. ScheduledTaskRegistrar;
import java. util. Calendar;
import java. util. Date;
import java. util. GregorianCalendar;
import java. util. List;
import java. util. concurrent. Executor;
@Configuration
@EnableScheduling
@Slf4j
public class OrderSchedule implements SchedulingConfigurer {
private volatile boolean isOpen = true ;
@Autowired
private OrderScheduleCache orderScheduleCache;
@Autowired
private OrderService orderService;
@Autowired
@Qualifier ( "fixedRateExecutor" )
public Executor taskExecutor;
@Scheduled ( cron = "0/5 * * * * ?" )
private void getToken ( ) {
if ( isOpen) {
log. info ( "开始刷新失效token" ) ;
this . orderService. refreshToken ( ) ;
}
}
@Override
public void configureTasks ( ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar. setScheduler ( this . taskExecutor) ;
taskRegistrar. addTriggerTask (
( ) - > {
if ( isOpen) {
List< UserModel> users = this . orderService. getUsefulUsers ( ) ;
log. info ( "执行动态定时任务运行中!: {},获取到的有效用户===>:{}" , LocalDateTime. now ( ) . toString ( ) , JSON. toJSON ( users) ) ;
if ( CollUtil. isNotEmpty ( users) ) {
users. parallelStream ( ) . forEach ( item - > this . orderService. getOrder ( item) ) ;
}
} else {
log. info ( "执行动态定时任务已停止!" ) ;
}
} ,
triggerContext - > {
Calendar nextExecutionTime = new GregorianCalendar ( ) ;
Date lastActualExecutionTime = triggerContext. lastActualExecutionTime ( ) ;
nextExecutionTime. setTime ( lastActualExecutionTime != null ? lastActualExecutionTime : new Date ( ) ) ;
String fixedRate = orderScheduleCache. getFixedRate ( "1" ) ;
if ( StringUtils. isEmpty ( fixedRate) ) {
fixedRate = "3000" ;
log. error ( "定时任务周期未配置!!!,fixedRate默认为{}毫秒" , fixedRate) ;
} else {
log. info ( "定时任务周期配置设置为 fixedRate={}毫秒" , fixedRate) ;
}
nextExecutionTime. add ( Calendar. MILLISECOND, Integer. parseInt ( fixedRate) ) ;
return nextExecutionTime. getTime ( ) ;
}
) ;
}
public synchronized void close ( ) {
this . isOpen = false ;
}
public synchronized void open ( ) {
this . isOpen = true ;
}
}