ssh集成quartz定时器

主要就分五步

1、加jar包

我这里用的quartz-1.8.5.jar、quartz-jobs-2.2.0.jar ,下载地址可以去https://mvnrepository.com这里下载

2、写一个执行任务的类

(里面就两个需要定期执行的方法,你可以写自己的方法),有个需要注意的地方就是service注入,要这么写DailyService dailyService =(DailyService) SpringContextUtil.getBeanValue("dailyService");才不会为空 如果使用@AutoWired会报空指针

public class Task {
	
	private DailyService dailyService;
	
	
	
	public DailyService getDailyService() {
		return dailyService;
	}
	public void setDailyService(DailyService dailyService) {
		this.dailyService = dailyService;
	}
	public void insertSystemRemind() {
		DailyService dailyService =(DailyService) SpringContextUtil.getBeanValue("dailyService");
		Map<String, Object> map = new HashMap<String, Object>();
		//获取下周过农历生日的民警信息
		List<Map<String, Object>> list = dailyService.findPoliceNongLiBirthdayList(map); 
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String content = "下周生日人员:";
		if(list != null && list.size() >0) {
			
			for(Map<String, Object> map1 : list) {
				String policeName = (String) map1.get("name");
				content += policeName +"、";
				
			}
			SystemRemind sr = new SystemRemind();
			System.out.println(content.substring(0, content.length()-1));
			sr.setXttxContent(content.substring(0, content.length()-1));
			sr.setXttxsendtime(sdf.format(new Date()));
			
			
			dailyService.saveSystemRemind(sr);
		}
	}
	
	public void insertSystemRemind1() {
		DailyService dailyService = (DailyService) SpringContextUtil.getBeanValue("dailyService");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//学习强国
		int count = dailyService.countPlantFormManage();
		System.out.println("学习强国:"+count);
		if(count ==0) {
			SystemRemind sr1 = new SystemRemind();
			sr1.setXttxContent("昨日‘学习强国’平台管理未完成,请于今日24时前录入工作数据。");
			sr1.setXttxsendtime(sdf.format(new Date()));
			dailyService.saveSystemRemind(sr1);
			System.out.println("成功插入一条提醒");
		}
		
		//每日考勤
		int count1 = dailyService.countDailyAttendanceCount();
		System.out.println("每日考勤:"+count1);
		if(count1 ==0) {
			SystemRemind sr2 = new SystemRemind();
			sr2.setXttxContent("昨日每日考勤未完成,请于今日24时前录入工作数据。");
			sr2.setXttxsendtime(sdf.format(new Date()));
			dailyService.saveSystemRemind(sr2);
			System.out.println("成功插入一条提醒");
		}
		//内务秩序
		int count2 = dailyService.countCheckOrderCount();
		System.out.println("内务秩序:"+count2);
		if(count2 ==0) {
			SystemRemind sr3 = new SystemRemind();
			sr3.setXttxContent("昨日内务秩序未完成,请于今日24时前录入工作数据。");
			sr3.setXttxsendtime(sdf.format(new Date()));
			dailyService.saveSystemRemind(sr3);
			System.out.println("成功插入一条提醒");
		}
		
		//窗口服务
		int count3 = dailyService.countWindowServer();
		System.out.println("窗口服务:"+count3);
		if(count3 ==0) {
			SystemRemind sr4 = new SystemRemind();
			sr4.setXttxContent("昨日窗口服务未完成,请于今日24时前录入工作数据。");
			sr4.setXttxsendtime(sdf.format(new Date()));
			dailyService.saveSystemRemind(sr4);
			System.out.println("成功插入一条提醒");
		}
		
		//民警工作状态
		int count4 = dailyService.countWorkStateCount();
		System.out.println("民警工作状态:"+count4);
		if(count4 ==0) {
			SystemRemind sr5 = new SystemRemind();
			sr5.setXttxContent("昨日民警工作状态未完成,请于今日24时前录入工作数据。");
			sr5.setXttxsendtime(sdf.format(new Date()));
			dailyService.saveSystemRemind(sr5);
			System.out.println("成功插入一条提醒");
		}
	}
	
	
	
}

