Spring MVC 3 Time scheduled task starting at a specific time - spring

I want to schedule a method call in Spring MVC to run after every two hours. This I can easily do via Spring 3.0 Time Scheduler. However, I want to kick the execution off only at a specific time of the day. That is, the method should only be invoked every 2 hours starting at a particular time.
For example - I want the method to run every 2 hours starting 6 AM.
The TimeScheduler interface has a scheduleAtFixedRate method which is overloaded to use startTime Date argument. I am not really sure how to use this.
Any idea how this can be achieved ?

You could take a look at the TaskScheduler interface. It provides a method scheduleAtFixedRate(Runnable task, Date startTime, long period) which returns a ScheduledFuture. You can use this with some simple Spring configuration:
<task:scheduler id="scheduler" pool-size="10"/>
This will create an instance of ThreadPoolTaskScheduler which implements TaskScheduler. Wire this bad boy into the class to call your specific method:
public class MyClass {
#Autowired
private TaskScheduler scheduler;
public void init() {
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
myMethod();
}
}, new Date(), 1000 * 60 * 60 * 2); //This will start now and run every two hours
}
public void myMethod() {
// the method you want to invoke
}
}

I would take a look at Spring's support for Task Execution and Scheduling. Specifically check out the #Scheduled annotation (with which you can specify a schedule based on a cron expression):
#Scheduled(cron="*/5 * * * * MON-FRI")
public void doSomething() {
// something that should execute on weekdays only
}

Related

how to call a method automatically in every seconds using spring boot?

In Spring-boot project, is there a method that want to make it called every seconds automatically?
And make an rest-api to set calling term in same project?
Here comes an example.
The greeting method will be executed every 5 seconds and it can be called when you visit /hello endpoint.
#SpringBootApplication
#EnableScheduling
#RestController
public class So47301079 {
public static void main(String[] args) {
SpringApplication.run(So47301079.class, args);
}
#Scheduled(fixedRate = 5000)
#GetMapping(value="/hello")
public void greeting() {
System.out.println("Hello!!!");
}
}
Hope this helps you!
Can use cron expression like #Scheduled(cron="*/5 * * * * *"). In this way, we have a control on minutes, hours, days also. Have a look at this video to know different possible ways to use cron expression.

