<?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="runkQuartz" class="com.idazui.lobby.quartz.runkQuartz">
<property name="daoRepo">
<ref bean="daoRepo"/>
</property>
</bean>
<!-- 注册定时器信息 -->
<bean id="taskInfo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 防止定时任务冲突执行,延迟作用 -->
<property name="concurrent" value="false" />
<!-- 指定要执行的定时任务类 这里是runkQuartz -->
<property name="targetObject">
<ref local="runkQuartz"/>
</property>
<!-- 指定定时器任务类要执行的方法名称 这里是execute -->
<property name="targetMethod">
<value>run</value>
</property>
</bean>
<!-- 配置定时器任务的调度器 -->
<bean id="quartzTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 声明要运行的实体 -->
<property name="jobDetail">
<ref bean="taskInfo"/>
</property>
<!-- 设置运行时间 -->
<property name="cronExpression">
<!-- 每天零点-->
<value>0 0 0 * * ?</value>
<!-- 每1分钟执行一次-->
<!-- <value>0 */1 * * * ?</value> -->
</property>
</bean>
<!-- 周定时器 -->
<bean id="weekRunkQuartz" class="com.idazui.lobby.quartz.weekRunkQuartz">
<property name="daoRepo">
<ref bean="daoRepo"/>
</property>
</bean>
<bean id="weekinfo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 防止定时任务冲突执行,延迟作用 -->
<property name="concurrent" value="false" />
<!-- 指定要执行的定时任务类 这里是runkQuartz -->
<property name="targetObject">
<ref local="weekRunkQuartz"/>
</property>
<!-- 指定定时器任务类要执行的方法名称 这里是execute -->
<property name="targetMethod">
<value>run</value>
</property>
</bean>
<bean id="weekrunk" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 声明要运行的实体 -->
<property name="jobDetail">
<ref bean="weekinfo"/>
</property>
<!-- 设置运行时间 -->
<property name="cronExpression">
<!-- 每周日零点 -->
<!-- <value>*/5 * * * * ?</value> -->
<value>0 0 0 ? * MON</value>
<!-- <value>0 0 0 ? * WED *</value> -->
<!-- <value>0 59 23 ? * WED</value> -->
</property>
</bean>
<!-- 月定时器 -->
<bean id="monthRunkQuartz" class="com.idazui.lobby.quartz.monthRunkQuartz">
<property name="daoRepo">
<ref bean="daoRepo"/>
</property>
</bean>
<bean id="monthinfo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 防止定时任务冲突执行,延迟作用 -->
<property name="concurrent" value="false" />
<!-- 指定要执行的定时任务类 这里是runkQuartz -->
<property name="targetObject">
<ref bean="monthRunkQuartz"/>
</property>
<!-- 指定定时器任务类要执行的方法名称 这里是execute -->
<property name="targetMethod">
<value>run</value>
</property>
</bean>
<bean id="monthrunk" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 声明要运行的实体 -->
<property name="jobDetail">
<ref bean="monthinfo"/>
</property>
<!-- 设置运行时间 -->
<property name="cronExpression">
<!-- 相当于每月第一天零点-->
<!-- <value>0 0 0 L * ? *</value> -->
<!-- 每月最后一天23:59 -->
<!-- <value>0 59 23 L * ?</value> -->
<!-- 每月一号零点 -->
<value>0 0 0 1 * ?</value>
</property>
</bean>
<!-- 心跳监听定时器 -->
<bean id="tokenQuartz" class="com.idazui.lobby.quartz.validUserToken">
<property name="daoRepo">
<ref bean="daoRepo"/>
</property>
</bean>
<bean id="tokeninfo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 防止定时任务冲突执行,延迟作用 -->
<property name="concurrent" value="false" />
<!-- 指定要执行的定时任务类 这里是runkQuartz -->
<property name="targetObject">
<ref bean="tokenQuartz"/>
</property>
<!-- 指定定时器任务类要执行的方法名称 这里是execute -->
<property name="targetMethod">
<value>run</value>
</property>
</bean>
<bean id="tokenrunk" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 声明要运行的实体 -->
<property name="jobDetail">
<ref bean="tokeninfo"/>
</property>
<!-- 设置运行时间 -->
<property name="cronExpression">
<!-- 每10秒执行一次-->
<value>0/10 * * * * ?</value>
</property>
</bean> <!-- 这个类用来做需要完成的业务-->
<bean id="doAutoPush" class="com.idazui.lobby.service.DoAutoPush">
<property name="daoRepo">
<ref bean="daoRepo"/>
</property>
</bean>
<!-- 定时任务 -->
<!-- 定义调用对象和调用对象的方法,这个配置和普通的一样的 -->
<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="doAutoPush" />
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>autoPush</value>
</property>
<property name ="concurrent" value ="false" />
</bean>
<!-- 定义触发时间 ,这边就不同了,这里必须将时间设置成无限长,因为我们要去读取数据库的时间来做为定时器的触发时间-->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobtask" />
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<!-- cron将时间设置成无限长 -->
<value>1 0 0 1 1 ? 2099</value>
</property>
</bean> <!-- 注册监听器 -->
<bean id="registerQuartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 注册定时器实体 集合 -->
<property name="triggers">
<list>
<ref bean="quartzTrigger"/>
<ref bean="weekrunk"/>
<ref bean="monthrunk"/>
<ref bean="tokenrunk"/>
<ref bean="cronTrigger"/>
</list>
</property>
</bean> <!--这里就是这里,就这个配置搞了我一天,这个类是用来设置触发时间的,调用这个类的init的方法来进行对时间的配置-->
<!--这个对象一定要注入,这样类才能进行管理,还有在类型要用get set方法,不然会报错。
<property name="scheduler" ref="registerQuartz" /> -->
<bean id="pushServiceImpl" class="com.idazui.lobby.service.DoAutoPushServiceImpl" lazy-init="false" init-method="init">
<property name="scheduler" ref="registerQuartz" />
</bean>
</beans>
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.text.ParseException; import org.apache.log4j.Logger;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.scheduling.quartz.CronTriggerBean;
import org.springframework.stereotype.Service; /**
* 功能说明:设置下发定时时间service接口实现类
*
* 创建人:wyb
*
*
*/
public class DoAutoPushServiceImpl{
protected Logger log = Logger.getLogger(getClass().getName()); //这个是总管理类
private Scheduler scheduler; public void init(){ try { CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger("cronTrigger", Scheduler.DEFAULT_GROUP);
String originConExpression = trigger.getCronExpression();
//以下几行用来测试时间用
/* Date date=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat rf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = "17:03:00";
String str2=df.format(date);
System.out.println(str2);
String bb=str2+" "+str;
Date shijian = rf.parse(bb);*/
//从fan()方法获取规定时间段内的随机时间
Date shijian=fan();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = format.format(shijian).substring(11, 19);
//因为是要每天做一次定时任务,所以我截取了字符串进行了拼接格式
String[] strTime = strDate.split(":");
String pushTime = strTime[2]+" "+strTime[1]+" "+strTime[0]+" * * ? ";
//以下就是重新对时间的设置,这样就可以达到动态设置效果。
trigger.setCronExpression(pushTime);
scheduler.rescheduleJob("cronTrigger", Scheduler.DEFAULT_GROUP, trigger);
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /* private String getCronExceptionDB() throws ParseException{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long timeNow= System.currentTimeMillis()/1000;
long timeLose=(System.currentTimeMillis()+2*60*60*1000)/1000;
//long timeLose=System.currentTimeMillis()+2*60*60*1000;
System.out.println(timeNow+" "+timeLose);
String dateNow=sdf.format(new Date((timeNow)*1000));
Date shijian=fan();
String ti=getCron(shijian);
return ti;
}*/
/***
*
* @param date
* @param dateFormat : e.g:yyyy-MM-dd HH:mm:ss
* @return
*/
public static String formatDateByPattern(Date date,String dateFormat){
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String formatTimeStr = null;
if (date != null) {
formatTimeStr = sdf.format(date);
}
return formatTimeStr;
}
/***
* convert Date to cron ,eg. "0 06 10 15 1 ? 2014"
* @param date : 时间点
* @return
*/
public String getCron(java.util.Date date){
String dateFormat="ss mm HH dd MM ? yyyy";
return formatDateByPattern(date, dateFormat);
}
public Date fan() throws ParseException{
/* Date date=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat rf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = "06:00:00";
String str2=df.format(date);
System.out.println(str2);
String bb=str2+" "+str;
Date today = rf.parse(bb); */
log.debug("进入随机时间方法");
Date today=timec();
log.debug("时间格式:"+today);
return today;
}
public Date timec() throws ParseException{
Date date=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat rf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//时间段开始
String str = "06:00:00";
String str2=df.format(date);
String bb=str2+" "+str;
Date one=rf.parse(bb);
long timeone=(one.getTime())/1000;
/* System.out.println(timeone);
System.out.println((timeone)/1000);*/
//时间段结束
String strone = "12:00:00";
String strtwo=df.format(date);
String two=strtwo+" "+strone;
Date newdate=rf.parse(two);
long timetwo=(newdate.getTime())/1000;
/* System.out.println(timetwo);
System.out.println((timetwo)/1000);*/
Random ra=new Random();
int onenum=(int)timetwo;
int twonum=(int)timeone;
int three=onenum-twonum;
/* System.out.println(onenum+" **");
System.out.println(twonum+" ^^");*/
int result=ra.nextInt(three)+twonum;
//System.out.println(result);
Date ndate=new Date((result)*1000L);
log.debug("最终获取时间为:"+ndate);
return ndate;
}
public Scheduler getScheduler() {
return scheduler;
} public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
} }
import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random; import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger; public class DoAutoPush {
private static Logger m_logger = Logger.getLogger(DoAutoPush.class);
private DaoRepo daoRepo;
public DaoRepo getDaoRepo() {
return daoRepo;
}
public void setDaoRepo(DaoRepo daoRepo) {
this.daoRepo = daoRepo;
}
public void autoPush(){
System.out.println("成功了");
} }

SSH框架,xml配置实现动态定时器的更多相关文章

  1. SSH框架中配置log4j的方法

    SSH框架中使用log4j的方便之处 1. 动态的改变记录级别和策略,即修改log4j.properties,不需要重启Web应用,这需要在web.xml中设置一下.2. 把log文件定在 /WEB- ...

  2. Java web.xml 配置技巧—动态欢迎页地址

    我们的 Java   Web  项目在配置web.xml 欢迎页地址默认是index.html .index.jsp ,不知道有人注意过没有,如果我要配置成/index/user.action  或者 ...

  3. mybatis使用注解替代xml配置,动态生成Sql

    mybatis使用注解替代xml配置时,遇到判断条件是否为null或者为空时,@Select很难搞定,不知道怎么办? mybatis3中增加了使用注解来配置Mapper的新特性,使用 SelectPr ...

  4. SSH框架项目配置和启动的加载顺序及请求的执行顺序

    1:======配置和启动====== (1)配置web.xml 配置<context-param>,其中内容为Spring的配置文件applicationContext.xml.注意&l ...

  5. SSH框架整合配置所需JAR包(SSH整合)

    转载于:http://www.cnblogs.com/kaige123/p/5719662.html Hibernate Jar: 1.hibernate3.jar,这个是hibernate3.0的核 ...

  6. SSH框架中配置Hibernate使用proxool连接池

    一.导入proxool.jar包 案例用的是proxool-0.8.3.jar,一般通过MyEclipse配置的SSH都会包含这个jar,如果没有,就去网上搜下下载导入就好了. 二.新建Proxool ...

  7. SSH框架的配置

    ^_^阅读本文前请先浏览 : http://www.cnblogs.com/LiJinfu/p/5842890.html 步骤 : 一.编写web.xml配置文件 该文件路径在项目文件下的WebCon ...

  8. SSH框架 spring 配置中的: scope="prototype"

    "可以利用容器的scope="prototype"来保证每一个请求有一个单独的Action来处理, 避免struts中Action的线程安全问题." 这句话怎么 ...

  9. Spring框架xml配置中属性ref与value的区别

    1.spring批量扫描mybatis的mapper,使用value 2.spring管理mybatis的单个mapper,用的是ref 虽然引用的是同一个bean,但两个对象的属相类型明显不一样,一 ...

随机推荐

  1. Ubuntu服务器搭建

    Ubuntu16 搭建Git 服务器 - 濮成林 - 博客园 https://www.cnblogs.com/charliePU/p/7528226.html Ubuntu 搭建 GitLab 笔记 ...

  2. DIXML(包括所有的W3C XML标准)

    Description:DIXml is an embedded XML, XSLT, and EXSLT processing library for Delphi (Embarcadero / C ...

  3. Redis系统管理

    EXISTS/DEL exists <key>判断某个key是否存在 del <key>删除某个key *** TYPE/KEYS type <key>获取key的 ...

  4. 深入理解 Win32 PE 文件格式 Matt Pietrek(慢慢体会)

    这篇文章假定你熟悉C++和Win32. 概述 理解可移植可执行文件格式(PE)可以更好地了解操作系统.如果你知道DLL和EXE中都有些什么东西,那么你就是一个知识渊博的程序员.这一系列文章的第一部分, ...

  5. Codility--- Distinct

    Task description Write a function class Solution { public int solution(int[] A); } that, given a zer ...

  6. Boyer-Moore字符串查找算法的实现

    前段时间在园子里看到一篇讲Boyer-Moore算法原理的文章http://kb.cnblogs.com/page/176945/,写的很详细,于是在这里自己写个C语言的实现,权当是练手吧. 基本思路 ...

  7. C# 设计模式,简单工厂

    C# 实现计算机功能 (封装,继承,多态) using System; using System.Collections.Generic; using System.Linq; using Syste ...

  8. 还在被大妈灵魂拷问?使用Python轻松完成垃圾分类!

    目录 0 环境 1 引言 2 思路 3 图像分类 4 总结 0 环境 Python版本:3.6.8 系统版本:macOS Mojave Python Jupyter Notebook 1 引言 七月了 ...

  9. 201907 TIOBE 编程语言排行榜-Python坐稳第三

    目录 一.编程语言7月排行榜 二.Top10编程语言指数走势(2002-2018) 三.历史排名(1988-2019) 四.编程语言"名人榜"( 2003-2018) 五.Top2 ...

  10. Codeforces 755A:PolandBall and Hypothesis(暴力)

    http://codeforces.com/problemset/problem/755/A 题意:给出一个n,让你找一个m使得n*m+1不是素数. 思路:暴力枚举m判断即可. #include &l ...