使用quartz框架自动发送报表。

来源:互联网 发布:网络歌曲歌词有天黑黑 编辑:程序博客网 时间:2024/04/28 07:58


自动报表,每天准时向oa系统发送数据:
使用到的技术:quartz+spring+mybatis+mysql

1)quartz在spring中的使用:

  a)创建applicationContext-quartz.xml,然后将applicationContext-quartz.xml加入:applicationContext.xml,如:
  <import resource="applicationContext-quartz.xml"/>,下面说一下在applicationContext-quartz.xml里面的设置:

       <!--new autoReporter-->
    <bean id="autoReport" class="com._21cn.analysis.sendoa.quartz.AutoReport"></bean>
    <!-- 定义调用对象和调用对象的方法 -->
    <bean id="autoReportDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 调用的类 -->
        <property name="targetObject">
            <ref bean="autoReport" />
        </property>
        <!-- 调用类中的方法 -->
        <property name="targetMethod">
            <value>doTask</value>
        </property>
    </bean>
    <!-- 定义触发时间 -->
    <bean id="autoReportTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="autoReportDetail" />
        </property>
        <!-- cron表达式 -->
        <property name="cronExpression">
            <!--每天早上的8点触发 -->
            <value>0 0 7 * * ?</value>
        </property>
    </bean>

       <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
    <bean id="startQuertz" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <!-- 发送OA报表由批次控制
                 <ref bean="doTime" />
                 -->
                 <ref bean="batchMailTrigger" />
                 <ref bean="batchExecuteTrigger" />
                 <ref bean="dataSyncTrigger" />
                 <ref bean="autoReportTrigger" />
            </list>
        </property>
    </bean>

首先,当然是设置detail了,包括执行哪个类的那个方法,如:com._21cn.analysis.sendoa.quartz.AutoReport的doTask方法。然后是
设置trigger,包括哪个detail,以及触发的时间。最后是总管理类中设置需要执行哪些trigger了。
  b)第二步当然是写具体的类和方法了:com._21cn.analysis.sendoa.quartz.AutoReport:
  public void doTask() {
        System.out.println("自动报表开始。。。。。");
         getRegister();
         getLogin();
         getMonth();
         getDirectLogin();
         getRoamLogin();
         getRoam();
         getVisit();

                 HttpClient client = new HttpClient();
         for (int i = 0; i < registIds.length; i++) {
         String url = "http://www.baidu.com:8000/cr/reportapi_save.asp?";
         regMap.get(i).setReValue(registerValues[i]);
         String name = null;
         try {
         name = URLEncoder.encode(regMap.get(i).getReName(), "GB2312");
         } catch (UnsupportedEncodingException e1) {
         log.info(e1.getMessage());
         }
         String paramStrs = "objective_id=" + regMap.get(i).getReId()
         + "&objective_name=" + name + "&objective_value="
         + regMap.get(i).getReValue() + "&objective_range="
         + TimeHelper.getEachDate();
         String url1 = url + paramStrs;
         url += paramStrs;
         PostMethod method = new PostMethod(url);
         try {
         client.executeMethod(method);
         } catch (Exception e) {
         log.info(e.getMessage());
         } finally {
         method.releaseConnection();// 释放连接
         }
         }
         System.out.println("注册发送成功。。。。。");

   上面我们可以看到doTask方法的作用:取数据,然后发送。取数据的主要用到mybatis+javaBean,发送主要用到httpclient.下面看看取数据

的过程:

 c)取数据准备:
   c1:定义javabean。
   public class Reporter {
    private Integer reId;
    private String  reName;
    private Integer reValue;
    private  String reDate;  

        get,set方法。

   c2:初始化数据:
       private static Integer[] loginValues = new Integer[36];
    private static Map<Integer, Reporter> loginMap = new HashMap<Integer, Reporter>();

    // 月登陆数
    private static Integer[] monthIds = { 291, 292, 293, 294, 295, 296, 297,
            299, 298 };
    private static String[] monthNames = { "月累计登录_综合平台门户", "月累计登录_189邮箱",
            "月累计登录_天翼云", "月累计登录_电话本", "月累计登录_A计划", "月累计登录_流量宝", "月累计登录_寻TA",
            "月累计登录_其他", "月累计登录_基地产品" };
    private static Map<Integer, Reporter> monthMap = new HashMap<Integer, Reporter>();
    private static Integer[] monthValues = new Integer[9];
    
        静态块初始化数据:
        static {
        
        for (int i = 0; i < monthIds.length; i++) {
            monthMap.put(i, new Reporter(monthIds[i], monthNames[i], 0, ""));
        }
        
     }
上面是讲需要发送的每个内容都封装为实体,但是值都为零,下面说说mybatis是怎么从数据库中取数据的。

  d)mybatis取数据:

   d1:取单独的数据。
      for (int j = 0; j < appKeys.length; j++) {
                Map<String, Object> regMap = new HashMap<String, Object>();
                regMap.put("gtStatDate", TimeHelper.getYesterDateTime());
                regMap.put("gtEndDate", TimeHelper.getRealDate());
                regMap.put("eqAppKey", appKeys[j]);
                regMap.put("eqRegSrc", source[i]);
                int tempCount = vappRegsrcService.getDailyIncreate(regMap);
                register.add(tempCount);
            }


      dao:
    public int getDailyIncrement(Map<String, Object> regMap) {
        Object obj=getSqlMapClientTemplate().queryForObject(getNameSpace()+".selectDailyIncByMap", regMap);
        if(obj==null){
            return 0;
        }else{
            return (Integer)obj;
        }
    }
     sqlmap:
<sql id="ByMap_Where_Clause" >
     <dynamic prepend="where" >
      <isPropertyAvailable prepend="and" property="eqAppKey" >
        app_key = #eqAppKey:VARCHAR#
      </isPropertyAvailable>
      <isPropertyAvailable prepend="and" property="inAppKeys" >
        app_key in ( $inAppKeys$ )
      </isPropertyAvailable>
      <isPropertyAvailable prepend="and" property="notInAppKeys" >
        app_key not in ( $notInAppKeys$ )
      </isPropertyAvailable>
      <isPropertyAvailable prepend="and" property="inAppKeyList" >
        app_key in <iterate prepend="" property="inAppKeyList" open="(" close=")"
          conjunction=",">#inAppKeyList[]:VARCHAR#</iterate>
      </isPropertyAvailable>
      <isPropertyAvailable prepend="and" property="notInAppKeyList" >
        app_key in <iterate prepend="" property="notInAppKeyList" open="(" close=")"
          conjunction=",">#notInAppKeyList[]:VARCHAR#</iterate>
      </isPropertyAvailable>
      <isPropertyAvailable prepend="and" property="gtStatDate" >
        stat_date <![CDATA[ >= ]]> #gtStatDate:DATE#
      </isPropertyAvailable>
       <isPropertyAvailable prepend="and" property="gtEndDate" >
        stat_date <![CDATA[ < ]]> #gtEndDate:DATE#
      </isPropertyAvailable>
      <isPropertyAvailable prepend="and" property="gtDayCnt" >
        day_cnt <![CDATA[ > ]]> #gtDayCnt:BIGINT#
      </isPropertyAvailable>
      <isPropertyAvailable prepend="and" property="eqAllCnt" >
        all_cnt = #eqAllCnt:BIGINT#
      </isPropertyAvailable>
      <isPropertyAvailable prepend="and" property="gtAllCnt" >
        all_cnt <![CDATA[ > ]]> #gtAllCnt:BIGINT#
      </isPropertyAvailable>
      <isPropertyAvailable prepend="and" property="gtCreateDate" >
        create_date <![CDATA[ > ]]> #gtCreateDate:TIMESTAMP#
      </isPropertyAvailable>
       <select id="selectDailyIncByMap" resultClass="java.lang.Integer" parameterClass="java.util.Map">
        select day_cnt from t_ums_stat_v_app_regsrc
        <isParameterPresent>
            <include refid="t_ums_stat_v_app_regsrc_sendoa.ByMap_Where_Clause" />
        </isParameterPresent>
      </select>



   d2:取一组数据。
     Map<String, Object> appMap = new HashMap<String, Object>();
            appMap.put("notInAppKeyList", appList);
            appMap.put("gtStatDate", TimeHelper.getYesterDateTime());
            appMap.put("gtEndDate", TimeHelper.getRealDate());
            List others = vappRegsrcService.getOthers(appMap);
            if (others != null) {
                for (int j = 0; j < others.size(); j++) {
                    sum = sum + (Integer) others.get(j);
                }
            }

    public List getOthers(Map<String, Object> appMap) {
        List obj=getSqlMapClientTemplate().queryForList(getNameSpace()+".selectOthersByMap", appMap);
        return obj;
    }

最主要的是:.queryForObject与.queryForList。还有sqlmap里面的:
 <isPropertyAvailable prepend="and" property="notInAppKeyList" >
        app_key in <iterate prepend="" property="notInAppKeyList" open="(" close=")"
          conjunction=",">#notInAppKeyList[]:VARCHAR#</iterate>
 </isPropertyAvailable>
这几个方面算是比较难的吧。



 


0 0
原创粉丝点击