spring quartz

kong_resty 发布于 2012/07/24 18:03
阅读 2K+
收藏 2
package com.sample.quartz;

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

public class QuartSimple {
	/*
	 * 该方法为任务调度是自动调用的方法,在这里可以实现你要实现的功能
	 */
	public void test(){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH🇲🇲ss"); 
		System.out.println(sdf.format(new Date())+"时刻的调用");
	}
}

applicationContext.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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	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/tx
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<bean id="testQuartz" class="com.sample.quartz.QuartSimple"></bean>

	<!-- bean触发方法配置 -->
	<bean name="quartzBean"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<!-- bean名字 -->
		<property name="targetObject" ref="testQuartz" /><!--目标对象-->
		<!-- bean方法 -->
		<property name="targetMethod"><!--目标方法-->
			<value>test</value>
		</property>
		<property name="concurrent"><!--配置为false不允许任务并发执行-->
			<value>false</value>
		</property>
	</bean>
	
	 <!-- bean触发时间配置,指定具体的时间执行 -->   
     <bean id="quartzTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">   
          <!-- 触发bean配置 -->   
         <property name="jobDetail" ref="quartzBean" />   
          <!-- 触发时间配置 -->   
          <property name="cronExpression">   
             <value>0/10 * * * * ?</value>   
          </property> 
      </bean> 
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<!-- spring -->   
   <context-param>   
        <param-name>contextConfigLocation</param-name>   
        <param-value>classpath:applicationContext.xml</param-value> 
    </context-param>   
    <listener>   
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
    </listener> 
</web-app>

项目已经部署到tomcat服务器中了 但是当我开启tomcat 服务器的时候

却没有按我指定时间去执行程序

是不是还少了那些东西

project结构

 

加载中
0
serenity
serenity

没有配置调度器,添加

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

<property name="triggers">
<list>
<ref bean="quartzTrigger" />
</list>
</property>
</bean>
0
z
zzJess
<!-- 总管理调度 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="default" autowire="default">
	<property name="triggers">
		<list>
			<ref local="quartzTrigger" />
		</list>
	</property>
	<property name="autoStartup" value="true" />
</bean>


把这一段加上。


0
y
yusimiao
楼上正解,缺少Scheduler
返回顶部
顶部