ホームページ  >  記事  >  Java  >  春のスケジュールタスク @Scheduled の詳細説明

春のスケジュールタスク @Scheduled の詳細説明

高洛峰
高洛峰オリジナル
2017-02-07 15:39:052654ブラウズ

1. 設定ファイル

<?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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"
 default-autowire="byName" default-lazy-init="false">
 
 <!-- 定时任务相关配置 -->
 <task:executor id="executor" pool-size="10" queue-capacity="128"/>
 <task:scheduler id="scheduler" pool-size="10"/>
 <task:annotation-driven executor="executor" scheduler="scheduler" proxy-target-class="true"/>
 
</beans>

2. 呼び出す方法は2つあります

1) 固定レートで実行する必要がある場合は、アノテーションで指定した属性名をfixedRateに変更するだけです。 be 実行の呼び出しには 5 秒の固定レートが使用されます。このサイクルは、前のタスクの開始時間に基づいて行われます。

@Scheduled(fixedDelay = 5000)
 public void testTask() {
  logger.info("测试定时任务");
 }

2) cron を使用します。式を使用して、次のようなスケジュールされた呼び出しを実装できます: 早朝の呼び出し、詳細な cron 関連パラメータは後で紹介します

@Scheduled(cron = "cron = "0 0 2 * * ?"")
 public void testTaskWithDate() {
  logger.info("测试2016.定时任务");
 }

3. cron 関連パラメータの意味

cron 式には、少なくとも 6 (おそらく 7) 個の時間要素が区切られていますスペースによって。

順に、

秒 (0~59)

分 (0~59)

時間 (0~23)

日 (月) (0~31) ですが、その数を考慮する必要があります。あなたの月の日数)

月(0~11)

日(週) (1~7 1=SUN or SUN、MON、TUE、WED、THU、FRI、SAT)

Year (1970-2099) - @Scheduled ははいではありません。Spring Quartz は

をサポートします。各要素は値 (6 など)、連続間隔 (9-12)、間隔 (8-18/4) になります (/ は 4 時間ごとを意味します) , リスト (1,3,5)、ワイルドカード。 「月の日付」と「週の日付」の 2 つの要素は相互に排他的であるため、どちらかを設定する必要があります?.

0 0 10,14,16 * * ? 毎日午前 10 時と午後 2 時、4 時'時計

0 0/30 9-17 * * ? 9 時から 5 時までの 30 分ごと

0 0 12 ? * WED は毎週水曜日の正午を意味します

"0 0 12 * * ?"毎日正午

「0 15 10 ? * *」 毎日午前 10:15 にトリガー

「0 15 10 * * ?」 毎日午前 10:15 にトリガー

「0 15 10 * * ? *」毎朝トリガー 10:15にトリガー

「0 15 10 * * ? 2005」 2005年の毎日午前10時15分にトリガー

「0 * 14 * * ?」 毎日午後2時から午後2時59分まで1分ごとにトリガー日

「0 0/5 14 * * ? 毎日午後 2 時から午後 2 時 55 分まで 5 分ごとにトリガーします

」「午後 2 時から午後 2 時まで 5 分ごとにトリガーします」毎日午後 55 時 55 時と午後 6 時から午後 6 時 55 分まで 5 分ごとにトリガー

「0 0-5 14 * * ?」 毎日午後 2 時から午後 2 時 05 分まで 1 分ごとにトリガー

「0 10、44 14」 ? 3 WED」 3 月の毎週水曜日、午後 2 時 10 分と午後 2 時 44 分にトリガーします

「0 15 10 ? * MON-FRI」 月曜日から金曜日の午前 10 時 15 分にトリガーします

「0 15 10 15 * ?」毎月 15 日の午前 10 時 15 分にトリガー

「0 15 10 L * ?」 毎月末日の午前 10 時 15 分にトリガー

「0 15 10 ? * 6L」 の朝にトリガー各月の最後の金曜日 10:15 にトリガーされます

「0 15 10? * 6L 2002-2005」 2002 年から 2005 年の毎月の最後の金曜日の午前 10 時 15 分にトリガーされます

「0 15 10? * 6」 #3" 毎月の 1 日にトリガーされます 3 つの金曜日の午前 10 時 15 分にトリガーされます

一部の部分式には範囲またはリストを含めることができます

