SSH + Quartz实现定时器

准备条件

  • jdk1.8
  • ssh框架
  • quartz-1.8.6.jar(放到项目的lib下)
  • 其他jar包
    这是依赖的所有的jar

spring配置

<?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"
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">
<context:annotation-config/>
//指向任务类,定时器具体做的事(只是一个普通的类)
<bean name="job3" class="cn.com.trueway.sys.util.PojJob"/>
//要执行的对象
<bean id="jobDetail_3" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
	<property name="targetObject">
		<ref bean="job3" />
	</property>
	//要执行对象的方法
	<property name="targetMethod">
		<value>job</value>
	</property>
</bean>
//配置定时的执行时间
<bean id="cronTrigger_3" class="org.springframework.scheduling.quartz.CronTriggerBean">
	<property name="jobDetail">
		<ref bean="jobDetail_3" />
	</property>
	<property name="cronExpression">
		<value>20 28 14 ? * *</value>//这里表示下午2点28分20秒,有cron表达式,具体不做累述
	</property>
	
</bean> 
//总调度器,可以执行多个定时任务,我这里只举了一个列子
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
	<property name="triggers">
		<list>
			<ref bean="cronTrigger_3"/>
		</list>
	</property>
</bean> 

任务类

package cn.com.trueway.sys.util;

import java.util.List;

import javax.annotation.Resource;

import cn.com.trueway.sms.util.SendMessageUtil;
import cn.com.trueway.sys.pojo.Employee;
import cn.com.trueway.sys.service.EmployeeService;

public class PojJob {
	@Resource
	private EmployeeService employeeService;

	
	//定时任务执行方法体
	public void job() throws Exception {
		System.out.println("标准分析任务发布开始!");
		List<Employee> list = employeeService.findBrithday();
		if(list != null) {
			for(Employee emp : list) {
				String name = emp.getEmployeeName();
				String context = "亲爱的"+name+":今天是你的生日,公司祝福你生日快乐!!";
				String mobile = emp.getEmployeeMobile();
			SendMessageUtil.sendmsg(mobile, context, 666543);
			}
		}
		System.out.println("标准分析任务发布结束!");
	}
}

dao层

	@Override
		public List<Employee> findBrithday(){
			SimpleDateFormat df = new SimpleDateFormat("MM-dd");
	        String nowTime = df.format(new Date());
			String sql = "select * from zwkj_employee emp where to_char(emp.employee_birthday,'yyyy-mm-dd') like '%"+nowTime+"%'";
			return getEm().createNativeQuery(sql,Employee.class).getResultList();
		}

注意事项

  • Spring+Quartz的版本问题:Spring 3.0版本中内置的Quartz版本是<2.0的,在使用最新的Quartz包(>2.0)之后,接口不兼容
  • 解决办法有两种:
    1.降低Quartz版本,降到1.X去。
    2.升级Spring版本到3.1+,根据Spring的建议,将原来的TriggerBean替换成TriggerFactoryBean,例如CronTriggerBean 就可以替换成 CronTriggerFactoryBean。替换之后问题解决。

猜你喜欢

转载自blog.csdn.net/ShanGe9527/article/details/88394069