`
kfcman
  • 浏览: 384129 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

spring配置时钟具体注意细节和代码

 
阅读更多
本来很简单的时钟今天配的时候却弄了我2个小时,在这里跟大家分享下错误经验。希望能帮助大家少走弯路。

1,注意jar包,特别是commons-collections.jar这个包的版本要3.x以上的。
2,注意编译jdk和运行jdk的版本问题要不然会报:java.lang.UnsupportedClassVersionError: Bad version number in .class file ...错误

配置一个服务时钟的类源码:
1,ClockService.java:处理时钟业务
Java代码  收藏代码

    public class ClockService { 
         
        public void doService(){ 
             
            System.out.println("do Service!"); 
             
        } 
    } 



2,ClockTime.java:时钟类(QuartzJobBean所属quartz-all-1.6.0.jar版本不一,这里是我用的版本)
Java代码  收藏代码

    public class ClockTime extends QuartzJobBean{ 
     
        protected void executeInternal(JobExecutionContext arg0) 
                throws JobExecutionException { 
            System.out.println("============== Time Clock Start ============="); 
            ClockService service = new ClockService(); 
            service.doService(); 
            System.out.println("============== Time Clock End ============="); 
        } 
         
    } 



3,applicationContext-clock.xml配置时钟
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-2.0.xsd"> 
                 
        <!-- 时钟  --> 
        <bean id="quartzClock" 
            class="org.springframework.scheduling.quartz.JobDetailBean"> 
            <property name="jobClass"> 
                <value>com.fairy.spring.clock.ClockTime</value> 
            </property> 
            <property name="jobDataAsMap"> 
                <map> 
                    <!--   spring 的依赖注入  --> 
                    <!-- <entry key="service"><ref local="handleOverTimeClockServiceImpl"/></entry>  --> 
                </map> 
            </property> 
        </bean> 
        <bean id="quartzClockTask" 
            class="org.springframework.scheduling.quartz.SimpleTriggerBean"> 
            <property name="jobDetail"> 
                <ref bean="quartzClock" /> 
            </property> 
            <property name="startDelay"> 
                <value>6000</value> 
                <!--    这里是服务启动后延时多少时间,开始计时任务,单位ms--> 
            </property> 
            <property name="repeatInterval"> 
                <value>30000</value> 
                <!--   这里是每隔多长时间就进行一次计时任务,单位ms --> 
            </property> 
        </bean> 
        <bean id="cronQuartzClock" 
            class="org.springframework.scheduling.quartz.CronTriggerBean"> 
            <property name="jobDetail"> 
                <ref bean="quartzClock"></ref> 
            </property> 
            <property name="cronExpression"> 
                <!-- <value>0 38 13 * * ?</value> --> 
                 <value>0,5,10,15,20,25,30,35,40,45,50,55 * * * * ?</value> 
                <!--
                <value>0 0,10,20,30,40,50,59 * * * ?</value>
                 --> 
                <!--定时在任何月份任何日期(不管星期几)的22点52分0秒 --> 
                <!-- 一个cron表达式有到少6个(也可能是7个)由空格分隔的时间元素.从左到右,这些元素的定义如下: 
                    1.秒(0-59) 
                    2.分钟(0-59) 
                    3.小时(0-23) 
                    4.月份中的是期(1-31) 
                    5.月份(1-12或SUN-DEC) 
                    6.星期中的日期(1-7或SUN-SAT) 
                    7.年份(1970-2099)  
                    例子: 
                    0 0 10,14,16 * * ? 每天上午10点,下午2点和下午4点 
                    0 0,15,30,45 * 1-10 * ? 每月前10天每隔15分钟 
                    30 0 0 1 1 ? 2012 在2012年1月1日午夜过30秒时 
                    0 0 8-5 ? * MON-FRI 每个工作日的工作时间 
                     
                    - 区间 
                    * 通配符 
                    ? 你不想设置那个字段 
                --> 
            </property> 
     
        </bean> 
        <bean 
            class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
            <property name="triggers"> 
                <list> 
                    <ref bean="cronQuartzClock"></ref> 
                </list> 
            </property> 
        </bean> 
        <!-- end 时钟  --> 
    </beans> 



4,web.xml配置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>    
         
http://lochen514.iteye.com/blog/1273240
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics