spring中轻松实现定时任务,quartz表达式的在线Cron表达式生成器推荐

蒋啸
2023-12-01

东西虽简单,保留下来,开箱即用,省时省力!!!!!


1.首先在pom.xml中引入quartz相关包

 <dependency>  
            <groupId>org.quartz-scheduler</groupId>  
            <artifactId>quartz</artifactId>  
            <version>2.2.1</version>  
        </dependency>  
        <dependency>  
            <groupId>org.quartz-scheduler</groupId>  
            <artifactId>quartz-jobs</artifactId>  
            <version>2.2.3</version>  
        </dependency>  


2.在spring 配置文件中加入

  <!--要调度的对象 -->
<bean id="jobBean" class="com.cqliving.cloud.controller.module.info.InfoClick" />
<bean id="jobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

<!-- 目标对象-->

<property name="targetObject" ref="jobBean" />

<!-- 执行目标对象的execute方法-->
<property name="targetMethod" value="execute" />
<property name="concurrent" value="false" />
</bean>


<bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail" />

<!--不熟悉表达式该怎么写的,请参照 表达式在线生成网站,很给力哦。--http://cron.qqe2.com/>
<property name="cronExpression" value="0 0 0/1 * * ? " />
</bean>


<bean id="startQuertz"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
lazy-init="false">
<property name="triggers">
<list>
<ref bean="trigger" />
</list>
</property>
</bean>


3.InfoClick类的代码为:





@Component
public class InfoClick {


public void execute() throws JobExecutionException {
System.out.println("开始执行定时任务-----");
}
}


 类似资料: