dongdong12345_
dongdong12345_
2017-03-08 01:51

Java中使用quartz定时器:定时处理类中却不能注入jdbc的接口类,报空指针

  • spring

定时器配置文件:

<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法-->

<bean id="cathodicInspectJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <!--false表示等上一个任务执行完后再开启新的任务-->
    <property name="concurrent" value="false"/>
    <property name="targetObject">
        <ref bean="cathodicInspectTaskJob"/>
    </property>
    <property name="targetMethod">
        <value>run</value>
    </property>
</bean>

<!--  调度触发器 -->
<bean id="cathodicInspectTrigger"
      class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail">
        <ref bean="cathodicInspectJob" />
    </property>
    <property name="cronExpression">
        <value> 0 39 09 * * ? </value>
    </property>
</bean>

<!-- 调度工厂 -->
<bean id="scheduler2" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="cathodicInspectTrigger"/>
        </list>
    </property>
</bean>


处理类:CathodicInspectTimer

package com.geok.gas;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.geok.core.common.exception.GeoException;
import com.geok.core.common.util.DateTime;
import com.geok.core.common.util.PIMConfig;
import com.geok.core.jdbc.def.IJdbcDao;
import com.geok.system.common.OrgUtil;
import com.project.web.util.UUID;

public class CathodicInspectTimer {
@Autowired
private IJdbcDao jdbcDao;

private String table_name =PIMConfig.DATABASE_OWNERBY+"pi";//阴保检测工作表(要插入数据的表)
private String PC_USER_MENU =PIMConfig.DATABASE_OWNERBY+"PC_USER_MENU";//用户菜单表
private String PC_USER =PIMConfig.DATABASE_OWNERBY+"PC_USER";//系统用户表

String time=DateTime.getDateTime();//系统日期和时间,格式: yyyy-MM-dd HH:mm:ss
String yearMonth=DateTime.getYearMonth();//系统当前年月,格式: yyyy-MM

public void run(){
    System.out.println("阴保作业任务开始   running!");
    insertData();
}

@SuppressWarnings("rawtypes")
private void insertData(){
    try {
        long start = System.currentTimeMillis();//运行起始时间
        List personList = this.getPersonList();//获取系统用户数据
        Map<String,Map> personMap = new HashMap<String, Map>();//以数据的id为key,数据本身为map的map对象
        for(int i=0;i<personList.size();i++){
            Map map = (Map)personList.get(i);
            personMap.put(map.get("EVENTID").toString(), map);
        }

        List list=this.getPersonData("010202020108");//阴保测试作业有新增权限的数据集合
        String sqlinsert = "INSERT INTO "+table_name+" (EVENTID, DEPARTMENTID, CREATEID, CREATENAME, CREATEDATE, MODIFYID, MODIFYNAME, MODIFYDATE,WORKNAME)";//要插入的列
        OrgUtil util = new OrgUtil();//部门工具类

        for(int j=0;j<list.size();j++){//循环拼接sql语句来添加数据
            Map map1 = (Map)list.get(j);
            String USEREVENTID = (String)map1.get("USEREVENTID");//获取有权限的用户id
            Map map2 = personMap.get(USEREVENTID);//通过用户id获取该用户对象
            sqlinsert += " select '"+UUID.getUUID()+"','"+map2.get("ORGID")+"','"
                        +map2.get("EVENTID")+"','"+map2.get("NAME")+"','"+time+"','"
                        +map2.get("EVENTID")+"','"+map2.get("NAME")+"','"+time+"','"
                        +yearMonth.replace("-", "年")+"月"+util.getOrgName(map2.get("ORGID").toString())+"阴保检测作业'"
                        +" FROM DUAL UNION ";
        }
        if(sqlinsert.indexOf("UNION")>0){//判断数据集合中存在数据
            sqlinsert = sqlinsert.substring(0, sqlinsert.length()-6);
            jdbcDao.insertOrUpdate(sqlinsert);
        }

        long end = System.currentTimeMillis();//运行结束时间
        System.out.println("插入:"+list.size()+"条  "+ (end - start) + "毫秒");
        System.out.println("阴保作业任务结束   running!");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private List getPersonData(String menuId) throws GeoException{
    String sql = "SELECT * FROM "+PC_USER_MENU+" T WHERE T.MENUID = '"+menuId+"'";
    return jdbcDao.queryForMapList(sql);
}

/**
 * 获取系统用户数据
 * @return
 * @throws GeoException
 */
private List getPersonList() throws GeoException{
    String sql = "SELECT T.EVENTID,T.NAME,T.ORGID FROM "+PC_USER+" T WHERE T.ACTIVE = 1";
    return jdbcDao.queryForMapList(sql);
}

}


  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答