spring3定时器简单配置

最近在做oa项目中写到一个功能,就是员工每天的签到和签退.当时想了很久都没有想出来,后来自己上网查了一下spring的定时器,然后就有了思路.

下面我贴上自己用到的这个定时器的配置.希望能够和大家一起学习.

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2 xmlns:task="http://www.springframework.org/schema/task"
 3 http://www.springframework.org/schema/task
 4     http://www.springframework.org/schema/task/spring-task-3.0.xsd">
 5
 6 <!-- spring触发器定时器开关 -->
 7     <task:annotation-driven />
 8       <task:scheduled-tasks>
 9             <!--这里设置每天的8:30点钟,执行考勤方法,检查每个员工谁没有签到 -->
10           <task:scheduled ref="taskManual" method="autoManual" cron="0 30 8 * * ?" />
11       </task:scheduled-tasks>
12 </beans>

这个是我的实体类:

 1 public class AutoManual {
 2 /**
 3  * 自动检测考勤方法
 4  * @author 吕兵阳
 5  * @return
 6  */
 7 public void autoManual(){
 8 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:config/applicationContext.xml");
 9 ManualsignService manualsignService = (ManualsignService) applicationContext.getBean("manualsignService");
10 EmployeeService employeeService = (EmployeeService) applicationContext.getBean("employeeService");
11 List<Manualsign>manuals = manualsignService.findManualignsByToday();//获取所有的今天签到的考勤
12 List<Employee> employees = employeeService.findEmployees();//获取所有的员工
13 System.out.println("总共的员工数:"+employees.size());
14 List<Employee> temp = new ArrayList<Employee>();
15 for (Manualsign manual : manuals) {
16 for (Employee employee : employees) {
17 if(manual.getEmployee().getEmpId().equals(employee.getEmpId())){
18 temp.add(employee);
19 }
20 }
21 }
22 employees.removeAll(temp);
23 for (Employee employee : employees) {
24 if(employee!=null){
25 Manualsign manual = new Manualsign();
26 manual.setEmployee(employee);
27 manual.setEndTime(null);
28 Calendar cal = Calendar.getInstance();
29 manual.setStartTime(cal.getTime());
30 manual.setManOffStatus(null);
31 manual.setManStatus(2);
32 manualsignService.addManualsign(manual);
33 }
34 }
35 System.out.println("今天没有签到的员工数:"+employees.size());
36 }
37 }

这个是我的spring里面配置的bean:

1 <bean id="taskManual" class="com.accp.office.util.AutoManual"></bean>

首先要引入spring定时器这个命名空间,然后我们只需要简单的配置.ref是我们在spring里面配置的一个pojo类,也就是一个普通的 bean,method方法,指向我们需要调用的那个方法,如果有多个可以用","号隔开,cron的配置大家可以看这方面的文档,当然我给大家提供一 份:

其中时间的定义按以下例子模仿 
表达式 
Java代码 
意义

 1 "0/10 * * * * ?" 每十秒触发
 2 "0 0/1 * * * ?" 每一分钟触发
 3
 4 "0 0 12 * * ?" 每天中午12点触发
 5 "0 15 10 ? * *" 每天上午10:15触发
 6 "0 15 10 * * ?" 每天上午10:15触发
 7 "0 15 10 * * ? *" 每天上午10:15触发
 8 "0 15 10 * * ? 2005" 2005年的每天上午10:15触发
 9 "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
10 "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
11 "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
12 "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
13 "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
14 "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
15 "0 15 10 15 * ?" 每月15日上午10:15触发
16 "0 15 10 L * ?" 每月最后一日的上午10:15触发
17 "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
18 "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
19 "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

关于spring3定时器cron参数设置每月最后一天不支持L

