spring 定时任务代码实现

笔者用到的spring quartz定时任务的jar包 quartz-2.3.0.jar 和quartz-jobs-2.3.0.jar
在这里插入图片描述

  • 定义任务执行的类
import java.text.SimpleDateFormat;
import java.util.Date;

public class Job {
	public void task1() {
		System.out.println(nowTime() + ":执行任务一");
	}
	
	public void task2() {
		System.out.println(nowTime() + ":执行任务二");
	}
	
	private String nowTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");
		return sdf.format(new Date(System.currentTimeMillis()));
	}
}

  • 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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   
    
    <!-- 线程执行器配置,用于任务注册 -->
	<bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
	    <property name="corePoolSize" value="5" />
	    <property name="maxPoolSize" value="100" />
	    <property name="queueCapacity" value="500" />
	</bean>
	
	<bean id="jobObj" class="com.demo.Job" />
	<!-- 调度业务 -->
	<bean id="job1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
	    <property name="targetObject" ref="jobObj" />
	    <property name="targetMethod" value="task1" />
	</bean>
	<!-- 第二個 -->
	<bean id="job2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
	    <property name="targetObject" ref="jobObj" />
	    <property name="targetMethod" value="task2" />
	</bean>
	
	<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
	    <property name="jobDetail" ref="job1" />
	    <property name="cronExpression" value="*/1 * * * * ?" />
	</bean>
	
	<bean id="cronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
	    <property name="jobDetail" ref="job2" />
	    <property name="cronExpression" value="*/5 * * * * ?" />
	</bean>
	
	<!-- 设置调度 -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
	    <property name="triggers">
	        <list>
	            <ref bean="cronTrigger" />
	            <ref bean="cronTrigger1" />
	        </list>
	 
	    </property>
	 
	    <property name="taskExecutor" ref="executor" />
	</bean>
</beans>

在这里插入图片描述

  • 将quartz.xml 放入web容器中,即在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_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>earth</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:application.xml,classpath:quartz.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	<context-param>
		<description>日志配置文件</description>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:log4j.xml</param-value>
	</context-param>
	
	<!--  <context-param>
	    <param-name>log4jConfigLocation</param-name>
	    <param-value>classpath:log.properties</param-value>
	  </context-param> -->
	
</web-app>
  • 执行结果
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值