3.在WEB-INF下创建一个QuartzConfig.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--注册定时执行任务实体-->
<bean id="lzstoneQuartzTask" class="cn.com.trueway.ntgzga.task.Task"/>
<!--注册定时器信息-->
<bean id="taskInfo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!--指定要执行的定时任务类  这里是LzstoneQuartzTask-->
<property name="targetObject">
<ref bean="lzstoneQuartzTask"/>
</property>
<!--指定定时器任务类要执行的方法名称 -->
<property name="targetMethod">
<value>insertSystemRemind</value>
</property>
</bean>
<!--注册定时器信息-->
<bean id="taskInfoNew" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!--指定要执行的定时任务类  这里是LzstoneQuartzTask-->
<property name="targetObject">
<ref bean="lzstoneQuartzTask"/>
</property>
<!--指定定时器任务类要执行的方法名称 -->
<property name="targetMethod">
<value>insertSystemRemind1</value>
</property>
</bean>
<!--配置定时器任务的调度器-->
<bean id="quartzTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!--声明要运行的实体-->
<property name="jobDetail">
    <ref bean="taskInfo"/>
</property>
<!--设置运行时间 每周一0点执行一次-->
<property name="cronExpression">
    <value>0 00 0 ? * MON</value>
</property>
</bean>
<!--配置定时器任务的调度器-->
<bean id="quartzTriggerNew" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!--声明要运行的实体-->
<property name="jobDetail">
    <ref bean="taskInfoNew"/>
</property>
<!--设置运行时间 每天完成23点59分30秒执行一次-->
<property name="cronExpression">
    <value>0 59 23 * * ?</value>
</property>
</bean>
<!--注册监听器-->
<bean id="registerQuartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!--注册定时器实体 集合-->
<property name="triggers">
    <list>
          <ref bean="quartzTrigger"/>
          <ref bean="quartzTriggerNew"/>
    </list>
</property>
</bean>
</beans>

我这里有两个任务分别是按不同的时间执行 所以定时器要注册两个 ,定时器任务的调度器也要注册两个,监听器里也加两个调度器

4.在web.xml中将创建的QuartzConfig.xml配置进去

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/*/*.xml,/WEB-INF/QuartzConfig.xml</param-value>
</context-param>

5.重启tomcat试下

以下为Cron表达式时间字段:

                        

●星号(*):可用在所有字段中,表示对应时间域的每一个时刻,例如,*在分钟字段时,表示“每分钟”;
●问号(?):该字符只在日期和星期字段中使用,它通常指定为“无意义的值”,相当于点位符;
●减号(-):表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12;
●逗号(,):表达一个列表值,如在星期字段中使用“MON,WED,FRI”,则表示星期一,星期三和星期五;
●斜杠(/):x/y表达一个等步长序列,x为起始值,y为增量步长值。如在分钟字段中使用0/15,则表示为0,15,30和45秒,而5/15在分钟字段中表示5,20,35,50,你也可以使用*/y,它等同于0/y;
●L字母:说明了某域上允许的最后一个值。它仅被日和周域支持。当用在日域上,表示的是在月域上指定的月份的最后一天。
●W字母:W 字符代表着平日 (Mon-Fri),并且仅能用于日域中。它用来指定离指定日的最近的一个平日。大部分的商业处理都是基于工作周的,所以 W 字符可能是非常重要的。例如,日域中的15W 意味着 "离该月15号的最近一个平日。" 假如15号是星期六,那么 trigger 会在14号(星期五)触发,因为星期四比星期一(这个例子中是17号)离15号更近。
●井号(#):# 字符仅能用于周域中。它用于指定月份中的第几周的哪一天。例如,如果你指定周域的值为6#3,它意思是某月的第三个周五 (6=星期五,#3意味着月份中的第三周)。 
 

如果对Cron表达式不熟练,我们可以去在线Cronn表达式生成器(点击打开链接)生成我们所需要的表达式。

java小白一枚 随便写写 如有不对的地方 多多赐教

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值