<!-- spring定时器 -->
      <bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
       <property name="targetObject">
       <ref bean="taskCount"/>
       </property>
       <property name="targetMethod">
       <value>autoCountTimes</value>
       </property>
   </bean>
   <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
       <property name="jobDetail">
           <ref bean="methodInvokingJobDetail"/>
       </property>
       <!-- 配置每个月的最后一天的23点59分,调用修改用户月登陆次数的方法 -->
       <property name="cronExpression">
           <value>0 59 23 L * ?</value>
       </property>
   </bean>
   <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
       <property name="triggers">
           <list><ref local="cronTrigger"/></list>
       </property>
    </bean>

我采用了和quartz方式的.其中里面的taskCount是我在bean里面配置的要执行定时任务的那个bean的id,

autoCountTimes指的是要执行的那个方法.这样就可以用带L那个corn表达式了.
时间: 2024-08-28 21:57:49

spring3定时器简单配置的相关文章

[spring-framework]Spring定时器的配置和使用

开发中我们常常会做一些定时任务,这些任务有开始时间,并会按一定的周期或规则执行.如此我们在Java程序开发中使用定时器来处理定时任务. <!-- MessageRequestTask类中包含了msgRequest方法,用于执行定时任务 --> <bean id="msg_request" class="com.santorini.task.timer.MessageRequestTask"></bean> <!-- 定时器配

Spring定时器的配置和使用

开发中我们常常会做一些定时任务,这些任务有开始时间,并会按一定的周期或规则执行.如此我们在Java程序开发中使用定时器来处理定时任务. <!-- MessageRequestTask类中包含了msgRequest方法,用于执行定时任务 --> <bean id="msg_request" class="com.santorini.task.timer.MessageRequestTask"></bean> <!-- 定时器配

Spring定时器的配置

Spring定时器的配置(注解+xml)方式 (2013-09-30 10:39:18)转载▼ 标签: spring定时器配置 spring定时器注解方式 spring定时器xml方式 spring配置 分类: Spring Spring 配置定时器(注解+xml)方式—整理 一.注解方式 1. 在Spring的配置文件ApplicationContext.xml,首先添加命名空间 xmlns:task="http://www.springframework.org/schema/task&qu

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

[工作必备]spring定时器简单的demo

原文:[工作必备]spring定时器简单的demo 源代码下载地址:http://www.zuidaima.com/share/1586950010391552.htm 本月的最后一天上班季.给大家分享一个工作中经常用到的一个算得上是技术吧.我在网上找了好多方法,都是简单的描述,并没有案例.今天抽时间做出来了一个简单的demo.希望可以帮助大家. 如图,我在这里配置的定时器是每10秒执行一次. 其实很简单,重点是在配置applicationContext.xml里面的cronExpression

0123简单配置LNMP

简单配置LNMP不怕出现错误,就怕错误不知道出现在哪里?看日值tail -f /var/log/message -- 系统整个的日志tail -f /var/log/nginx/error.log -- 单个应用的日志http://www.cnblogs.com/make217/p/5836864.htmlhttp://www.cnblogs.com/xiaoit/p/3991037.html

Samba服务器的简单配置

案例说明: 公共目录        public   /abc            ro  允许任何人匿名访问, Daiqing1        smbdq1   /tmp/daiqing1   rw  不允许他人访问 Daiqing2        smbdq2   /tmp/daiqing2   rw  不允许他人访问 Samba服务器的简单配置,布布扣,bubuko.com

Linux安装MariaDB和简单配置

1.安装MariaDB 安装命令 yum -y install mariadb mariadb-server 安装完成MariaDB,首先启动MariaDB systemctl start mariadb 设置开机启动 systemctl enable mariadb 接下来进行MariaDB的相关简单配置 mysql_secure_installation 首先是设置密码,会提示先输入密码 Enter current password for root (enter for none):<–初

linux ntp安装简单配置

公司的一台服务器硬件坏了挂了,其中就有ntp服务端,写一下ntp服务端的简单配置. 1.首先安装ntp,centos 系统执行 yum install ntp 2.写入配置文件/etc/ntp.conf如下 restrict 192.168.18.153 mask 255.255.255.0 nomodify notrap restrict 192.168.18.0 mask 255.255.255.0 nomodify server time.pool.aliyun.com #server 1