Make writing a habit together! This is the fourth day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

Spring provides excellent support for task scheduling and asynchronous method execution based on CRon expressions using @scheduled annotations. The @Scheduled annotation can be added to a method along with trigger metadata. In this article, I’ll show you how to use the @Scheduled feature in four different ways.

Spring @Scheduled Annotation

The @scheduled annotation is used for scheduling tasks. Trigger information needs to be supplied with this annotation.

You can use attributes fixedDelay/fixedRate/cron to trigger the information.

  • FixedRate causes Spring to run tasks periodically, even if the last call is still running
  • FixedDelay specifically controls the next execution time at the end of the last execution.
  • Cron is a feature derived from the Unix Cron utility and has various options depending on your needs.

Example usage is as follows:

@Scheduled Usages
@Scheduled(fixedDelay =30000)
public void demoServiceMethod () {... }
 
@Scheduled(fixedRate=30000)
public void demoServiceMethod () {... }
 
@Scheduled(cron="0 0 * * * *")
public void demoServiceMethod () {... }
Copy the code

1.2 How Do I Enable the @Scheduled Comment

To use @Scheduled in a Spring application, you must first define the XML namespace and schema location definitions in the applicationConfig.xml file. Task: annotation driver is also added to support annotation based task scheduling.

applicationConfig.xml
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task/
http://www.springframework.org/schema/task/spring-task-3.0.xsd
 
<task:annotation-driven>
Copy the code

The above addition is necessary because we will be using annotation-based configuration.

1.3 Use @scheduled annotations

The next step is to create a class and a method in the class as follows:

DemoService.java
public class DemoService
{
  @Scheduled(cron="*/5 * * * * ?")
  public void demoServiceMethod()
  {
    System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date());
  }
}
Copy the code

In the example above

  • Using the @scheduled annotation in turn causes the Spring container to understand that the methods below the annotation will be run as jobs.
  • Remember that @scheduled annotated methods should not have arguments passed to them.
  • They should also not return any value
  • If you want to use external objects in the @Scheduled method, you should inject them into the DemoService class using automatic wiring rather than passing them as arguments to the @Scheduled method.

Fixed delays and frequencies using @scheduled

In this method, the fixedDelay attribute is used with the @Scheduled annotation.

For example:

DemoServiceBasicUsageFixedDelay.java package com.howtodoinjava.service; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; public class DemoServiceBasicUsageFixedDelay { @Scheduled(fixedDelay = 5000) //@Scheduled(fixedRate = 5000) //Or use this public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); }}Copy the code

The application configuration is as follows:

applicationContext.xml < ? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/task/ http://www.springframework.org/schema/task/spring-task-3.0.xsd "> < - driven/task: an annotation > < bean id="demoServiceBasicUsageFixedDelay" class="com.howtodoinjava.service.DemoServiceBasicUsageFixedDelay"></bean> </beans>Copy the code

Use @scheduled with crON expressions

In this method, the cron attribute is used with the @scheduled annotation.

For example:

DemoServiceBasicUsageCron.java
package com.howtodoinjava.service;
 
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
 
public class DemoServiceBasicUsageCron
{
  @Scheduled(cron="*/5 * * * * ?")
  public void demoServiceMethod()
  {
    System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date());
  }
}
Copy the code

The application configuration is as follows:

applicationContext.xml < ? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/task/ http://www.springframework.org/schema/task/spring-task-3.0.xsd "> < - driven/task: an annotation > < bean id="demoServiceBasicUsageCron" class="com.howtodoinjava.service.DemoServiceBasicUsageCron"></bean> </beans>Copy the code

Configure Cron using the properties file

In this method, the cron attribute is used with the @scheduled annotation. The value of this property must be a Cron expression, as shown in the previous method, but the CRon expression will be defined in the properties file, and the key of the related property will be used for the @Scheduled annotation.

This separates the CRon expression from the source code, making it easy to change.

DemoServicePropertiesExample.java package com.howtodoinjava.service; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; public class DemoServicePropertiesExample { @Scheduled(cron = "${cron.expression}") public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); }}Copy the code

The application configuration is as follows:

applicationContext.xml <? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/task/ http://www.springframework.org/schema/task/spring-task-3.0.xsd "> < - driven/task: an annotation > < util: properties id="applicationProps" location="application.properties" /> <context:property-placeholder properties-ref="applicationProps" /> <bean id="demoServicePropertiesExample" class="com.howtodoinjava.service.DemoServicePropertiesExample"></bean> </beans>Copy the code

5. Configure Cron using context

This method configures the CRon expression in a properties file, where the cron expression’s property key is used to configure job scheduling. The main change is that you don’t need to use @Scheduled annotations on any of the methods. Method configuration is also done in the application configuration file.

For example:

DemoServiceXmlConfig.java package com.howtodoinjava.service; import java.util.Date; public class DemoServiceXmlConfig { public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); }}Copy the code

The application configuration is as follows:

applicationContext.xml <? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/task/ http://www.springframework.org/schema/task/spring-task-3.0.xsd "> < - driven/task: an annotation > < util: properties id="applicationProps" location="application.properties" /> <context:property-placeholder properties-ref="applicationProps" /> <bean id="demoServiceXmlConfig" class="com.howtodoinjava.service.DemoServiceXmlConfig" /> <task:scheduled-tasks> <task:scheduled ref="demoServiceXmlConfig" method="demoServiceMethod" cron="#{applicationProps['cron.expression']}"></task:scheduled> </task:scheduled-tasks> </beans>Copy the code