Spring MVC使用Schedule实现定时任务

Schedule存在spring-context.jar包中。

实现简单步骤:

1、配置bean.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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                             http://www.springframework.org/schema/beans/spring-beans.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.xsd 
                             http://www.springframework.org/schema/task  
                           http://www.springframework.org/schema/task/spring-task.xsd ">

    <!-- 开启定时任务 -->
    <task:annotation-driven />

    <context:component-scan base-package="com.jsoft.testspring" />

    <context:annotation-config />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    
</beans>

代码实现:

package com.jsoft.testspring.testmvchelloworld;

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

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component  
public class ScheduleTest {  

    @Scheduled(cron = "0/5 * * * * ?")  
    public void schTest1() {  
        Date date = new Date();  
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        String dateStr = sim.format(date);  
        System.out.println("这是spring定时器1,每五秒执行一次,当前时间:" + dateStr);  
    }  
}  

注意要加@Component这类的注解。

示例工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test24/testmvchelloworld

 

参考:

http://blog.csdn.net/tuzongxun/article/details/51576301

转载于:https://www.cnblogs.com/EasonJim/p/7820280.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在上一个资源v1.0基础上的增强和改进! 1、解压。解压得到名为"schedule"的目录 2、打开界面。双击里面的schedule v1.1.html打开 3、解除限制。单击浏览器弹出框“允许阻止的内容”,可以看到效果 功能说明: 界面采用bootstrap和JEasyUI技术实现,提供三种任务运行规则:一次性、周期性、自定义 1、一次性(i:立即运行;ii:在规定的时间刻运行) 2、周期性(i:按小时;ii:按天 iii:按周; iv:按月(日);v: 按月(星期) ) 3、自定义(自定义功能,用户可以在前台随意定制执行计划,只要符合spring schedule cronExpression语法) 使用说明: 一: /* line 96 */ var action = "edit";//edit,add,view action用来指定用户动作,新增、编辑(修改)、查看 二: /* line 108 */ var cronExpression = "13 12 11 1 11 ? 2017";//when action is edit or view this value is useful cronExpression 的建意值: cronExpression=""; // action为空时 cronExpression =$("cronExpression");//当action为edit或view时, 把后面传过来的表达式值赋给cronExpression,界面会自动判断该如何展示 三: /* line 628 */ $("#sbmt").click(function() {//按钮事件....} 表单按钮提交事件相关代码自已根据实际情况替换修改。 亮点: i:日期选择功能使用JEasyUI的datetime,并在选择框上加了限制,不能选择早于当前的日期 ii:时间选择功能使用JEasyUI的spinner实现 iii: 自定义功能用户可以随意定制执行计划 iv:采用bootstrap的pills实现tab(选项卡)功能,界面友好 v: 打开修改界面时界面会根据后台cronExpression值智能展示tab和radio及表单值。规则:优先顺序:一次性、周期性、自定义 vi: 提交前javascript会对cronExpression进行严格的规则验证 vii: 使用javascript正则表达式实现各定时分类的匹配展示 viii: 强兼容性,集成时能与其它css样式文件兼容,尽量把schedule.html内部样式表中的样式放在目标集成界面中样式的后面避免覆盖 另:内附schdule Spring后台核心代码供后台开发参考

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值