好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring中@Scheduled功能的使用方法详解

前言

Spring 为任务调度和基于使用@Scheduled 注释的 cron 表达式的异步方法执行提供了极好的支持。可以将@Scheduled 注释与触发器元数据一起添加到方法中。在这篇文章中,我将以4种不同的方式展示@Scheduled 功能的使用方法。

一、Spring @Scheduled Annotation

@ scheduled注释用于任务调度。触发器信息需要与这个注释一起提供。

您可以使用属性 fixedDelay/fixedRate/cron 来提供触发信息。

fixedRate 使 Spring 定期运行任务,即使最后一次调用仍在运行 fixedDelay 特别控制最后一次执行结束时的下一次执行时间。 Cron 是一个源自 Unix cron 实用工具的特性,并且根据您的需求有各种选项。

示例用法如下:

?

1

2

3

4

5

6

7

8

9

@Scheduled Usages

@Scheduled (fixedDelay = 30000 )

public void demoServiceMethod () {... }

 

@Scheduled (fixedRate= 30000 )

public void demoServiceMethod () {... }

 

@Scheduled (cron= "0 0 * * * *" )

public void demoServiceMethod () {... }

1.2 如何启用@Scheduled 注释

要在 spring 应用程序中使用@Scheduled,必须首先在 applicationConfig.xml 文件中定义 xml 名称空间和模式位置定义。还添加任务: 注释驱动,以支持基于注释的任务调度。

?

1

2

3

4

5

6

applicationConfig.xml

xmlns:task="http://HdhCmsTestspringframework.org/schema/task"

http://HdhCmsTestspringframework.org/schema/task/

http://HdhCmsTestspringframework.org/schema/task/spring-task-3.0.xsd

 

< task:annotation-driven >

上面的添加是必要的,因为我们将使用基于注释的配置。

1.3 使用@Scheduled 注释

下一步是在类中创建一个类和一个方法,如下所示:

?

1

2

3

4

5

6

7

8

9

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());

   }

}

在上面的例子中

使用@Scheduled 注释反过来会使 Spring 容器理解这个注释下面的方法将作为作业运行。 记住,带@Scheduled 注释的方法不应该有传递给它们的参数。 它们也不应该返回任何值 如果希望在@Scheduled 方法中使用外部对象,应该使用自动连接将它们注入到 DemoService 类中,而不是将它们作为参数传递给@Scheduled 方法。

二、固定的延时和频率使用@Scheduled

在这个方法中,fixedDelay 属性与@Scheduled 注释一起使用。

举例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

DemoServiceBasicUsageFixedDelay.java

package com.howtodoinjava.service;

 

import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;

 

public class DemoServiceBasicUsageFixedDelay

{

  &nbsp;@Scheduled(fixedDelay = 5000)

  &nbsp;//@Scheduled(fixedRate = 5000)  //Or use this

  &nbsp;public void demoServiceMethod()

   {

  &nbsp; &nbsp;System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date());

   }

}

复制代码

应用程序配置如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

applicationContext.xml

< ? xml   version = "1.0" encoding = "UTF-8" ?>

< beans xmlns = "http://HdhCmsTestspringframework.org/schema/beans"

  xmlns:xsi = "http://HdhCmsTestw3.org/2001/XMLSchema-instance"

  xmlns:task = "http://HdhCmsTestspringframework.org/schema/task"

  xmlns:util = "http://HdhCmsTestspringframework.org/schema/util"

  xmlns:context = "http://HdhCmsTestspringframework.org/schema/context"

  xsi:schemaLocation="http://HdhCmsTestspringframework.org/schema/beans http://HdhCmsTestspringframework.org/schema/beans/spring-beans-3.0.xsd

     http://HdhCmsTestspringframework.org/schema/context/ http://HdhCmsTestspringframework.org/schema/context/spring-context.xsd

     http://HdhCmsTestspringframework.org/schema/util/ http://HdhCmsTestspringframework.org/schema/util/spring-util.xsd

         http://HdhCmsTestspringframework.org/schema/task/ http://HdhCmsTestspringframework.org/schema/task/spring-task-3.0.xsd">

     < task:annotation-driven />

     < bean id = "demoServiceBasicUsageFixedDelay" class = "com.howtodoinjava.service.DemoServiceBasicUsageFixedDelay" ></ bean >

</ beans >

三、配合cron表达式使用@Scheduled

在此方法中,cron 属性与@Scheduled 注释一起使用。

举例:

?

1

2

3

4

5

6

7

8

9

10

11

12

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());

   }

}

应用程序配置如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

applicationContext.xml

< ? xml   version = "1.0" encoding = "UTF-8" ?>

