欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

quartz定时定时任务执行两次

发布时间:2025/7/14 编程问答 40 豆豆
生活随笔 收集整理的这篇文章主要介绍了 quartz定时定时任务执行两次 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

quartz框架没问题。

流程:

sping-quartz配置

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd "><context:annotation-config/><task:annotation-driven/><!-- 业务对象 --> <bean id="testJobTask" class="com.sunyard.quartz.TestQuartzTask" /><!-- 自动扫描包 --><context:component-scan base-package="com.sunyard.quartz.*" /><!-- 定时任务start --><!-- 调度业务 --> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="testJobTask" /> <property name="targetMethod" value="execute" /> </bean><!-- 增加调用的触发器,触发时间 并注入到调度工厂--> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><property name="jobDetail" ref="jobDetail" /> <property name="cronExpression" value="0/10 * * * * ? *" /> </bean><!-- 设置调度工厂 注入触发器--><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref bean="cronTrigger" /></list></property></bean><!-- 定时任务end --></beans>

之后在web.xml中

<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mybatis.xml,classpath:spring-quartz.xml</param-value></context-param>

测试

/*** 定时任务--打印测试定时任务是否成功* @author wang**/ public class TestQuartzTask{public void execute() {System.out.println("定时任务已启动!");} }

之后发现启动之后输出两行,即任务启动两次。

最终解决:

<Host appBase="" autoDeploy="true" name="localhost" unpackWARs="true">

将appBase置空即可

转载于:https://www.cnblogs.com/nankeyimengningchenlun/p/9641181.html

总结

以上是生活随笔为你收集整理的quartz定时定时任务执行两次的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。