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

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名称自动注入,必须通过明确引用注入)

时间: 2024-09-17 03:28:25

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

j2ee项目中quartz定时任务的疑问?

问题描述 请问在j2ee项目中可以设置多个定时任务吗?若可以是不是要考虑用多线程来实现定时任务?因为我在操作项目的同时有可能定时任务也在执行,并发操作了.疑惑中....请亲们指点迷津!!!!注(定时任务用的是spring的quartz框架)? 问题补充:hao117 写道 解决方案 这样是不行的,因为Quartz是和spring分离的你的类都需要自己去注入,而且你的services不应该写到一起去啊建议把定时任务类,写在单独的类中自己写个Utils类 里面定义个static 的Applicati

Spring集成Quartz定时任务框架介绍和Cron表达式详解

原文地址:http://www.cnblogs.com/obullxl/archive/2011/07/10/spring-quartz-cron-integration.html 在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但时调度控制非常不方便,并且我们需要大量的代码.使用Quartz框架无疑是非常好的选择,并且与Spring可以非常方便的

(2)Spring集成Quartz定时任务框架介绍和Cron表达式详解

在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但时调度控制非常不方便,并且我们需要大量的代码.使用Quartz框架无疑是非常好的选择,并且与Spring可以非常方便的集成,下面介绍它们集成方法和Cron表达式的详细介绍. 一.增加所依赖的JAR包1.增加Spring的Maven依赖 <dependency><groupId>org.

关于spring+quartz定时任务问题!!

问题描述 关于spring+quartz定时任务问题!! 怎么把定时任务写在表中,项目启动加载表数据,一个定时任务对应一个时间,在类中该怎么调用?数据写在一张表中.有人做过这种项目吗?求教啊.. 解决方案 关于spring和quartz的使用,可参考:http://git.oschina.net/wangkang/llsfw 希望能够帮到你.

khj-spring整合quartz定时任务报错,请大神们解救

问题描述 spring整合quartz定时任务报错,请大神们解救 web.xml中的配置 spring_mvcorg.springframework.web.servlet.DispatcherServlet contextConfigLocationclasspath:applicationContext spring_mvc/ applicationContext.xml的配置 <!-- 要调用的工作类 --> <!-- 定义调用对象和调用对象的方法 --> <!-- 调

java quartz2 2 1使用-quartz 2.2.1 为什么不会执行myjob中的函数

问题描述 quartz 2.2.1 为什么不会执行myjob中的函数 代码如下: myjob拓展了job接口,可是程序执行的时候没有执行myjob中的print public class QuartzTest { public void run() throws Exception { // 调度工厂 SchedulerFactory sf = new StdSchedulerFactory(); // 从工厂中,获取一个任务调度实体 Scheduler sched = sf.getSchedu

quartz 定时任务被挂起问题

问题描述 场景: 一个quartz定时任务,定时调用一个方法,方法里面通过多数据源从三个数据源ds1, ds2, ds3,获取数据,然后将这批数据插入另一个数据源 ds4的表中,再将这批数据从 ds4中查出来进行业务分析, 用的c3p0 连接池, 报错:[下面有贴出来] 同时 数据源 ds1 数据库出现异常, 报错后,数据源 ds1, ds2, ds3 的数据不能同步到 ds4 了,ds4的业务也不在执行,定时任务被挂起.. 这种场景 有可能是什么原因 引起定时任务被挂起的啊? WARN [co

Quartz 定时任务延时10个小时

问题描述 各位前辈高手们,请教一个问题:我最近在一个系统中添加一个定时任务.系统中的Quartz已经是搭建好的,而且已经有定时任务在运行了.我现在添加任务的方法是向表中直接插入数据:insertintoPUB_P_QZ_JOB_DETAILSvalues('job4','DEFAULT','','AutoJob(类名)','1','0','0','1','');insertintoPUB_P_QZ_TRIGGERSvalues('trigger4','DEFAULT','job4','DEFAU

在Java的Spring框架中配置Quartz的教程_java

Spring中配置Quartz的过程: 1.导入JAR包 quartz需要的JAR包,已经包含在spring中,位置在spring解压后目录的 \lib\quartz 下的quartz-all-1.6.1.jar, 将其拷贝到工程 的 WEB-INF/lib 下就行了. 2.配置web.xml,让spring启动时加载quartz的配置文件 <?xml version="1.0" encoding="UTF-8"?> <web-app versio