* Quartz [#t466b389]

下記サイトのチュートリアルあたりが参考になるはず。
- [[Quartz - opensymphony:http://www.opensymphony.com/quartz/]]


** 小技など [#yf66d9b3]

*** コンテキスト設定ファイルで設定してみる [#vf206bd3]

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

''struts.xml''(一部)
#code(HTML){{

	<constant name="struts.objectFactory" value="spring" />
	<constant name="struts.objectFactory.spring.autoWire" value="name" />

}}

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

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


#code(){{
#============================================================================
# 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''
#code(HTML){{
<?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''
#code(HTML){{
<?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''
#code(Java){{
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();


		// ここで定期的に実行したい処理を書く。


	}

}
}}

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

#code(Java){{
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アプリ初期化処理。


	}

}
}}


** 参考サイト [#qacb3708]

- [[naritocの日記:http://d.hatena.ne.jp/naritoc/searchdiary?word=%2a%5bQuartz%5d]]

-----
[[MLEXP. Wiki]]


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS