- 追加された行はこの色です。
- 削除された行はこの色です。
*Java:SpringTaskサンプル [#u2e5b033]
Springのタスクスケジューラを使うサンプル
非常に簡単にタスクの定期実行ができます。
サンプルでは、以下の2種類のタスクを実行します。
-5秒ごとに現在時刻を表示するタスク
-cron形式で実行時刻が指定され、現在時刻を表示するタスク
***コード [#u509bce1]
-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);
}
}