馬光軍--------BLOG

          BlogJava 聯系 聚合 管理
            1 Posts :: 25 Stories :: 5 Comments :: 0 Trackbacks
          (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>

          兩者結合就能實現定時運行程序!

          posted on 2008-12-02 22:27 馬光軍 閱讀(1351) 評論(0)  編輯  收藏 所屬分類: Spring2.xSpring1.x

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 阿荣旗| 长海县| 宁蒗| 车险| 鲁甸县| 台前县| 莒南县| 敦煌市| 遵化市| 齐齐哈尔市| 常山县| 西充县| 吴桥县| 长白| 定西市| 平塘县| 通江县| 延川县| 前郭尔| 阿克| 霍城县| 泰安市| 桃园市| 梅州市| 集贤县| 屏东县| 龙江县| 临武县| 云林县| 五河县| 新龙县| 深水埗区| 曲阜市| 比如县| 秀山| 西乌珠穆沁旗| 定日县| 城口县| 郁南县| 卢氏县| 金阳县|