スケジューラの一例を以下に示します。
コード中にでてくる設定は以下の通りです。
- fixedDelay 前の実行が終わってからxミリ秒後に実行
- fixedRate 前の実行がスタートしてからXミリ秒後に実行
- cron Linuxにあるようなcron形式で指定。例:"*/5 * * * * MON-FRI"
- TestScheduler.java
package jp.hogehoge.scheduler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
/**
* スケジューラのお試しクラス
*
*/
@Service
public class TestScheduler {
@Scheduled(fixedRate = 2000) /*単位はミリ秒です*/
// @Scheduled(cron = "* * * * * *") /*cron形式でも書ける*/
public void greet() {
System.out.println("Hello World!");
}
}
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
/**
* スケジューラのお試しクラス
*
*/
@Service
public class TestScheduler {
@Scheduled(fixedRate = 2000) /*単位はミリ秒です*/
// @Scheduled(cron = "* * * * * *") /*cron形式でも書ける*/
public void greet() {
System.out.println("Hello World!");
}
}
- applicationContext.xml
<?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:util="http://www.springframework.org/schema/util"
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/util
http://www.springframework.org/schema/util/spring-util-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.1.xsd">
<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:util="http://www.springframework.org/schema/util"
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/util
http://www.springframework.org/schema/util/spring-util-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.1.xsd">
<!-- スケジューラ定義 -->
<task:annotation-driven scheduler="scheduler"/>
<task:scheduler id="scheduler" pool-size="1"/>
<context:component-scan base-package="jp.hogehoge"/>
</beans>
<task:annotation-driven scheduler="scheduler"/>
<task:scheduler id="scheduler" pool-size="1"/>
<context:component-scan base-package="jp.hogehoge"/>
</beans>
- TestScheduler.java
package jp.hogehoge.scheduler;
import org.springframework.stereotype.Service;
/**
* スケジューラのお試しクラス
*
*/
@Service
public class TestScheduler {
public void greet() {
System.out.println("Hello World!");
}
}
import org.springframework.stereotype.Service;
/**
* スケジューラのお試しクラス
*
*/
@Service
public class TestScheduler {
public void greet() {
System.out.println("Hello World!");
}
}
- applicationContext.xml
<?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:util="http://www.springframework.org/schema/util"
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/util
http://www.springframework.org/schema/util/spring-util-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.1.xsd">
<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:util="http://www.springframework.org/schema/util"
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/util
http://www.springframework.org/schema/util/spring-util-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.1.xsd">
<!-- スケジューラ定義 -->
<task:annotation-driven scheduler="scheduler"/>
<task:annotation-driven scheduler="scheduler"/>
<task:cheduled ref="testScheduler" method="geet" fixed-delay="5000">
<task:cheduled ref="hogeScheduler" method="run" fixed-delay="1000">
<task:cheduled ref="hogeScheduler" method="run" fixed-delay="1000">
<task:scheduler id="scheduler" pool-size="1"/>
<context:component-scan base-package="jp.hogehoge"/>
</beans>
</beans>
【参考】