HiiHahWIKI - making some notes for... -
Java:SpringTaskサンプル
をテンプレートにして作成
開始行:
*Java:SpringTaskサンプル [#u2e5b033]
Springのタスクスケジューラを使うサンプルです。
-http://spring.io/guides/gs/scheduling-tasks/
-http://docs.spring.io/spring/docs/current/spring-framewo...
非常に簡単にタスクの定期実行ができます。
サンプルでは、以下の2種類のタスクを実行します。
-5秒ごとに現在時刻を表示するタスク
-cron形式で実行時刻が指定され、現在時刻を表示するタスク
実行結果を確認すると、タスクの追い抜きがないことが確認で...
※つまり、fixedRateでもcronでも、前のタスクの実行が終わる...
***コード [#u509bce1]
-appCtx_taskSchedular.xml(コンテキストXML)
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-insta...
xmlns="http://www.springframework.org/schema/beans" xml...
xmlns:context="http://www.springframework.org/schema/co...
xmlns:jee="http://www.springframework.org/schema/jee" x...
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...
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-b...
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring...
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee...
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jd...
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-ta...
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.x...
<context:component-scan base-package="sample2" />
<task:annotation-driven executor="myExecutor" scheduler...
<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.EnableS...
import org.springframework.scheduling.annotation.Schedul...
import org.springframework.stereotype.Component;
@Component
@EnableScheduling
public class TaskSchedulerSample {
private static final SimpleDateFormat dateFormat = new ...
"HH:mm:ss");
@Scheduled(fixedRate = 1000)
public void reportCurrentTime() throws InterruptedExcep...
System.out.println("start");
System.out.println("#FixedRate : The time is now " + d...
Thread.sleep(2000);
System.out.println("end");
}
@Scheduled(cron = "*/3 * * * * *")
public void reportCronTime() throws InterruptedExceptio...
Thread.sleep(4000);
System.out.println("#Cron : The time is now " + d...
}
}
-テストコード
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Auto...
import org.springframework.test.context.ContextConfigura...
import org.springframework.test.context.junit4.SpringJUn...
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:appCtx_taskSchedular.xml" })
public class TaskSchedulerSampleTest {
@Autowired
private TaskSchedulerSample tss;
@Test
public void testReportCurrentTime() throws InterruptedE...
//スケジューラーがタスクを実行することを待つ。
Thread.sleep(60000);
}
}
終了行:
*Java:SpringTaskサンプル [#u2e5b033]
Springのタスクスケジューラを使うサンプルです。
-http://spring.io/guides/gs/scheduling-tasks/
-http://docs.spring.io/spring/docs/current/spring-framewo...
非常に簡単にタスクの定期実行ができます。
サンプルでは、以下の2種類のタスクを実行します。
-5秒ごとに現在時刻を表示するタスク
-cron形式で実行時刻が指定され、現在時刻を表示するタスク
実行結果を確認すると、タスクの追い抜きがないことが確認で...
※つまり、fixedRateでもcronでも、前のタスクの実行が終わる...
***コード [#u509bce1]
-appCtx_taskSchedular.xml(コンテキストXML)
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-insta...
xmlns="http://www.springframework.org/schema/beans" xml...
xmlns:context="http://www.springframework.org/schema/co...
xmlns:jee="http://www.springframework.org/schema/jee" x...
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...
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-b...
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring...
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee...
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jd...
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-ta...
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.x...
<context:component-scan base-package="sample2" />
<task:annotation-driven executor="myExecutor" scheduler...
<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.EnableS...
import org.springframework.scheduling.annotation.Schedul...
import org.springframework.stereotype.Component;
@Component
@EnableScheduling
public class TaskSchedulerSample {
private static final SimpleDateFormat dateFormat = new ...
"HH:mm:ss");
@Scheduled(fixedRate = 1000)
public void reportCurrentTime() throws InterruptedExcep...
System.out.println("start");
System.out.println("#FixedRate : The time is now " + d...
Thread.sleep(2000);
System.out.println("end");
}
@Scheduled(cron = "*/3 * * * * *")
public void reportCronTime() throws InterruptedExceptio...
Thread.sleep(4000);
System.out.println("#Cron : The time is now " + d...
}
}
-テストコード
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Auto...
import org.springframework.test.context.ContextConfigura...
import org.springframework.test.context.junit4.SpringJUn...
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:appCtx_taskSchedular.xml" })
public class TaskSchedulerSampleTest {
@Autowired
private TaskSchedulerSample tss;
@Test
public void testReportCurrentTime() throws InterruptedE...
//スケジューラーがタスクを実行することを待つ。
Thread.sleep(60000);
}
}
ページ名: