Quartz作业调度(2)

本文介绍如何在Spring框架中使用Quartz实现定时任务。通过配置applicationContext-quartz.xml文件,定义了两个不同的定时任务及其触发器。任务分别按不同频率执行,并详细展示了配置过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在spring中使用

applicationContext-quartz.xml

<?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: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-3.2.xsd        
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.2.xsd        
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">


	<!-- 定时任务 -->
	<bean id="quartzJob" class="com.test.quartz.ScheduleJob" scope="prototype" />

	<!-- 绑定任务bean 和目标方法 -->
	<bean id="jobOne"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref bean="quartzJob" />
		</property>
		<property name="targetMethod">
			<value>JobOne</value>
		</property>
		<property name="concurrent" value="false" />
	</bean>
	<!-- 设置执行时间 -->
	<bean id="jobOneTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="jobOne"></property>
		<!-- 系统会在每分钟的0秒执行调度任务。 -->
		<property name="cronExpression">
			<!-- 每秒执行 -->
			<value>0/30 * * * * ?</value>
		</property>
	</bean>

	<!-- 定时任务2-->
	<bean id="jobTwo"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref bean="quartzJob" />
		</property>
		<property name="targetMethod">
			<value>JobTwo</value>
		</property>
		<property name="concurrent" value="false" />
	</bean>
	<!-- 设置执行时间 -->
	<bean id="jobTwoTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="jobTwo"></property>
		<!-- 系统会在每分钟的0秒执行调度任务。 -->
		<property name="cronExpression">
			<!-- 每秒执行 -->
			<value>0/1 * * * * ?</value>
			<!-- 每分钟执行1次 -->
			<!-- <value>0 0/01 * * * ?</value> -->
		</property>
	</bean>

	<!-- 调度器执行 -->
	<bean id="startQuertz" lazy-init="false"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers" >
			<list>
				<ref bean="jobOneTrigger" />
				<ref bean="jobTwoTrigger" />
			</list>
		</property>
	</bean>
</beans>


java

package com.test.quartz;

import java.util.Date;

public class ScheduleJob {

	public void JobOne()
	{
		System.out.println("Job 1 "+ new Date());
	}
	
	public void JobTwo()
	{
		System.out.println("Job 2 "+ new Date());
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值