配置spring定时器步骤:
1.搭建spring环境,同时需要quartz.jar的支持
2. 编写applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- - Application context definition for JPetStore's business layer. - Contains bean references to the transaction manager and to the DAOs in - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation"). --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 处理定时业务javaBean--> <bean id="quartjob" class="com.espeed.timer.ClickEmailSynTimer" ></bean> <!--Spring提供的类-- 提供2个属性--> <bean id="objAndmethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!--目标类(定时器启动后操作的类,timerMethod是方法名--> <property name="targetObject" ref="quartjob" /> <property name="targetMethod" value="timerMethod" /> </bean> <!--配置的时间--> <bean id="dotime" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="objAndmethod" /> </property> <property name="cronExpression"> <!--这里表示每1分钟启动1次,更多时间设置,查询cronExpression表达式—> <value>0 0/1 * * * ?</value> </property> </bean> <!-- 启动定时器--> <bean id="startQuartz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers" > <list> <ref bean="dotime"/> </list> </property> </bean> <!-- --> <bean id="updatetime" class="com.espeed.timer.InitTimerTask"> <property name="scheduler" ref="startQuartz" /> </bean> </beans>
3. 编写的InitTimerTask类(用于使spring 配置文件中修改的时间生效)
import org.quartz.Scheduler; import org.springframework.scheduling.quartz.CronTriggerBean; public class InitTimerTask { private static Scheduler scheduler; public void setScheduler(Scheduler scheduler){ InitTimerTask.scheduler = scheduler; } //用于修改默认的时间设定,仅仅在配置文件中进行 修改是不行的,必须提供此入口. public static void updateTime(String expression) { try { /* * 通过Scheduler.getTrigger("truggerName","GroupName")得到CronTriggerBean * 通过setCronExpression方法设置时间 * */ CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger("dotime", Scheduler.DEFAULT_GROUP); if(! trigger.getCronExpression().equalsIgnoreCase(expression)){ trigger.setCronExpression(expression); scheduler.rescheduleJob("dotime", Scheduler.DEFAULT_GROUP, trigger); } //trigger.setCronExpression(expression); System.out.println(trigger.getName() + ":"+ trigger.getCronExpression()); } catch (Exception e) { e.printStackTrace(); } } }
4. 编写定时器类ClickEmailSynTimer.java
import java.sql.CallableStatement; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.espeed.dao.ISysCompanyDao; import com.espeed.dao.impl.SysCompanyDaoImpl; import com.espeed.mail.util.getArea; import com.espeed.util.DBUtil; import com.espeed.util.IpLocationTool; import com.espeed.util.MakeJsonObject; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class ClickEmailSynTimer { private Connection conn; private ResultSet rs; private PreparedStatement pst; private ISysCompanyDao dao; private String grantDomain; private CallableStatement cs; public void timerMethod() throws SQLException { //step1: 查询远程未同步的数据 System.out.println("*********开始查询远程点读日志*********"); dao = new SysCompanyDaoImpl(); org.json.simple.JSONObject obj2 = dao.findCompanyGrant(); grantDomain = obj2.get("domain_list").toString(); conn = TimerTaskUtil.getConnection(); pst = conn.prepareStatement("SELECT * FROM tb_singreadinfo WHERE mail_company_domain =? AND mail_has_syn = -1"); pst.setString(1, grantDomain); rs = pst.executeQuery(); JSONArray datas = MakeJsonObject.makeArray(rs); TimerTaskUtil.close(rs, pst, conn); if(conn!=null) { conn.close(); } //step2.将未同步的日志数据插入到本地数据库 System.out.println("***********将未同步的日志数据插入到本地数据库************"); conn = DBUtil.getConnection(); String sql =""; int batchsize=0; for(int i=0;i<datas.size();i++) { sql = "INSERT INTO eml_mail_click_log( mail_id, mail_uid, link_id, click_time, view_ip ) VALUES (?,?,?,?,?)"; pst = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); JSONObject obj = datas.getJSONObject(i); System.out.println(obj.toString()); pst.setInt(1, Integer.parseInt(obj.getString("mail_id"))); pst.setString(2,obj.getString("mail_uid") ); pst.setInt(3,1 ); pst.setString(4,obj.getString("mail_readDate")); pst.setString(5, obj.getString("mail_readIp")); pst.executeUpdate(); } DBUtil.close(rs, pst, conn); System.out.println("*****start update********"); //step3.更新发送结果表中的点击归属地 conn = DBUtil.getConnection(); System.out.println("**********开始更新数据IP库************"); sql = "update eml_mail_click_log set view_zone= ? where mail_id= ?"; String zone=""; for(int i=0;i<datas.size();i++) { pst = conn.prepareStatement(sql); zone = IpLocationTool.getCity(datas.getJSONObject(i).getString("mail_readIp")); //本地查找不到地址,查询远程. if(zone.equals("")||zone==""||zone==null) { zone = getArea.getArea(datas.getJSONObject(i).getString("mail_readIp")); } pst.setString(1, zone); pst.setInt(2,datas.getJSONObject(i).getInt("mail_id")); pst.executeUpdate(); } //step4. 执行存储过程,计算发送结果 System.out.println("**************** 执行存储过程,计算发送结果********************"); conn = DBUtil.getConnection(); cs = conn.prepareCall("{call sp_eml_click_result()}"); cs.executeUpdate(); DBUtil.close(rs, pst, conn); //step5. 更新远程数据库中是否同步字段为已经同步 1:已同步 -1:未同步 conn = TimerTaskUtil.getConnection(); sql = " update tb_singreadinfo set mail_has_syn = 1 where " + " mail_company_domain = ? and sing_cid= ? "; int flag = 0; for(int i=0;i<datas.size();i++) { pst = conn.prepareStatement(sql); pst.setString(1, grantDomain); pst.setInt(2,datas.getJSONObject(i).getInt("sing_cid") ); pst.executeUpdate(); } TimerTaskUtil.close(rs, pst, conn); } }