quartz中怎么整合spring

发布时间:2021-08-12 16:41:04 作者:Leah
来源:亿速云 阅读:96

这篇文章将为大家详细讲解有关quartz中怎么整合spring,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

maven依赖:

<dependency>
	<groupId>org.quartz-scheduler</groupId>
	<artifactId>quartz</artifactId>
	<version>2.0.2</version>
</dependency>

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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
	<bean name="quartz_scheduler" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="configLocation" value="classpath:quartz.properties" />
		<property name="triggers">
			<list>
				<ref bean="updateAccountTrigger" />
				<!-- other Trigger -->
			</list>
		</property>
	</bean>


	<!-- 定义调用对象和调用对象的方法 -->
	<bean id="updateAccountTask"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="sysTimerServiceImpl" />
		<property name="targetMethod" value="updateAllAccounts" />
		<property name="concurrent" value="false" />
	</bean>
	<!-- 9-19点每隔一个小时触发 -->
	<bean id="updateAccountTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="updateAccountTask" />
		<property name="cronExpression" value="0 0 9-19 * * ?" />
	</bean>
	

</beans>
	

quartz.properties:	
	
	#============================================================================
	# Configure Main Scheduler Properties  
	#============================================================================
	org.quartz.scheduler.instanceName: quartz_scheduler_jobs
	org.quartz.scheduler.instanceId: AUTO
	org.quartz.scheduler.skipUpdateCheck: true
	#============================================================================
	# Configure ThreadPool  
	#============================================================================
	org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
	org.quartz.threadPool.threadCount: 5
	org.quartz.threadPool.threadPriority: 5

定时任务的内容:

// 调用对象
@Service("sysTimerServiceImpl")
public class SysTimerServiceImpl implements SysTimerService {

	// 调用方法
	[@Override](https://my.oschina.net/u/1162528)
	public void updateAllAccounts() {
		// ...
	}

}

关于quartz中怎么整合spring就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. Springboot整合quartz产生错误怎么办
  2. Spring怎么整合Quartz开发

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

quartz spring

上一篇:Docker中如何使用非root用户

下一篇:openstack中如何使用Ubuntu镜像

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》