Java:SpringTaskサンプル
Last-modified: 2015-01-05 (月) 14:50:27 (3253d)
Top / Java:SpringTaskサンプル
Java:SpringTask?サンプル †
Springのタスクスケジューラを使うサンプルです。
- http://spring.io/guides/gs/scheduling-tasks/
- http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-scheduled
非常に簡単にタスクの定期実行ができます。
サンプルでは、以下の2種類のタスクを実行します。
- 5秒ごとに現在時刻を表示するタスク
- cron形式で実行時刻が指定され、現在時刻を表示するタスク
実行結果を確認すると、タスクの追い抜きがないことが確認できます。 ※つまり、fixedRateでもcronでも、前のタスクの実行が終わるまで、次のタスクは実行されないということです。
コード †
- appCtx_taskSchedular.xml(コンテキストXML)
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="sample2" /> <task:annotation-driven executor="myExecutor" scheduler="myScheduler"/> <task:executor id="myExecutor" pool-size="5"/> <task:scheduler id="myScheduler" pool-size="10"/> </beans>
- ソースコード
import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component @EnableScheduling public class TaskSchedulerSample { private static final SimpleDateFormat dateFormat = new SimpleDateFormat( "HH:mm:ss"); @Scheduled(fixedRate = 1000) public void reportCurrentTime() throws InterruptedException { System.out.println("start"); System.out.println("#FixedRate : The time is now " + dateFormat.format(new Date())); Thread.sleep(2000); System.out.println("end"); } @Scheduled(cron = "*/3 * * * * *") public void reportCronTime() throws InterruptedException { Thread.sleep(4000); System.out.println("#Cron : The time is now " + dateFormat.format(new Date())); } }
- テストコード
import static org.junit.Assert.*; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:appCtx_taskSchedular.xml" }) public class TaskSchedulerSampleTest { @Autowired private TaskSchedulerSample tss; @Test public void testReportCurrentTime() throws InterruptedException { //スケジューラーがタスクを実行することを待つ。 Thread.sleep(60000); } }