package com.qmth.boot.redis.schedule;

import com.qmth.boot.core.schedule.config.ScheduleProperties;
import com.qmth.boot.redis.constant.RedisConstant;
import org.redisson.api.RScheduledExecutorService;
import org.redisson.api.RedissonClient;
import org.redisson.api.WorkerOptions;
import org.redisson.codec.JsonJacksonCodec;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.TaskScheduler;

//@Configuration
public class RedisScheduleAutoConfiguration implements RedisConstant {

    //@Bean(destroyMethod = "")
    public RScheduledExecutorService scheduledExecutorService(RedissonClient redissonClient,
            ScheduleProperties scheduleProperties, BeanFactory beanFactory) {
        RScheduledExecutorService scheduledExecutorService = redissonClient
                .getExecutorService(SCHEDULE_KEY, JsonJacksonCodec.INSTANCE);
        scheduledExecutorService.registerWorkers(
                WorkerOptions.defaults().workers(scheduleProperties.getPoolSize()).beanFactory(beanFactory));
        return scheduledExecutorService;
    }

    //@Bean(destroyMethod = "")
    public TaskScheduler taskScheduler(RScheduledExecutorService scheduledExecutorService,
            ApplicationContext applicationContext) {
        return new RedisTaskScheduler(scheduledExecutorService, applicationContext);
    }
}
