Spring 4.0.6+Quartz-2.2.1 定时器的配置

1. 项目结构图



 

2.    TestTimer.Java 代码

   package test;

import org.springframework.stereotype.Repository;

@Repository(value = "dispatchService")
public class TestTimer {

    public static int timer = -1;

    protected void executeInternal() {
        timer++;
        System.out.println(timer);
    }
}

 3. 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">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>


  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring.xml,/WEB-INF/TimerConfig.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

4.  TimerConfig.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" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd">


  <bean id="activateCardJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject">   <!-- targetObject是Spring定时器的特殊属性 -->
      <ref bean="dispatchService" /> <!-- 这个就是具体实现类,如果是注解,则必须为component指定value -->
    </property>
    <property name="targetMethod">    <!-- targetMethod是Spring定时器的特殊属性 -->
      <value>executeInternal</value><!-- 就是java类中的任务方法 -->
    </property>
  </bean>

  <bean id="sayRunTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail"><!-- jobDetail是Spring定时器的特殊属性 -->
      <ref bean="activateCardJobDetail" />
    </property>
    <property name="cronExpression">
      <value>0 */1 * * * ?</value><!-- cronExpression是Spring定时器的特殊属性 -->
    </property>
    <property name="startDelay">
      <value>0</value> <!-- 容器启动后 延时 0毫秒 后 定时器开始启动 -->
    </property>
  </bean>


  <bean autowire="no" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">    <!-- triggers是Spring定时器的特殊属性 -->
      <list>
        <ref bean="sayRunTrigger" />
      </list>
    </property>
    <property name="autoStartup" value="true" />  <!-- 自动启动定时任务 -->
  </bean>
</beans>

 

5.     spring.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:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:cache="http://www.springframework.org/schema/cache"    
xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  
                        http://www.springframework.org/schema/cache  
                        http://www.springframework.org/schema/cache/spring-cache-3.2.xsd  
                        http://www.springframework.org/schema/context  
                   http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
  <context:component-scan base-package="test" />
</beans>

6. spring-servlet.xml配置

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
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-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射 -->
  <mvc:annotation-driven />
  <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView">
    </property>
    <property name="prefix" value="/WEB-INF/view/"></property>
    <property name="suffix" value=".jsp"></property>
  </bean>
</beans>

7. jar 包结构



 

8.执行结果



 

附件上传工程的时候 老是卡着出错......

猜你喜欢

转载自tbk.iteye.com/blog/2103579
今日推荐