Reset state before each Spring scheduled (#Scheduled) run

I have a Spring Boot Batch application that needs to run daily. It reads a daily file, does some processing on its data, and writes the processed data to a database. Along the way, the application holds some state such as the file to be read (stored in the FlatFileItemReader and JobParameters), the current date and time of the run, some file data for comparison between read items, etc.
One option for scheduling is to use Spring's #Scheduled such as:
#Scheduled(cron = "${schedule}")
public void runJob() throws Exception {
jobRunner.runJob(); //runs the batch job by calling jobLauncher.run(job, jobParameters);
}
The problem here is that the state is maintained between runs. So, I have to update the file to be read, the current date and time of the run, clear the cached file data, etc.
Another option is to run the application via a unix cron job. This will obviously meet the need to clear state between runs but I prefer to tie the job scheduling to the application instead of the OS (and prefer it to OS agnostic). Can the application state be reset between #Scheduled runs?
You could always move the code that performs your task (and more importantly, keeps your state) into a prototype-scoped bean. Then you can retrieve a fresh instance of that bean from the application context every time your scheduled method is run.
Example
I created a GitHub repository which contains a working example of what I'm talking about, but the gist of it is in these two classes:
ScheduledTask.java
Notice the #Scope annotation. It specifies that this component should not be a singleton. The randomNumber field represents the state that we want to reset with every invocation. "Reset" in this case means that a new random number is generated, just to show that it does change.
#Component
#Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
class ScheduledTask {
private double randomNumber = Math.random();
void execute() {
System.out.printf(
"Executing task from %s. Random number is %f%n",
this,
randomNumber
);
}
}
TaskScheduler.java
By autowiring in ApplicationContext, you can use it inside the scheduledTask method to retrieve a new instance of ScheduledTask.
#Component
public class TaskScheduler {
#Autowired
private ApplicationContext applicationContext;
#Scheduled(cron = "0/5 * * * * *")
public void scheduleTask() {
ScheduledTask task = applicationContext.getBean(ScheduledTask.class);
task.execute();
}
}
Output
When running the code, here's an example of what it looks like:
Executing task from com.thomaskasene.example.schedule.reset.ScheduledTask#329c8d3d. Random number is 0.007027
Executing task from com.thomaskasene.example.schedule.reset.ScheduledTask#3c5b751e. Random number is 0.145520
Executing task from com.thomaskasene.example.schedule.reset.ScheduledTask#3864e64d. Random number is 0.268644
Thomas' approach seems to be a reasonable solution, that's why I upvoted it. What is missing is how this can be applied in the case of a spring batch job. Therefore I adapted his example little bit:
#Component
public class JobCreatorComponent {
#Bean
#Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public Job createJob() {
// use the jobBuilderFactory to create your job as usual
return jobBuilderFactory.get() ...
}
}
your component with the launch method
#Component
public class ScheduledLauncher {
#Autowired
private ... jobRunner;
#Autwired
private JobCreatorComponent creator;
#Scheduled(cron = "${schedule}")
public void runJob() throws Exception {
// it would probably make sense to check the applicationContext and
// remove any existing job
creator.createJob(); // this should create a complete new instance of
// the Job
jobRunner.runJob(); //runs the batch job by calling jobLauncher.run(job, jobParameters);
}
I haven't tried out the code, but this is the approach I would try.
When constructing the job, it is important to ensure that all reader, processors and writers used in this job are complete new instances as well. This means, if they are not instantiated as pure java objects (not as spring beans) or as spring beans with scope "step" you must ensure that always a new instance is used.
Edited:
How to handle SingeltonBeans
Sometimes singleton beans cannot be prevented, in these cases there must be a way to "reset" them.
An simple approach would be to define an interface "ResetableBean" with a reset method that is implemented by such beans. Autowired can then be used to collect a list of all such beans.
#Component
public class ScheduledLauncher {
#Autowired
private List<ResetableBean> resetables;
...
#Scheduled(cron = "${schedule}")
public void runJob() throws Exception {
// reset all the singletons
resetables.forEach(bean -> bean.reset());
...

How to program the spring MVC cron without annotation

package com.test.cron;
#Service
public class CronJob {
protected static final Logger logger = Logger.getLogger(CronJob.class);
#Scheduled(cron="0 0 23 * * *")
public void demoServiceMethod()
{
logger.debug("Cron job started.");
}
}
Cron excution time will be often changed.
I have to exchange '#Scheduled' annotation to java code.
You can externalize this cron value into a properties file.
#Scheduled(cron="${schedularTime}")
and in your properties file ( example: application-dev.properties)
schedularTime=0 0/2 * * * ?
Old thread, but there is no real answer to the question.
Here is a way to schedule jobs without any annotations.
The example is in Kotlin, but works the same way in java.
#Component
class CliRunner(
private val scheduler: ThreadPoolTaskScheduler,
private val scheduledService: ScheduledService
) : CommandLineRunner {
override fun run(vararg args: String) {
scheduler.scheduleAtFixedRate(scheduledService::run, 10_000)
}
}
Inject ThreadPoolTaskScheduler and whatever you want to schedule, and call any of the scheduling methods on the scheduler.
My example schedules the run method to be executed every 10 seconds.
You could use a database for the configuration of schedule, Quartz had a capability to save its job meta data in a datasource.
Somebody had implemented something like this here.
https://github.com/davidkiss/spring-boot-quartz-demo

Using #Schedule in spring

Hello i working on a cron job and would like to schedule the task to run once every two weeks on a Monday morning using #schedule in spring can anyone help me out here?
You should check out the getting started examples in the spring.io website:
Scheduling Tasks
For your use case you'll be using the scheduled annotation with a cron expression:
#Scheduled(cron=". . .")
This uses the CronSequenceGenerator.
As the other answer mentioned you just have to add the #Scheduled annotation. however if using spring boot dont forget to add this anotation #EnableScheduling in the main class of your app
#EnableScheduling
#EnableAutoConfiguration
public class MyApplication {
Hope it helps
If you use not springboot but spring framework, you can configure scheduling job like below.
#EnableScheduling
#Configuration
public class SchedulingConfig {
// ...
}
And use like this.
private static final int TEN_MINUTES = 60 * 10 * 1000;
#Scheduled(fixedRate = TEN_MINUTES)
public void doSomething() {
// ...
}
Regarding the cron expression for every two weeks, here're some discussion.
It seems not very convenient to use cron expression to set two weeks schedule. Instead, you can use cron expression to set weekly tasks, and use a boolean variable to flip the boolean value:
boolean flag;
// run the method at 8:00 AM every Monday
#Scheduled(cron="0 0 8 * * MON")
public void schedulingTask() {
// run the actual task only if flag is true
if (flag) {
// handle the biweekly task
}
// flip the flag
flag = !flag;
}

Configuring multiple tasks with Spring scheduler having configurable triggers

I need some help with some design as well as spring scheduler related code. I am trying to write few utility classes, where by all the tasks (which are going to do some async processing) are scheduled at regular intervals.
So, I create a Task class which has a execute() method, which needs to be implemented by each task.
public interface Task
{
void execute();
}
public class TaskOne implements Task
{
#Override
public void execute()
{
// do something
}
}
public class TaskTwo implements Task
{
#Override
public void execute()
{
// do something
}
}
#Component
public class ScheduledTasks
{
#Scheduled(fixedRate = 5000)
public void runTask()
{
Task taskOne = new TaskOne();
taskOne.execute();
Task taskTwo = new TaskTwo();
taskTwo.execute();
}
}
I need to run different tasks at different interval, which I want to be configurable without the need of restarting the server. By this, I mean that time specified here can be changed without a restart.
#Scheduled(fixedRate = configurable, with some initial value)
Normally, what is the best way of doing this ?
One approach I can think of is:
1. Keep the trigger (periodic or cron) for each task in db
2. Load these triggers at the time of start up
3. Somehow, annotate the execute() methods with the proper value. This is going to be technically stiff.
4. If we want to change the job interval timing, update the db and refresh the caches.
Also, if some one can share some code snippet or suggest something like a small library, that would be highly appreciated. Thanks.
For configuration you don't need ScheduledTasks. Instead, you can annotate every task as #Component and annotate execute method with #Scheduled.
#Componenet
public class TaskOne implements Task
{
#Override
#Scheduled(...)
public void execute()
{
// do something
}
}
As far as updating this question. You'll have to figure out how to differentiate for different tasks.

Resources