疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

Java实现定时器,定时修改数据库代码


 

1.配置sprinbean文件:
<!-- 配置定时修改积数任务 -->
<bean name="payfitJob" class="com.unteck.fms.web.businessdata.controller.PayfitJob"/>
<bean id="fitmethodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="payfitJob" />
</property>
<property name="targetMethod">
<value>payfitJob</value>
</property>
<property name="concurrent" value="false"/>
</bean>
<!-- 配置修改积数触发器 -->
<bean id="payfitTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail"><ref bean="fitmethodInvokingJobDetail" /></property>
<!-- <property name="cronExpression"><value>1/30 * * * * ?</value></property> -->
<property name="cronExpression"><value>00 00 00 * * ?</value></property>//此处配置为每晚12点执行
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 添加触发器 -->
<property name="triggers">
<list>
<ref local="payfitTrigger" />
</list>
</property>
</bean> 
2.导入quartzjar包
3.定时器执行类:
package com.unteck.fms.web.businessdata.controller;
 
import org.springframework.beans.factory.annotation.Autowired;
 
import com.unteck.fms.web.businessdata.dao.MulAcctDao;
 
public class PayfitJob{
@Autowired
private MulAcctDao mulAcctDao; 
public void payfitJob(){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
// System.out.println("定时跑缴费任务");
OrderTimeTofit();
} catch (Exception e) {
e.printStackTrace();
System.err.print(e.getMessage());
}
}
});
thread.setName("payfitJob");
thread.start();
}
 
public void OrderTimeTofit(){
// MulAcctDao mulAcctDao = new MulAcctDao();
mulAcctDao.updateProduct();//此处对数据库进行修改
System.out.println("********");
 
 
}