Is there a way to call a spring scheduled method (job) through a user interaction? I need to create a table with shown all jobs and the user should be able to execute them manually. For sure the jobs run automatically but the user should be able to start them manually.
@Configuration
@EnableScheduling
public class ScheduleConfiguration {
@Bean
public ScheduledLockConfiguration taskScheduler(LockProvider
lockProvider) {
return ScheduledLockConfigurationBuilder
.withLockProvider(lockProvider)
.withPoolSize(15)
.withDefaultLockAtMostFor(Duration.ofHours(3))
.build();
}
@Bean
public LockProvider lockProvider(DataSource dataSource) {
return new JdbcTemplateLockProvider(dataSource);
}
}
@Component
public class MyService {
@Scheduled(fixedRateString = "1000")
@SchedulerLock(name = "MyService.process", lockAtLeastFor = 30000)
@Transactional
public void process() {
// do something
}
}
Regardless of the Windows version or edition you have, you can also use the Run window to launch the Task Scheduler. Press the Windows + R keys on your keyboard to open Run, and then type taskschd. msc in the Open field. Finally, click or tap on OK, or press Enter on your keyboard.
Implementation: It is depicted below stepwise as follows: Step 1: Creating a spring boot application using Spring Initializer for which one can refer to the basics of creating a Spring class. Step 2: Specifying @EnableScheduling annotation in the Spring Boot application class.
The Spring Framework provides abstractions for asynchronous execution and scheduling of tasks with the TaskExecutor and TaskScheduler interfaces, respectively. Spring also features implementations of those interfaces that support thread pools or delegation to CommonJ within an application server environment.
Here's an example using a TaskScheduler
:
@Component
public class SomeTask implements Runnable {
private static final Logger log = LoggerFactory.getLogger();
@Autowired
public SomeDAO someDao;
@Override
public void run() {
// do stuff
}
}
TaskScheduler
bean:@Configuration
public class TaskSchedulerConfig {
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(5);
threadPoolTaskScheduler.setThreadNamePrefix("ThreadPoolTaskScheduler");
return threadPoolTaskScheduler;
}
}
@Component
public class ScheduledTasks {
@Autowired private TaskScheduler taskScheduler;
// Tasks
@Autowired private SomeTask task1; // autowired in case the task has own autowired dependencies
@Autowired private AnotherTask task2;
@PostConstruct
public void scheduleTasks() {
taskScheduler.schedule(task1, new PeriodicTrigger(20, TimeUnit.SECONDS));
taskScheduler.schedule(task2, new CronTrigger("0 0 4 1/1 * ? *"));
}
}
@Controller
public class TaskController {
@Autowired private TaskScheduler taskScheduler;
// Tasks
@Autowired private SomeTask task1;
@RequestMapping(value = "/executeTask")
public void executeTask() {
taskScheduler.schedule(task1, new Date()); // schedule task for current time
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With