zoukankan      html  css  js  c++  java
  • springschemaSpring定时任务

    本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~

        
    Spring准时任务的功能很壮大,上次简单应用一下,给大家分享下,希望大家多多交流!

        这是一个准时打印时光控制台,这是一个简单准时任务!

        请看程序的运行原码:

        首先新建一个类:TellingTheTimeJob这个类是继承于Spring重写executeInternal这个方法。

    package jobs;
    
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    import org.springframework.scheduling.quartz.QuartzJobBean;
    import service.ITellingTheTimeService;
    
    
    /**
     * @ProjectName:报时Demo   
     * @ClassName:TellingTheTimeJob   
     * @Description:   
     * @author:Sheep
     * @date:2012-4-19 下昼03:58:11   
     * @Modifier: 
     * @Modify Date:  
     * @Modify Note:   
     * @version
     */
    public class TellingTheTimeJob extends QuartzJobBean {
    	
    	private ITellingTheTimeService tellingTheTimeService = null;
    
    	@Override
    	protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
    		//调用报时方法
    		this.tellingTheTimeService.tellingTheTime();
    	}
    
    	public ITellingTheTimeService getTellingTheTimeService() {
    		return tellingTheTimeService;
    	}
    
    	public void setTellingTheTimeService(
    			ITellingTheTimeService tellingTheTimeService) {
    		this.tellingTheTimeService = tellingTheTimeService;
    	}
    }

        建立一个接口ITellingTheTimeService,功能为了实现准时调用

    package service;
    
    
    /**
     * @ProjectName:报时Demo 
     * @ClassName:ITellingTheTimeService   
     * @Description:   
     * @author:Sheep
     * @date:2012-4-19 下昼03:59:37   
     * @Modifier: 
     * @Modify Date:  
     * @Modify Note:   
     * @version
     */
    public interface ITellingTheTimeService {
    	
    	/**
    	 * @Title: tellingTheTime 
    	 * @Description: 报当前时光      
    	 * @throws
    	 */
    	void tellingTheTime();
    }

        实现类

    package service.impl;
    
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    import service.ITellingTheTimeService;
    
    
    /**
     * @ProjectName:报时Demo
     * @ClassName:TellingTheTimeServiceImpl   
     * @Description:   
     * @author:Sheep
     * @date:2012-4-19 下昼03:59:06   
     * @Modifier: 
     * @Modify Date:  
     * @Modify Note:   
     * @version
     */
    public class TellingTheTimeServiceImpl implements ITellingTheTimeService {
    
    	/**@Description: 报时
    	 * @see service.ITellingTheTimeService#tellingTheTime()
    	 */
    	public void tellingTheTime() {
    		// TODO Auto-generated method stub
    		Calendar now = Calendar.getInstance();
    		System.out.println("现在是北京时光:"+this.format(now.getTime()));
    	}
    
    	public String format(Date date) {
    		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    		return format.format(date);
    	}
    }

        applicationContext 配置文件

        每日一道理
    悲观的人,先被自己打败,然后才被生活打败;乐观的人,先战胜自己,然后才战胜生活。悲观的人,所受的痛苦有限,前途也有限;乐观的人,所受的磨难无量,前途也无量。在悲观的人眼里,原来可能的事也能变成不可能;在乐观的人眼里,原来不可能的事也能变成可能。悲观只能产生平庸,乐观才能造就卓绝。从卓绝的人那里,我们不难发现乐观的精神;从平庸的人那里,我们很容易找到阴郁的影子。
    <?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"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
    	<bean id="tellingTheTimeService" class="service.impl.TellingTheTimeServiceImpl"/>
    	
    	<!-- 配置一个Job -->
    	<bean id="tellTheTimeJob" class="org.springframework.scheduling.quartz.JobDetailBean">
    		<property name="jobClass" value="jobs.TellingTheTimeJob"/>
    		<property name="jobDataAsMap">
    			<map>
    				<entry key="tellingTheTimeService" value-ref="tellingTheTimeService"></entry>
    			</map>
    		</property>
    	</bean>
    	
    	<!-- 简单的触发器 -->
    	<bean id="simpleTellTheTimeTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
    		<property name="jobDetail">
    			<ref bean="tellTheTimeJob" />
    		</property>
    		<!-- 以毫秒为单位,启动后一分钟触发 -->
    		<property name="startDelay">
    			<value>60000</value>
    		</property>
    		<!-- 每距离一分钟触发一次 -->
    		<property name="repeatInterval">
    			<value>60000</value>
    		</property>
    	</bean>
    
    	<!-- 复杂的触发器 -->
    	<bean id="complexTellTheTimeTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    		<property name="jobDetail">
    			<ref bean="tellTheTimeJob"/>
    		</property>
    		<property name="cronExpression">
    			<!-- 这里是重点,可以自定义表达式实现准时触发。以下含义是每分钟触发一次 -->
    			<value>0 0/1 * * * ?</value>
    		</property>
    	</bean>
    	
    	<!-- Spring触发工厂 -->
    	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    		<property name="triggers">
    			<list>
    				<ref bean="complexTellTheTimeTrigger"/>
    				<!-- ....上面可以继续添加其他触发器 -->
    			</list>
    		</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 XML上下文路径 -->
      <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>

        这样就可以实现spring准时任务了,所引用的jar是:commons-collections.jar,commons-logging-1.0.4.jar,log4j-1.2.15.jar,quartz-1.6.1-RC1.jar和spring.jar(2.5) 。

        

    文章结束给大家分享下程序员的一些笑话语录: 刹车失灵
    有一个物理学家,工程师和一个程序员驾驶着一辆汽车行驶在阿尔卑斯山脉 上,在下山的时候,忽然,汽车的刹车失灵了,汽车无法控制地向下冲去, 眼看前面就是一个悬崖峭壁,但是很幸运的是在这个悬崖的前面有一些小树 让他们的汽车停了下来, 而没有掉下山去。 三个惊魂未定地从车里爬了出来。
    物理学家说, “我觉得我们应该建立一个模型来模拟在下山过程中刹车片在高 温情况下失灵的情形”。
    工程师说, “我在车的后备厢来有个扳手, 要不我们把车拆开看看到底是什么 原因”。
    程序员说,“为什么我们不找个相同的车再来一次以重现这个问题呢?”

    --------------------------------- 原创文章 By
    spring和schema
    ---------------------------------

  • 相关阅读:
    Javascript是单线程的深入分析
    非阻塞式JavaScript脚本介绍
    javascript 关于函数的返回值
    javascript运算符的优先级
    JavaScript 中的 this
    javascript中关于坐标 大小 的描述
    Javascript引擎单线程机制及setTimeout执行原理说明
    回车登录页面的问题
    有关架构的若干思考
    Bootstrap3 模态框 select2搜索框无法输入
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3102302.html
Copyright © 2011-2022 走看看