(1)ScheduledTimer.java:
package com.strongit.zwjc.infopush.vo;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.strongit.zwjc.common.dao.BaseDAO;
import com.strongit.zwjc.infopush.po.Afficheinfo;
import com.strongit.zwjc.infopush.po.InfopushDocClass;
import com.strongit.zwjc.infopush.po.Performance;
import com.strongit.zwjc.util.TimeKit1;
/**
* 定一个定时查询的类
*/
public class ScheduledTimer {
/**
* 定时运行的方法
*/
private BaseDAO baseDaoImpl;
@SuppressWarnings({ "unchecked", "deprecation" })
public void startRun() throws Exception {
// 第一,查询类型表中的规则
Calendar now = Calendar.getInstance();
@SuppressWarnings("unused")
int nowDay = now.get(Calendar.DAY_OF_MONTH);
int nowMonth = now.get(Calendar.MONTH) + 1;
List<InfopushDocClass> InfopushClassList = this.baseDaoImpl
.findAll(InfopushDocClass.class);
String docPushRule = "monthly:15";
@SuppressWarnings("unused")
String monthly = "";
String strDay = "";
int intDay = 0;
String classId = "";
String className = "" ;
String orgId = "";
String orgName = "orgName" ;
//InfopushDoc infopushDoc = null;
List infopushDocList = null ;
Performance performance = null ;
Afficheinfo afficheinfo = null ;
String strSfficheinfo = "" ;
List<Performance> performanceList = null ;
Date performanceDate = TimeKit1.getDateByString(TimeKit1.now("short"));
for (InfopushDocClass infopushDocClass : InfopushClassList) {
docPushRule = infopushDocClass.getDocPushRule();
classId = infopushDocClass.getClassId();
orgId = infopushDocClass.getOrgId();
monthly = docPushRule.split(":")[0];
strDay = docPushRule.split(":")[1];
intDay = Integer.parseInt(strDay);
//此方法只能适应与 月报
if (nowDay == intDay) {
infopushDocList = this.baseDaoImpl
.findAll("FROM InfopushDoc idoc where idoc.infopushDocClass.classId='"
+ classId
+ "' and idoc.orgId = '"
+ orgId
+ "'");
int totalsize = infopushDocList.size() ;
performanceList = this.baseDaoImpl.findAll(Performance.class);
if(totalsize>0){
boolean flag = false ;
for(Performance performance2:performanceList){
int performanceMonth = performance2.getPerformanceDate().getMonth()+1 ;
if(nowMonth==performanceMonth){
flag = true ;
break ;
}
}
if(flag){
}else{
// 有数据 加分
className = infopushDocClass.getClassName() ;
orgName = infopushDocClass.getOrgName() ;
performance = new Performance() ;
performance.setClassId(classId);
performance.setOrgId(orgId);
performance.setOrgName(orgName);
performance.setClassName(className);
performance.setPerformanceDate(performanceDate);
performance.setScore(Long.valueOf("1"));
this.baseDaoImpl.insert(performance);
}
}else{
boolean flag = false ;
for(Performance performance2:performanceList){
int performanceMonth = performance2.getPerformanceDate().getMonth()+1 ;
if(nowMonth==performanceMonth){
flag = true ;
break ;
}
}
if(flag){
}else{
// 没有数据 为0分,并且通报
orgName = infopushDocClass.getOrgName() ;
className = infopushDocClass.getClassName() ;
performance = new Performance() ;
performance.setClassId(classId);
performance.setClassName(className);
performance.setOrgId(orgId);
performance.setOrgName(orgName);
performance.setPerformanceDate(performanceDate);
performance.setScore(Long.valueOf("0"));
this.baseDaoImpl.insert(performance);
className = infopushDocClass.getClassName() ;
afficheinfo = new Afficheinfo() ;
afficheinfo.setClassId(classId);
afficheinfo.setOrgId(orgId);
afficheinfo.setAfficheinfoDate(performanceDate);
strSfficheinfo = "【"+TimeKit1.getNowDate()+"】【"+orgName+"】未推送【"+className+"】分类文献信息" ;
afficheinfo.setAfficheInfo(strSfficheinfo);
afficheinfo.setClassName(className);
afficheinfo.setOrgName(orgName);
this.baseDaoImpl.insert(afficheinfo);
}
}
}
}
System.out.println("成功插入!!");
}
public BaseDAO getBaseDaoImpl() {
return baseDaoImpl;
}
public void setBaseDaoImpl(BaseDAO baseDaoImpl) {
this.baseDaoImpl = baseDaoImpl;
}
}
applicationContext-infopush-timer.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">
<!-- 定时运行的类 -->
<bean id="scheduledTimer"
class="com.strongit.zwjc.infopush.vo.ScheduledTimer">
<property name="baseDaoImpl">
<ref bean="baseDAO" />
</property>
</bean>
<!--定义定时执行ScheduledTimer类中 的startRun()方法-->
<bean id="scheduledTask"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="scheduledTimer" />
</property>
<property name="targetMethod">
<value>startRun</value>
</property>
</bean>
<!--触发器的bean的设置,在这里我们设置了我们要触发的jobDetail是哪个。
这里我们定义了要触发的jobDetail,即触发器去触发哪个bean..并且我们还定义了触发的时间-->
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="scheduledTask" />
</property>
<property name="cronExpression">
<!-- 关键在配置此表达式 每天中午12点钟整触发事件-->
<value>0 0 12 * * ?</value>
</property>
</bean>
<!--管理触发器的总设置,可以放置多个触发器。-->
<bean autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>
两者结合就能实现定时运行程序!