Spring taskのタイムミッションツールを持参して使用します.
20945 ワード
プロジェクトのニーズに応じて、1日に1回のタイムミッションが必要です.Spring 3.0以降はtaskスケジュールツールを持参しています.Quartzよりも簡単で便利です.軽量級のQuartzに相当します.springコアパッケージ以外に追加のjarパッケージを導入する必要がなく、注釈と配置ファイルの2つの形式をサポートしています.ここで紹介します.
1.プロファイル方式
jobクラスを作成する
2.注釈の方式
jobクラスを作成するには、運転方法に注釈@Schduledを追加し、式を設定する必要があります.
1.プロファイル方式
jobクラスを作成する
package cn.com.job.order;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.stereotype.Component;
@Component("orderFileJob")
public class OrderFileJob {
private static final long serialVersionUID = -7194794905986149036L;
public void executeJob() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
System.out.println("executeJob........." + sdf.format(new Date()));
}
}
springプロファイル設定<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-lazy-init="false">
<context:component-scan base-package="cn.com.job.order" />
<task:scheduled-tasks>
<task:scheduled ref="orderFileJob" method="executeJob" cron="0/10 * * * * ?"/> <!-- , 10 -->
</task:scheduled-tasks>
</beans>
beansタグ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「spring注釈スキャンを設定する.taskタグのrefは指定されたjobクラスを設定し、methodは実行する方法を設定し、cronはタイミング周期を設定する.2.注釈の方式
jobクラスを作成するには、運転方法に注釈@Schduledを追加し、式を設定する必要があります.
package cn.com.job.order;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component("orderFileJob")
public class OrderFileJob {
private static final long serialVersionUID = -7194794905986149036L;
@Scheduled(cron="0/10 * * * * ?")
public void executeJob() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
System.out.println("executeJob........." + sdf.format(new Date()));
}
}
springプロファイル設定<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-lazy-init="false">
<context:component-scan base-package="cn.com.job.order" />
<task:annotation-driven/>
</beans>
最初の方式に比べて、注釈方式を採用して、構成ファイルに追加すればいいです.