最近忙于项目,一直没有更新blog,请大家见谅。
在项目中使用了Lucene全文检索,考虑到系统的特性,把Lucene的全文检索索引创建时间放在夜晚,已减轻系统的压力。
首先,需要写一个类,这个类是用来执行具体的操作。也就是你想做什么事情,这个类需要extends org.springframework.scheduling.quartz.QuartzJobBean 类。
比如:
package com.finegold.digimus.service.config;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import com.finegold.digimus.comm.StringHelper;
import com.finegold.digimus.lucene.index.service.imp.AddCatalogArticleDataDocument;
import com.finegold.digimus.lucene.index.service.imp.AddMediaContentDocument;
import com.finegold.digimus.lucene.index.service.imp.IndexFactory;
import com.finegold.digimus.service.CatalogArticleDataService;
import com.finegold.digimus.service.MediaContentService;
import com.finegold.digimus.service.bean.IndexPath;
/**
* @author 汪心利 2007-9-6 下午03:41:29
* @copyRigth FineGold 2007
* @Describle 定时创建Lucene索引任务的定时器
*/
public class IndexQuartz extends QuartzJobBean {
private Logger logger = Logger.getLogger(IndexQuartz.class);
private MediaContentService mediaContent;
private CatalogArticleDataService catalogArticleData;
private IndexPath indexPath;
/**
* @return the indexPath
*/
public IndexPath getIndexPath() {
return indexPath;
}
/**
* @param indexPath
* the indexPath to set
*/
public void setIndexPath(IndexPath indexPath) {
this.indexPath = indexPath;
}
/**
* @return the catalogArticleData
*/
public CatalogArticleDataService getCatalogArticleData() {
return catalogArticleData;
}
/**
* @param catalogArticleData
* the catalogArticleData to set
*/
public void setCatalogArticleData(
CatalogArticleDataService catalogArticleData) {
this.catalogArticleData = catalogArticleData;
}
/**
* @return the mediaContent
*/
public MediaContentService getMediaContent() {
return mediaContent;
}
/**
* @param mediaContent
* the mediaContent to set
*/
public void setMediaContent(MediaContentService mediaContent) {
this.mediaContent = mediaContent;
}
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
//在这里加入你的操作
StringBuffer logInfo = new StringBuffer();
logInfo.append("在").append(StringHelper.encodeHTML(new Date())).append(
"开始执行创建索引的任务调度");
logger.info(logInfo.toString());
try {
List m_list = mediaContent.findByMediaid(null, null);
IndexFactory.getInstance().createIndex(m_list,
new AddMediaContentDocument(),
indexPath.getMediaIndexPath());
List c_list = catalogArticleData.loadAllData();
IndexFactory.getInstance().createIndex(c_list,
new AddCatalogArticleDataDocument(),
indexPath.getCatalogIndexPath());
logger.info(StringHelper.encodeHTML(new Date()) + "创建索引任务完成!");
} catch (Exception e) {
logger.error("使用Spring定时器创建索引出错!");
}
}
}
然后在applicationContext.xml中配置:
<!-- =========================Quartz TimeTask 配置 ========================= -->
<bean id="indexJob"
class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>
com.finegold.digimus.service.config.IndexQuartz<!-- 刚刚写的类-->
</value>
</property>
<property name="jobDataAsMap">
<map><!-- 类里一些属性 key:propertyName-->
<entry key="indexPath">
<ref local="indexPath" />
</entry>
<entry key="catalogArticleData">
<ref bean="catalogArticleDataService" />
</entry>
<entry key="mediaContent">
<ref bean="mediaContentService" />
</entry>
</map>
</property>
</bean>
<!-- 配置那个任务在何时执行 -->
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="indexJob" />
</property>
<property name="cronExpression">
<value>0 9 * * * ?</value>
<!--<value>* * 23 * * ?</value>-->
</property>
</bean>
<!-- 将任务放入 SchedulerFactoryBean-->
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
这只是其中的一种方式,还有其它的方式。请等待......