< beans xmlns = "http://HdhCmsTestspringframework.org/schema/beans"

  xmlns:xsi = "http://HdhCmsTestw3.org/2001/XMLSchema-instance"

  xmlns:task = "http://HdhCmsTestspringframework.org/schema/task"

  xmlns:util = "http://HdhCmsTestspringframework.org/schema/util"

  xmlns:context = "http://HdhCmsTestspringframework.org/schema/context"

  xsi:schemaLocation="http://HdhCmsTestspringframework.org/schema/beans http://HdhCmsTestspringframework.org/schema/beans/spring-beans-3.0.xsd

     http://HdhCmsTestspringframework.org/schema/context/ http://HdhCmsTestspringframework.org/schema/context/spring-context.xsd

     http://HdhCmsTestspringframework.org/schema/util/ http://HdhCmsTestspringframework.org/schema/util/spring-util.xsd

         http://HdhCmsTestspringframework.org/schema/task/ http://HdhCmsTestspringframework.org/schema/task/spring-task-3.0.xsd">

     < task:annotation-driven />

     < bean id = "demoServiceBasicUsageCron" class = "com.howtodoinjava.service.DemoServiceBasicUsageCron" ></ bean >

</ beans >

四、使用properties文件配置Cron

在这个方法中,cron 属性与@Scheduled 注释一起使用。此属性的值必须是 cron 表达式,如前面的方法所示,但是,此 cron 表达式将在属性文件中定义,相关属性的键将用于@Scheduled 注释。

这将使 cron 表达式与源代码分离,从而使更改变得容易。

?

1

2

3

4

5

6

7

8

9

10

11

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());

   }

}

应用程序配置如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

applicationContext.xml

<? xml   version = "1.0" encoding = "UTF-8" ?>

< beans xmlns = "http://HdhCmsTestspringframework.org/schema/beans"

  xmlns:xsi = "http://HdhCmsTestw3.org/2001/XMLSchema-instance"

  xmlns:task = "http://HdhCmsTestspringframework.org/schema/task"

  xmlns:util = "http://HdhCmsTestspringframework.org/schema/util"

  xmlns:context = "http://HdhCmsTestspringframework.org/schema/context"

  xsi:schemaLocation="http://HdhCmsTestspringframework.org/schema/beans http://HdhCmsTestspringframework.org/schema/beans/spring-beans-3.0.xsd

     http://HdhCmsTestspringframework.org/schema/context/ http://HdhCmsTestspringframework.org/schema/context/spring-context.xsd

     http://HdhCmsTestspringframework.org/schema/util/ http://HdhCmsTestspringframework.org/schema/util/spring-util.xsd

         http://HdhCmsTestspringframework.org/schema/task/ http://HdhCmsTestspringframework.org/schema/task/spring-task-3.0.xsd">

     < task:annotation-driven />

     < util:properties id = "applicationProps" location = "application.properties" />

   < context:property-placeholder properties-ref = "applicationProps" />

     < bean id = "demoServicePropertiesExample" class = "com.howtodoinjava.service.DemoServicePropertiesExample" ></ bean >

</ beans >

五、使用context配置Cron

该方法在属性文件中配置 cron 表达式,在配置文件中使用 cron 表达式的属性键配置作业调度。主要的变化是您不需要在任何方法上使用@Scheduled 注释。方法配置也是在应用程序配置文件中完成的。

举例:

?

1

2

3

4

5

6

7

8

9

10

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());

   }

}

应用程序配置如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

applicationContext.xml

<? xml   version = "1.0" encoding = "UTF-8" ?>

< beans xmlns = "http://HdhCmsTestspringframework.org/schema/beans"

  xmlns:xsi = "http://HdhCmsTestw3.org/2001/XMLSchema-instance"

  xmlns:task = "http://HdhCmsTestspringframework.org/schema/task"

  xmlns:util = "http://HdhCmsTestspringframework.org/schema/util"

  xmlns:context = "http://HdhCmsTestspringframework.org/schema/context"

  xsi:schemaLocation="http://HdhCmsTestspringframework.org/schema/beans http://HdhCmsTestspringframework.org/schema/beans/spring-beans-3.0.xsd

     http://HdhCmsTestspringframework.org/schema/context/ http://HdhCmsTestspringframework.org/schema/context/spring-context.xsd

     http://HdhCmsTestspringframework.org/schema/util/ http://HdhCmsTestspringframework.org/schema/util/spring-util.xsd

         http://HdhCmsTestspringframework.org/schema/task/ http://HdhCmsTestspringframework.org/schema/task/spring-task-3.0.xsd">

     < task:annotation-driven />

     < 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 >

总结

到此这篇关于Spring中@Scheduled功能使用的文章就介绍到这了,更多相关Spring @Scheduled使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://juejin.cn/post/7082538457251184647

查看更多关于Spring中@Scheduled功能的使用方法详解的详细内容...

  阅读:16次