例: 部分式 (日 (週)) は「MON-FRI」、 "MON, WED, FRI", " MON-WED,SAT"

"*" 文字はすべての可能な値を表します

したがって、部分式 (month) 内の "*" は各月の意味を表し、"部分式 (day (week) )) の *" は各曜日を表します

値の増分を指定するには "/" 文字が使用されます

例:部分式(分)は、0分から15分ごとを意味します

部分式(分)の「3/20」は、3分から20分ごとを意味します(「3、23、 43”)


”? " 文字は、日 (月) と日 (週) の 2 つの部分式でのみ使用され、値が指定されていないことを示します

2 つの部分式のいずれかに値が指定されている場合、競合を避けるために、 、他の部分式は次のようにする必要があります。式の値は「?」に設定されます。 「

「L」文字は、day(月)とday(週)の2つの部分表現でのみ使用されます。これは単語「last」の略語です

が、2つの部分表現での意味は、表現が異なります

日(月)の部分表現では、「L」は月の最終日を表します

日(週)の自己表現では、「L」は週の最後の日を表しますis SAT

If 「L」の前に特定の内容がある場合は、他の意味になります

例:「6L」は今月の6日から末日を意味し、「FRIL」は今月の最終金曜日を意味します

注: " L" パラメータを使用する場合、問題が発生するため、リストまたは範囲を指定しないでください

spring 定时任务@Scheduled详解

4. 制限事項 - @Scheduled の cron は実行年を指定できません

つまり、次のスケジュールされたメソッドを使用すると、タスク

@Scheduled(cron = "0 18 10 * * ? 2016-2016")
  public void testTaskWithDate() {
    logger.info("测试2016.定时任务");
  }

次のエラーが報告されます

Cron expression must consist of 6 fields (found 7 in "0 18 10 * * ? 2016-2016")

org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;giftReceiveRecordServiceImp&#39; defined in URL [jar:file:/Users/zhengcanrui/WORK/git/seewoedu-train-server/train-web/target/train/WEB-INF/lib/train-server-2.0-SNAPSHOT.jar!/com/seewoedu/train/service/impl/GiftReceiveRecordServiceImp.class]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Encountered invalid @Scheduled method &#39;testTaskWithDate&#39;: Cron expression must consist of 6 fields (found 7 in "0 18 10 * * ? 2016-2016")
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
 at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
 at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
 at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4842)
 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303)
 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
 at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
 at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
 at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1696)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:606)
 at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
 at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
 at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
 at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:484)
 at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:433)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:606)
 at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
 at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
 at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
 at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1487)
 at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:97)
 at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1328)
 at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1420)
 at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:606)
 at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
 at sun.rmi.transport.Transport$2.run(Transport.java:202)
 at sun.rmi.transport.Transport$2.run(Transport.java:199)
 at java.security.AccessController.doPrivileged(Native Method)
 at sun.rmi.transport.Transport.serviceCall(Transport.java:198)
 at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:567)
 at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828)
 at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.access$400(TCPTransport.java:619)
 at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:684)
 at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:681)
 at java.security.AccessController.doPrivileged(Native Method)
 at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:681)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
 at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method &#39;testTaskWithDate&#39;: Cron expression must consist of 6 fields (found 7 in "0 18 10 * * ? 2016-2016")
 at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:405)
 at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:258)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
 ... 58 more

错误原因:

/**
 * Parse the given pattern expression.
 */
private void parse(String expression) throws IllegalArgumentException {
 String[] fields = StringUtils.tokenizeToStringArray(expression, " ");
 if (fields.length != 6) {
  throw new IllegalArgumentException(String.format(""
    + "cron expression must consist of 6 fields (found %d in %s)", fields.length, expression));
 }

spring taks 不支持年位定时,它毕竟不是quartz,只是简单的定时框架,比起jdk Timer就加入了线程池而以.

但是制定到年份,会存在一个问题,就是在你在过了这个时间后再启动项目的时候,他会一直报一个memory leak的错误,大概的意思是你的定时任务将永远不会被执行,导致项目一直启动不了。

源码里的注释:

 *The pattern is a list of six single space-separated fields: representing
 * second, minute, hour, day, month, weekday. Month and weekday names can be
 * given as the first three letters of the English names.

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

更多spring 定时任务@Scheduled详解相关文章请关注PHP中文网!

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。