Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

scheduler - Shedlock : LockableTaskScheduler is not lock my tasks

I am using and managing my jobs with scheduler and shedlock mechanism with spring and annotations. I have a requirement that need to do my tasks more dynamic. I will configure my jobs on db and automatically schedule with that. I did manage using taskScheduler with spring but can not use LockableTaskScheduler. My requirement is that every job different LockConfiguration each other.(I have multi node)

Normally i use multi nodes but for testing purpose i create same name jobs on different class post construct and watch if those will trigger together or not. But it does not work. Anyone idea that how LockableTaskScheduler works? I read the docs but no satisfied example of that.

public SchedulerManager(TaskScheduler taskScheduler,
                        ApplicationContext applicationContext) {
    this.taskScheduler = taskScheduler;
    this.applicationContext = applicationContext;
}

@PostConstruct
void load() {

    LockProvider lockProvider = (LockProvider) applicationContext.getBean("lockProvider");

    String name = "First Job!";

    LockConfiguration lockConfiguration = new LockConfiguration(Instant.now(), name, Duration.ofMinutes(1), Duration.ofMinutes(1));
    LockConfigurationExtractor lockConfigurationExtractor = new DefaultLockConfigurationExtractor(lockConfiguration);
    LockManager lockManager = new DefaultLockManager(lockProvider, lockConfigurationExtractor);
    LockableTaskScheduler lockableTaskScheduler = new LockableTaskScheduler(taskScheduler, lockManager);

    tasks.put(name, lockableTaskScheduler.schedule(() -> {
        System.out.println("NAME IS : " + name);
    }, new CronTrigger("20 * * * * *")));

    String name2 = "Second Job!";

    LockConfiguration lockConfiguration2 = new LockConfiguration(Instant.now(), name2, Duration.ofMinutes(1), Duration.ofMinutes(1));
    LockConfigurationExtractor lockConfigurationExtractor2 = new DefaultLockConfigurationExtractor(lockConfiguration2);
    LockManager lockManager2 = new DefaultLockManager(lockProvider, lockConfigurationExtractor2);
    LockableTaskScheduler lockableTaskScheduler2 = new LockableTaskScheduler(taskScheduler, lockManager2);

    tasks.put(name2, lockableTaskScheduler2.schedule(() -> {
        System.out.println("NAME IS : " + name2);
    }, new CronTrigger("30 * * * * *")));
}

configs

@Configuration
public class SchedulerConfiguration {
    @Bean
    public LockProvider lockProvider(DataSource dataSource) {

        return new JdbcTemplateLockProvider(JdbcTemplateLockProvider.Configuration.builder().withLockedByValue("1").withTableName("XXER.SHEDLOCK").withJdbcTemplate(new JdbcTemplate(dataSource)).build());
    }

    @Bean
    public LockProvider lockProvider2(DataSource dataSource) {
        return new JdbcTemplateLockProvider(JdbcTemplateLockProvider.Configuration.builder().withLockedByValue("2").withTableName("XXER.SHEDLOCK").withJdbcTemplate(new JdbcTemplate(dataSource)).build());
    }

    @Bean
    public ThreadPoolTaskScheduler taskScheduler(){
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setPoolSize(30);
        threadPoolTaskScheduler.setThreadNamePrefix("ThreadPoolTaskScheduler");
        return threadPoolTaskScheduler;
    }
}
question from:https://stackoverflow.com/questions/65853110/shedlock-lockabletaskscheduler-is-not-lock-my-tasks

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...