springmvc中实现quartz定时任务(每分钟的第3秒执行任务调度方法)

20 篇文章 0 订阅

1:实现触发器,最大的问题是jar包的处理(*.jar定时jar和sourcecodesource code):

此处,最关键的jar为第二个,名字最长。

maven依赖:

		<dependency>
			<groupId>org.apache.servicemix.bundles</groupId>
			<artifactId>org.apache.servicemix.bundles.spring-context-support</artifactId>
			<version>4.0.7.RELEASE_2</version>
		</dependency>
		<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<version>1.8.6</version>
		</dependency>

2:触发器在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"
	id="WebApp_ID" version="3.0">
	<display-name>webdemo1</display-name>
	  <!-- 监听spring上下文容器 -->
  <listener>
  	<listener-class>
  			org.springframework.web.context.ContextLoaderListener 
  	</listener-class>	
  </listener>
  <!-- 加载spring的xml配置文件到spring的上下文容器中 -->
  <context-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath*:applicationContext-*.xml</param-value>
  </context-param>
    <!-- 配置springmvc DispatcherServlet  -->
  <servlet>
  	<servlet-name>mvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>/WEB-INF/springmvc.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 配置DispatcherServlet需要拦截的url -->
  <servlet-mapping>
  	<servlet-name>mvc</servlet-name>
  	<url-pattern>*.html</url-pattern>
  </servlet-mapping>
	
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

3:springmvc的配置:

<?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:context="http://www.springframework.org/schema/context"  
	xmlns:mvc="http://www.springframework.org/schema/mvc"  
	xsi:schemaLocation="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
	http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
		<!-- springmvc配置 -->
		<!-- 通过component-scan让spring扫描package下的所有类,让spring的注解生效-->
		<context:component-scan base-package="com.tsxs"></context:component-scan>
		<!-- 配置springmvc的视图渲染器,让其前缀为:/ 后缀为: .jsp 将视图渲染到 /views/<method返回值>.jsp中 -->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/WEB-INF/views/"></property>
			<property name="suffix" value=".jsp"></property>
		</bean>
</beans>

4:定时任务配置文件

<?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-4.0.xsd"
	default-autowire="byName" default-lazy-init="false">
	<!-- default-autowire="byName" default-lazy-init="false"此两个值可以不配置 -->
	<description>Quartz Job Setting</description>
  <!-- A.配置调度的任务对应bean的id和自定义class-->
  <bean id="myQuartz" class="com.tsxs.tools.Quartz" />
  <!-- B.配置调度任务对应的bean的id和执行的方法,作业不并发调度-->
  <bean id="myDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="myQuartz" />
    <property name="targetMethod" value="tips" />
    <property name="concurrent" value="false" />
  </bean>
  <!-- C.配置调度任务执行的触发的时间-->
  <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="myDetail" />
     <property name="cronExpression">
     <!-- 每分钟的第3秒执行任务调度 -->
      <value>3 * * * * ?</value>
    </property>
  </bean>
  <!-- D.Quartz的调度工厂,调度工厂只能有一个,多个调度任务在list中添加 -->
  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
      <list>
         <!-- 所有的调度列表-->
        <ref bean="myTrigger" />
<!-- <ref bean="myTrigger1" />
        <ref bean="myTrigger2" />
        对应的bean配置:id="myDetail1" 和 id="myTrigger2" 可以对应的并行多配置-对应执行JavaBean和执行时间(各自触发time)
  -->
      </list>
    </property>
  </bean>
</beans>

注:时间配置:可以看quartz配置或者网络搜索“quartz定时配置”

5:定时任务执行JavaBean:

package com.tsxs.tools;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class Quartz {
//	public class Quartz implements Job{
//此处可以不实现Job接口的execute方法
//	private Date date;
	/**
	 * 定时任务,执行方法
	 * */
	public void tips(){
		String time = new SimpleDateFormat("MMM d,yyyy KK:mm:ss a",Locale.ENGLISH).format(System.currentTimeMillis());
		System.out.println("time:"+time);
	}

//	@Override
//	public void execute(JobExecutionContext context) throws JobExecutionException {
//		date = context.getFireTime();
//	}
}


6:运行结果:

time:Jun 24,2015 00:05:03 PM
time:Jun 24,2015 00:06:03 PM
time:Jun 24,2015 00:07:03 PM
time:Jun 24,2015 00:08:03 PM
time:Jun 24,2015 00:09:03 PM

 

注:

①:定时任务执行JavaBean可以不实现Job接口的execute方法

②:在定时任务配置中:设置default-lazy-init="true",否则定时任务不触发,如果不明确指明default-lazy-init的值,默认是false

③:在定时任务配置中:设置default-autowire="byName"的属性,可能导致后台会报org.springframework.beans.factory.BeanCreationException错误(此时,就不能通过Bean名称自动注入,必须通过明确引用注入)

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值