码迷,mamicode.com
首页 > 编程语言 > 详细

Spring定时实际项目运用

时间:2014-12-08 10:50:50      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:spring定时

applicationContext-timer.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
	
	<!-- 微博数据抓取 -->
	<bean id="weiboManager" class="com.wiseweb.pom.service.weibo.WeiboStatusManager"></bean>
	<bean id="usersManager" class="com.wiseweb.pom.service.weibo.WeiboUsersManager"></bean>
	<bean id="assignUsersManager" class="com.wiseweb.pom.service.weibo.WeiboAssignUsersManager"></bean>
	<bean id="ntManager" class="com.wiseweb.pom.service.tracking.NegativeTrackingManager"></bean>
	<bean id="classPersonalManager" class="com.wiseweb.pom.service.classify.ClassPersonalManager"></bean>
	<bean id="searchManager" class="com.wiseweb.pom.service.docInfo.SearchManager"></bean>
	<bean id="cacheCountManager" class="com.wiseweb.pom.service.cache.CacheCountManager"></bean>
	<bean id="docInfoManager" class="com.wiseweb.pom.service.docInfo.DocInfoManager"></bean>
	<!-- 微博数据抓取 -->
	<bean id="weiboCrawJob" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass" value="com.wiseweb.util.timer.SinaWeiboCrawTimer"></property>
		<property name="jobDataAsMap">
			<map>
				<entry key="weiboManager" value-ref="weiboManager"></entry>
				<entry key="usersManager" value-ref="usersManager"></entry>
				<entry key="assignUsersManager" value-ref="assignUsersManager"></entry>
				<entry key="ntManager" value-ref="ntManager"></entry>
			</map>
		</property>
	</bean>
	<!-- 微博数据抓取 -->
	<bean id="weiboCrawTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="weiboCrawJob"></property>
		<!-- 30分运行一次; -->
		<property name="cronExpression" value="0 0/30 * * * ?" />
	</bean>
	
	<!-- 个人keyword提取 -->
	<bean id="personalCrawJob" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass" value="com.wiseweb.util.timer.PersonalCrawTimer"></property>
		<property name="jobDataAsMap">
			<map>
				<entry key="classPersonalManager" value-ref="classPersonalManager"></entry>
				<entry key="searchManager" value-ref="searchManager"></entry>
				<entry key="cacheCountManager" value-ref="cacheCountManager"></entry>
			</map>
		</property>
	</bean>
	<!-- 个人keyword提取 -->
	<bean id="personalTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="personalCrawJob"></property>
		<!-- 30分运行一次; -->
		<property name="cronExpression" value="1 0 0 * * ?" />
	</bean>
	
	<!-- 地图图表信息存储 -->
	<bean id="mapchartCrawJob" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass" value="com.wiseweb.util.timer.MapChartCrawTimer"></property>
		<property name="jobDataAsMap">
			<map>
				<entry key="docInfoManager" value-ref="docInfoManager"></entry>
				<entry key="cacheCountManager" value-ref="cacheCountManager"></entry>
				<entry key="searchManager" value-ref="searchManager"></entry>
			</map>
		</property>
	</bean>
	<!-- 地图图表信息存储 -->
	<bean id="mapchartTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="mapchartCrawJob"></property>
		<property name="cronExpression" value="0 10 0 * * ?" />
		<!--<property name="cronExpression" value="0 0/1 * * * ?" />
	--></bean>
	
	<bean id="scheduledFactoryBean"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				 <ref bean="weiboCrawTrigger"/> 
				 <ref bean="personalTrigger"/>  
				 <ref bean="mapchartTrigger"/>
			</list>
		</property>
	</bean>
</beans>		

给出某一个定时类的写法

package com.wiseweb.util.timer;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.wiseweb.pom.entity.CacheCount;
import com.wiseweb.pom.entity.ClassConfigPersonal;
import com.wiseweb.pom.entity.ClassTreePersonal;
import com.wiseweb.pom.entity.ClassTreePersonalKeyword;
import com.wiseweb.pom.entity.ClassifyPersonalRole;
import com.wiseweb.pom.entity.SearchInfo;
import com.wiseweb.pom.service.cache.CacheCountManager;
import com.wiseweb.pom.service.classify.ClassPersonalManager;
import com.wiseweb.pom.service.docInfo.SearchManager;

