quartz-spring.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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- for query storage --> <bean id="queryJob" class="com.ibm.cmcc.community.dynamic.quartz.query.QueryJob"> <property name="dynamicContentService" ref="dynamicContentService"></property> </bean> <bean id="queryJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- Bean的名字 --> <property name="targetObject"><ref bean="queryJob"/></property> <!-- Bean的执行方法 --> <property name="targetMethod"><value>execute</value></property> </bean> <bean id="queryJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="queryJobDetail"/> </property> <property name="cronExpression"> <value>0 0 0/1 * * ?</value> <!--<value>0 0/1 * * * ?</value>--> </property> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="queryJobTrigger"/> </list> </property> </bean> </beans>
java类
package com.ibm.cmcc.community.dynamic.quartz.query;
import java.io.IOException;
import java.util.List;
import net.sf.ehcache.server.soap.utils.CacheUtils;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import com.ibm.cmcc.community.dynamic.constant.Constant;
import com.ibm.cmcc.community.dynamic.model.RecDynamicContent;
import com.ibm.cmcc.community.dynamic.service.DynamicContentService;
/**
* 定时查询集团下的
* 推荐是省推荐个集团推荐的最新100条动态
* @author zhaining
*
*/
public class QueryJob {
private static ObjectMapper mapper = new ObjectMapper();
private static Logger logger = Logger.getLogger(QueryJob.class);
private DynamicContentService dynamicContentService;
/**
* 每小时去查一次数据库
* 生成json串,写到缓存里
*/
protected void execute(){
// try {
// //Constant.GROUP_REC_CREATE_JS
// //String rootPath=getClass().getResource("/").getFile().toString();
// String url=QueryJob.class.getResource("/").getFile().toString();
// System.out.println("url="+url);
// url=url.substring(url.indexOf("/")+1);
// System.out.println("url="+url);
// url=url.substring(0,url.lastIndexOf("/"));
// System.out.println("url="+url);
// url=url.substring(0,url.lastIndexOf("/"));
// System.out.println("url="+url);
// url=url.substring(0,url.lastIndexOf("/"));
// System.out.println("url="+url);
// //新文件所属目录
// File destFile = new File(url+"/group_rec_create");
// if (!destFile.exists())
// {
// destFile.mkdirs();
// }
// FileOutputStream fos=new FileOutputStream(url+"/group_rec_create/group_rec_create.js");
// fos.write(jsonString.getBytes());
// fos.flush();
// fos.close();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
List<RecDynamicContent> list = dynamicContentService.findProRecmDyna();
String jsonString="";
try {
jsonString = mapper.writeValueAsString(list);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
CacheUtils.put(Constant.GROUP_REC_DYNAMIC_JSON, "GROUP_REC_DYNAMIC_JSON", jsonString);
}
public DynamicContentService getDynamicContentService() {
return dynamicContentService;
}
public void setDynamicContentService(DynamicContentService dynamicContentService) {
this.dynamicContentService = dynamicContentService;
}
}