Quartz

下記サイトのチュートリアルあたりが参考になるはず。

小技など

コンテキスト設定ファイルで設定してみる

まず struts.xml に struts.objectFactory を spring で設定するように書いておく。

struts.xml(一部)

Everything is expanded.Everything is shortened.
  1
  2
  3
 
 
 
    <constant name="struts.objectFactory" value="spring" />
    <constant name="struts.objectFactory.spring.autoWire" value="name" />

ここは名前一致で。型一致(value="type")でいけるかどうかは未確認。

quartz.properties を書く。 Quartz のセットの中にサンプルがあるので、必要な部分のコメントをはずして設定する。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
#============================================================================
# Configure Main Scheduler Properties  
#============================================================================
 
#org.quartz.scheduler.instanceName = TestScheduler
#org.quartz.scheduler.instanceId = AUTO
 
#============================================================================
# Configure ThreadPool  
#============================================================================
 
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 3
org.quartz.threadPool.threadPriority = 5
 
#============================================================================
# Configure JobStore  
#============================================================================
 
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
 
##データソースは server.xml で設定する場合は必要ないはず。
#org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
#org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
#org.quartz.jobStore.useProperties = false
#org.quartz.jobStore.dataSource = myDS
#org.quartz.jobStore.tablePrefix = QRTZ_
#org.quartz.jobStore.isClustered = false
 
#============================================================================
# Configure Datasources  
#============================================================================
 
#org.quartz.dataSource.myDS.driver = org.postgresql.Driver
#org.quartz.dataSource.myDS.URL = jdbc:postgresql://localhost/dev
#org.quartz.dataSource.myDS.user = jhouse
#org.quartz.dataSource.myDS.password = 
#org.quartz.dataSource.myDS.maxConnections = 5
 
 
 
#============================================================================
# Configure Plugins 
#============================================================================
 
#org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
 
#org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
#org.quartz.plugin.jobInitializer.fileNames = jobs.xml
#org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
#org.quartz.plugin.jobInitializer.failOnFileNotFound = true
#org.quartz.plugin.jobInitializer.scanInterval = 10
#org.quartz.plugin.jobInitializer.wrapInUserTransaction = false

プロジェクト直下に .springBeans というファイルを配置。

以下のように書く。

.springBeans

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 
 
 
 
 
 
 
 
 
 
 
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
    <configExtensions>
        <configExtension>xml</configExtension>
    </configExtensions>
    <configs>
        <config>conf/spring/applicationContext.xml</config>
    </configs>
    <configSets>
    </configSets>
</beansProjectDescription>

このコンテキスト設定ファイルに Quartz の設定を書く。

