前两天做项目,会有结算,结算是晚上凌晨定时任务开始结算,本人采用的是Quertz,具体的方法如下:
<?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.xsd ">
<!--class是定时任务的实现类的路径-->
<bean id="AutoTask" class="com.test.bill.bms.task.ArticleAutoTask"></bean>
<bean id="AutoTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="AutoTask"/>
<property name="targetMethod" value="execute"/>
<property name="concurrent" value="false"/>
</bean>
<bean id="AutoTaskJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="AutoTaskJob"/>
</property>
<property name="cronExpression">
<!--晚上0点10分-->
<value>0 10 0 * * ?</value>
</property>
</bean>
<bean name="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="AutoTaskJobTrigger"/>
</list>
</property>
</bean>
</beans>
当时我的项目在服务器上面的环境是Tomcat,将项目打包成var包,第二天查数据库,发现定时任务直接运行了两次,里面的insert数据全部都添加了两条,即使我在前面有是否重复判断,直接绕过我的判断!!
后来查询各种百度,最终找到了原因,是因为默认的Tomcat配置要进行修改。
默认的tomcat配置server.xml是这样的
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
将上面的Host节点的代码改为下面:
本文章为博主原创文章,未经允许禁止转载!