タイミングタスクは一般に、中規模および大規模なエンタープライズレベルのプロジェクトに存在します。サーバーとデータベースへの圧力を減らすために、特定のビジネスロジックを完了するために期間を使用することがよくあります。最も一般的なことは、金融サービスシステムがコールバックをプッシュすることです。一般に、コールバックが成功せず、コンテンツを返す場合、支払いシステムの注文は引き続きコールバックを継続します。この種のコールバックは、通常、時限タスクによって完了します。レポートの生成もあります。顧客の数が小さすぎる場合、通常、この操作を完了しますが、これは多くの場合早朝です。現時点では、時限タスクを使用してロジックを完了することもできます。 Springbootにはタイミングタスクが組み込まれており、タイミングを使用できるようにするための注釈は1つだけです。
開発では、タイミングタスクは一般的な機能です。 Spring Bootの下でタイミングタスクを開発することは、実際には非常に簡単です。特定のコードは次のとおりです。
1.依存関係パッケージpom.xmlを構成します
デフォルトのMavenリポジトリにはアクセスできないため、Alibaba CloudのMavenリポジトリ画像がここで使用されます。
<?xml version = "1.0" encoding = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance <http://www.w3.org/2001 xsi:schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.0.sdsd"> <modeleversion> 4.0.0 </modelversion> <グループ< <バージョン> 0.0.1-snapshot </version> <packaging> jar </packaging> <name> spring-boot-scheduled </name> <説明> spring boot </description> <! - alibabaクラウドマベンリポジトリ - > <リポジトリ> <リポジトリ> <id>公開< <url> http://maven.aliyun.com/nexus/content/groups/public/ </url> <lileases> <Enabled> true </enabled> </relieses> </repository> </repositories> <pluginRepository> <pluginRepository> plugin </id>> pluginRepository> <url> http://maven.aliyun.com/nexus/content/groups/public/ </url> <lileases> <Enabled> true </enabled> </lileases> <snapshots> <Enabled> false </enabled> </snapshots> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-boot-starter-parent </artifactid> <bersion> 1.4.5.release </version> <relativepath/> <! - lookup parent from repository-> </parent> <properties> <project.build. <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> </properties> <dependencies> <shipencies> <dependency> <Dependency> GroupId> org.ProjectLombok </groupId> <artifactid> lombok </artifactid> <optional> true </optional> </dependency> <dependency> groupid> org.springframework.boot </groupid> <artifactid> spring-boot-tertest </artificid> </scope> </scope> scop </dependencies> <build> <blugins> <plugin> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-maven-plugin </artifactid> </plugin> </plugins> </build> </project>
2。タスクシナリオをカスタマイズします
タイミングされたタスクが実装され、固定サイクル、固定サイクル遅延間隔、策定された時点などのシナリオが提供されます。注釈には@scheduled Annotationを使用します。
Exampletimer.java
パッケージcom.exampamp; Import java.text.simpledateformat; Import java.util.date; import org.springframework.scheduling.annotation.scheduled; import org.springframework.stereotype.component; @componentpublic class exampletimer {シンプル化された形成明細書=シンプル化明細書をSimpleDateFormat( "HH:MM:SS"); @スケジュール(sixtrate = 10000)public void timerrate(){system.out.println(dateformat.format(new date());} {system.out.println( "init:"+ dateformat.format(new date()));} // @scheduled(cron = "50 16 20 *?")public void timercron(){system.out.println( "current time:"+ dateformat.format(new date());}}}3。アプリケーションを開始します
プログラムを開始するには、 @enablesChedulingアノテーションを追加する必要があります。
Springbootscheduledapplication.java
パッケージcom.example; import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.scheduling.annotation.enablescheduling; static void main(string [] args){springapplication.run(springbootscheduledapplication.class、args);}}}4。出力結果
20:16:27Init:20:16:28 Init:20:16:30init:20:16:32init:20:16:34init:20:16:3620:16:37init:20:16:38init:20:16:40init:20:16:42init:20:16:44init:20:16:16:48:48:48:48:16:48:48:48:48:48:48:48: 20:16:50Init:20:16:50Init:20:16:52Init:20:16:54
要約します
上記は、スプリングブートがスケジュールされたスケジュールされたタスクコードを実装することに関するこの記事のすべての内容です。すべての人に役立つことを願っています。興味のある友達は引き続きこのサイトを参照できます:
スプリングブートクロスドメイン設定インスタンスの詳細な説明
Spring Bootをすばやく知ることができます
春へのスプリングブートの利点に関する簡単な議論
欠点がある場合は、それを指摘するためにメッセージを残してください。このサイトへのご支援をありがとうございました!