public class PersonalCrawTimer extends QuartzJobBean{
	private SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd") ;
    private SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
    private ClassPersonalManager classPersonalManager ;
    private SearchManager searchManager ;
    private CacheCountManager cacheCountManager ;
    private Integer order = 1; // 按时间排序
	private String queryValue = "";
	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		System.out.println("*************执行关键字定时开始***********");
		flushPersonal() ;
		System.out.println("*************执行关键字定时结束***********");
	}
	
	public void flushPersonal() {
		List<ClassConfigPersonal> ccp = this.cacheCountManager.getAllConfig() ;
		//设置时间戳
		Long nowTime = System.currentTimeMillis()/1000 ;
		
		for(ClassConfigPersonal c : ccp) {
			Long userId = c.getUserId() ;
			List<ClassTreePersonal> resultObject = new ArrayList<ClassTreePersonal>() ;
				//根据用户id获取到classConfigPersonal集合
			   List<ClassConfigPersonal> ccps = this.classPersonalManager.findAll(userId);
			   for(int i=0;i<ccps.size();i++) {
				   //个人配置id
				   Long classConfigId = ccps.get(i).getId() ; 
				   //个人配置id & 用户id找到personalRole
				   List<ClassifyPersonalRole> classifyRoles = this.classPersonalManager
							.findByClassConfigRole(userId, classConfigId);
				   List<ClassTreePersonal> classTreePersonals = this.classPersonalManager
							.findAllByClassConfigId(classConfigId);
				   
				   for (int k = 0; k < classTreePersonals.size(); k++) {
						ClassTreePersonal ctn = classTreePersonals.get(k);
						for (int j = 0; j < classifyRoles.size(); j++) {
							if (ctn.getId().equals(
									classifyRoles.get(j).getTreeNodeId())) {
								resultObject.add(ctn) ;
							}
						}
					}
			   }
			int size = resultObject.size() ;
			for(int i=0;i<size;i++) {
				String name = resultObject.get(i).getName() ;
				Long id = resultObject.get(i).getId() ;
				ClassTreePersonalKeyword ctnk2 = this.classPersonalManager.findKeywordBy("nodeNid",Integer.parseInt(id+""));
				SearchInfo searchInfo = ctnk2SearchInfo(ctnk2) ;
				//传入keyword和用户id执行入库操作
				try {
					executeSql(name, searchInfo, userId) ;
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println("________________________");
		System.out.println("执行操作已完成,进行删除旧数据操作...");
		//执行完后执行删除旧数据操作
		List<Long> deleteIds = this.cacheCountManager.getAllIdForDelete(nowTime) ;
		this.cacheCountManager.delete(deleteIds) ;
		System.out.println("数据删除已完成...");
	}
	
	public void executeSql(String keywordName, SearchInfo searchInfo, Long userId) throws ParseException {
		
		for(int i=30;i>0;i--) {
			String riqi = getStrDate(String.valueOf(i)) ;
			long start = sdf1.parse(riqi).getTime() ;
			long end = sdf2.parse(riqi + " 23:59:59").getTime() ;
			int count = this.searchManager.search(searchInfo,start/1000,end/1000);
			CacheCount cc = new CacheCount() ;
			cc.setkUid(userId) ;
			cc.setkName(keywordName) ;
			cc.setkCount(count + "") ;
			cc.setkTime(riqi) ;
			cc.setkInserttime(System.currentTimeMillis()/1000) ;
			this.cacheCountManager.inStorage(cc) ;
			System.out.println(keywordName + "_" + riqi + "已入库...");
		}
	}
	private SearchInfo ctnk2SearchInfo(ClassTreePersonalKeyword ctnk2) {
	    SearchInfo si = new SearchInfo() ;
	    si.setOrder(this.order);
		if (ctnk2.getQueryAnd() != null
				&& !"".equals(ctnk2.getQueryAnd())) {
			si.setQueryValue(ctnk2.getQueryAnd() + " "
					+ this.queryValue);
		}
		if (ctnk2.getQueryOr() != null
				&& !"".equals(ctnk2.getQueryOr())) {
			si.setOr(ctnk2.getQueryOr());
		}
		if (ctnk2.getQueryNot() != null
				&& !"".equals(ctnk2.getQueryNot())) {
			si.setNot(ctnk2.getQueryNot());
		}
		return si ;
	}
	
	public String getStrDate(String backDay) {
		Calendar calendar = Calendar.getInstance() ;
		calendar.add(Calendar.DATE, Integer.parseInt("-" + backDay));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ;
		String back = sdf.format(calendar.getTime()) ;
		return back ;
	}

	public ClassPersonalManager getClassPersonalManager() {
		return classPersonalManager;
	}

	public void setClassPersonalManager(ClassPersonalManager classPersonalManager) {
		this.classPersonalManager = classPersonalManager;
	}

	public Integer getOrder() {
		return order;
	}

	public void setOrder(Integer order) {
		this.order = order;
	}

	public String getQueryValue() {
		return queryValue;
	}

	public void setQueryValue(String queryValue) {
		this.queryValue = queryValue;
	}

	public SearchManager getSearchManager() {
		return searchManager;
	}

	public void setSearchManager(SearchManager searchManager) {
		this.searchManager = searchManager;
	}

	public CacheCountManager getCacheCountManager() {
		return cacheCountManager;
	}

	public void setCacheCountManager(CacheCountManager cacheCountManager) {
		this.cacheCountManager = cacheCountManager;
	}
}


Spring定时实际项目运用

标签:spring定时

原文地址:http://blog.csdn.net/benjamin_whx/article/details/41799707

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!