Spring Frameworkでのスケジュールされたコード実行

ここでは、Spring Frameworkで書かれた小さなプロジェクションで定期的にコードを実行することが義務付けられていました。 クォーツの長いねじ込みとxmlの設定の書き込みのために、私はすでに古いメモリ(春2.x以降)から準備されていました。



Spring Frameworkのドキュメントはいつものように網羅的ですが、時には冗長な答えを与えます



実際、すべてが簡単です。



1.一度だけ実行-spring configに追加します(プロジェクトには最小限のものが存在します-まだアノテーションは追加されていません)。

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="

www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd

www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd

www.springframework.org/schema/tx www.springframework.org/schema/tx/spring-tx.xsd

www.springframework.org/schema/task www.springframework.org/schema/task/spring-task.xsd">









2. 2つの操作を行います-スケジューラーを同じ場所に構成します。



<task:annotation-driven executor="executor" scheduler="scheduler"/>

<task:executor id="executor" pool-size="5"/>

<task:scheduler id="scheduler" pool-size="10"/>









3. 3つ実行します。たとえば、Beanのメソッドに@Scheduled注釈を付けます。

@Scheduled(fixedDelay = 5000)

public void doSomething(){

// 5秒ごとに何かをする

}



それだけです! もう一度、私は春に対する私の愛を確信しました-技術は技術のためではなく、開発者の便宜のために開発されるとき



All Articles