spring_quartz配置多定时任务

spring_quartz配置多定时任务

1:引入pom.xml

<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<version>1.8.6</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

2:配置web.xml

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
			classpath:/spring/*.xml
		
  
  
    org.springframework.web.context.ContextLoaderListener
  

3:配置调度器,触发器,任务

<!-- 任务1配置 -->
	<bean name="job1" class="com.quartz.demo.Job1Demo"/>
	<bean id="jobDetail_1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<!-- 执行的类 -->
		<property name="targetObject">
			<ref bean="job1" />
		</property>
		<!-- 类中的方法 -->
		<property name="targetMethod">
			<value>sayHello</value>
		</property>
	</bean>
	<bean id="cronTrigger_1" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="jobDetail_1" />
		</property>
		<!-- 每一秒钟执行一次 -->
		<property name="cronExpression">
			<value>0/1 * * * * ?</value>
		</property>
	</bean>
	
	<!-- 任务2配置 -->
	<bean name="job2" class="com.quartz.demo.Job2Demo"/>
	<bean id="jobDetail_2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref bean="job2" />
		</property>
		<property name="targetMethod">
			<value>sayHello</value>
		</property>
	</bean>
	<bean id="cronTrigger_2" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="jobDetail_2" />
		</property>
		<property name="cronExpression">
			<value>0/1 * * * * ?</value>
		</property>
	</bean>
	
	<!-- 总配置 -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<!-- 添加触发器 -->
		<property name="triggers">
			<list>
				<ref bean="cronTrigger_1" />
				<ref bean="cronTrigger_2" />
			</list>
		</property>
	</bean>

4:编写java类

import java.util.Date;
 
public class Job1Demo {
	public void sayHello() {
		System.out.println(new Date() + " -> Hello, 我是任务 2");
	}
}
import java.util.Date;
 
public class Job2Demo {
	public void sayHello() {
		System.out.println(new Date() + " -> Hello, 我是任务 1");
	}
}

运行
按配置定时任务执行

core配置参考:https://www.pppet.net/

https://gitee.com/thsoc/spring_quartz?_from=gitee_search

以上是配置文件配置定时任务;

以下是java代码实现定时任务:

任务类,触发器,配置放入调度器中

spring_quartz配置多定时任务_第1张图片

https://gitee.com/selfly/spring-quartz-demo?_from=gitee_search

你可能感兴趣的:(ssm,quartz)