applicationContext.xml

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
|
|
|
!
 
 
-
|
|
|
|
|
|
|
|
!
 
 
-
|
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
|
|
|
|
|
|
!
 
 
 
 
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    default-autowire="autodetect" 
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop.xsd 
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-2.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
       http://www.springframework.org/schema/jee 
       http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
 
    <!-- Web アプリの設定諸々 ここから -->
    <!-- Web アプリの設定諸々 ここまで -->
 
    <!-- Quartz の設定 ここから -->
 
    <!-- 処理を実行するクラス -->
    <bean id="sampleJob" class="com.foo.job.SampleJob" scope="prototype" >
        <!-- このクラスで使用するオブジェクトたち -->
        <property name="object1" ref="object1"/>
        <property name="object2" ref="object2"/>
    </bean>
 
    <!-- Quartz で呼び出されるクラス -->
    <bean id="sampleJobDetail"
        class="org.springframework.scheduling.quartz.JobDetailBean" scope="prototype">
        <property name="jobClass" value="com.foo.job.sampleJob" />
            <property name="jobDataAsMap">
            <map>
                <!-- このクラスに渡すオブジェクトたち -->
                <entry key="argumentObject1"><ref bean="argumentObject1" /></entry>
                <entry key="argumentObject2"><ref bean="argumentObject2" /></entry>
            </map>
        </property>
        <!-- リスナ -->
        <property name="jobListenerNames">
            <list>
                <value>sampleJobListener</value>
            </list>
        </property>
    </bean>
 
    <!-- トリガの設定 -->
    <bean id="sampleJobTrigger"
        class="org.springframework.scheduling.quartz.CronTriggerBean" scope="prototype">
        <!-- Quartz で呼び出されるクラス -->
        <property name="jobDetail" ref="sampleJobDetail" />
        <!-- 例えば、毎日AM1:00に実行の場合 -->
        <property name="cronExpression" value="0 0 1 * * ?" />
    </bean>
 
    <!-- リスナ -->
    <!-- 複数処理がある場合に起動順序をここで設定する。
	<bean id="sampleJobListener" class="com.foo.job.SampleJobListener">
		<property name="jobChainLinkList">
	  	 	<list>
	  	 		<ref bean="sampleJobDetail" />
	  	 	</list>
		</property>
	</bean>
	-->
 
    <!-- 排他制御の設定 -->
    <!-- 重複起動を抑制する bean の id を指定する。
	<bean id="triggerListener" class="com.foo.job.TriggerListener" >
		<property name="listenerName" value="exclusiveTriggerListener" />
		<property name="controlJobName">
			<set>
			 	<value>sampleJobDetail</value>
			</set>
		</property>
	</bean>
	-->
 
    <!-- ファクトリの定義 -->
    <!--
		autowire の設定は struts.xml と揃える。
	-->
    <bean id="schedulerFactory"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean" autowire="byName">
        <property name="triggers">
            <list>
                <!-- 起動対象のトリガを書く -->
                <ref bean="sampleJobTrigger" />
            </list>
        </property>
        <property name="configLocation">
            <value>classpath:/conf/quartz/quartz.properties</value>
        </property>
        <property name="jobDetails">
            <list>
                <ref bean="sampleJobDetail" />
            </list>
        </property>
        <property name="dataSource">
            <!-- RAMJobStore を使用するので null -->
            <null />
        </property>
        <property name="autoStartup">
            <value>false</value>
        </property>
        <!--
		<property name="globalTriggerListeners">
			<list>
				<ref bean="triggerListener" />
			</list>
		</property>
		<property name="jobListeners">
			<list>
				<ref bean="sampleJobListener" />
			</list>
		</property>
		-->
 
    </bean>
 
</beans>

このファイルは /conf/spring/ に配置。

ジョブクラスは、例えばこんな感じで。

com.foo.job.SampleJob?

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 
 
 
 
 
 
 
 
 
 
 
 
-
|
-
|
!
|
-
|
-
!
|
|
-
!
|
!
|
!
package com.foo.job;
 
import java.sql.Timestamp;
import java.util.Map;
import java.util.Set;
 
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerContext;
 
import org.springframework.scheduling.quartz.QuartzJobBean;
 
public class SampleJob extends QuartzJobBean {
 
    /**
	 *	ジョブの起動。
	 */
    public void executeInternal(JobExecutionContext jobExecutionContext) 
        throws JobExecutionException {
 
        // スケジューラのコンテキストを取得する。
        SchedulerContext sc = jobExecutionContext.getScheduler().getContext();
 
 
        // ここで定期的に実行したい処理を書く。
 
 
    }
 
}

で、サーブレットから起動。

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
-
|
-
!
|
|
|
|
-
!
|
-
!
|
-
!
|
|
|
-
!
|
|
-
!
|
!
|
!
package com.foo.servlet;
 
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
 
import org.quartz.Scheduler;
import org.quartz.SchedulerContext;
import org.quartz.SchedulerException;
 
import org.springframework.web.context.WebApplicationContext;
 
 
public class SystemInitServlet implements Servlet {
 
    public void init(ServletConfig config) throws ServletException {
 
        // Web アプリケーションのコンテキストを取得する。
        WebApplicationContext wac = 
                (WebApplicationContext) config.getServletContext().getAttribute(
                    WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
                );
 
        // スケジューラを取得する。
        Scheduler scheduler = (Scheduler) wac.getBean("schedulerFactory");
 
        // スケジューラのコンテキストを取得する。
        SchedulerContext sc = scheduler.getContext();
 
        // 必要な設定を追加する。
        sc.put("hoge", objHoge);
        sc.put("fuga", objFuga);
 
 
        // スケジューラを開始する。
        scheduler.start();
 
 
        // その他、ジョブ以外のWebアプリ初期化処理。
 
 
    }
 
}

参考サイト


MLEXP. Wiki


トップ   編集 凍結解除 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2010-05-15 (土) 12:07:46 (5089d)