spring+quartz实现定时任务支持集群
程序员文章站
2022-07-10 12:18:10
...
最近项目要做集群,然后找了下资料发现quartz本身就支持集群,但是需要引入一些表,这里废话就少说了,这里就直接上代码吧,也提供了对应的代码下载https://github.com/zqh1989/quartz
1,首先下载quartz的jar包,找到docs的dbTables目录下边的对应的sql文件引入项目中
如图:我这边用的是mysql
2,quartz.properties配置
#============================================================== #Configure Main Scheduler Properties #============================================================== org.quartz.scheduler.instanceName = quartzScheduler org.quartz.scheduler.instanceId = AUTO #============================================================== #Configure ThreadPool #============================================================== org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 10 org.quartz.threadPool.threadPriority = 5 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true #============================================================== #Configure JobStore #============================================================== org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate org.quartz.jobStore.tablePrefix = QRTZ_ org.quartz.jobStore.isClustered = true org.quartz.jobStore.clusterCheckinInterval = 20000 org.quartz.jobStore.dataSource = myDS #============================================================== #Configure DataSource \uFF08\u6B64\u5904\u586B\u4F60\u81EA\u5DF1\u7684\u6570\u636E\u5E93\u8FDE\u63A5\u4FE1\u606F\uFF09 #============================================================== org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver org.quartz.dataSource.myDS.URL = jdbc\:mysql\://192.168.9.223:3306/cloud_live_course?createDatabaseIfNotExist\=true&characterEncoding\=utf-8 org.quartz.dataSource.myDS.user = root org.quartz.dataSource.myDS.password = ablejava org.quartz.dataSource.myDS.maxConnections =30
3,spring-time.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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <!-- 调度器lazy-init='false'那么容器启动就会执行调度程序 --> <bean id = "startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:quartz.properties" /> <property name ="triggers"> <list> <ref bean ="doTime" /> </list> </property> <!-- 允许在Quartz上下文中使用Spring实例工厂 --> <property name = "applicationContextSchedulerContextKey" value="applicationContext" /> </bean> <!-- 触发器 --> <bean id = "doTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name = "jobDetail" ref="jobtask"></property> <!-- cron表达式 --> <property name = "cronExpression" value="10,15,20,25,30,35,40,45,50,55 * * * * ?"> </property> </bean> <!-- 任务 --> <bean id="jobtask" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass"> <value>zhang.quartz.MyDetailQuartzJobBean</value> </property> <property name="jobDataAsMap"> <map> <entry key="targetObject" value="synUsersJob"/> <entry key="targetMethod" value="execute"/> </map> </property> </bean> <!-- 要调用的工作类 --> <bean id = "synUsersJob" class="zhang.quartz.core.SynUsersJob"></bean> </beans>
4,web.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>liveCourse</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring/spring-config.xml classpath:spring/spring-time.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--转码 --> <filter> <filter-name>SpringEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>SpringEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- session超时定义,单位为分钟 --> <session-config> <session-timeout>60</session-timeout> </session-config> </web-app>
最后看执行效果,我这边启动了两个tomcat只有一个会起作用,然后停掉其中一个另一个马上就起来了